JQuery slideToggle timeout

前端 未结 3 1307
盖世英雄少女心
盖世英雄少女心 2021-01-23 08:01

I have simple html page:





        
相关标签:
3条回答
  • 2021-01-23 08:26

    Try this code

     if($('.to_hide').css("display") == "block")
    {
        $(".to_hide").mouseout(function(){
    
            setTimeout(hidepara,10000);
        })
    }
    function hidepara()
    
        { $(".to_hide").hide();
    
    }
    

    Working sample http://jsfiddle.net/kaYLG/

    0 讨论(0)
  • 2021-01-23 08:28

    Check http://jsfiddle.net/XRYLk/3/

    I added mouseleave so in case the mouse was over it when first function fires up, it will set timer on mouseleave.

    jQuery:

        $("button").click(function() {
            $("div").slideToggle("slow");
        });
    
    setTimeout(hidepanel, 4000);
    
    function hidepanel(){
        if($('div').is(':hover') === false){ $('div').slideToggle(); }
    }
    
     $('div').mouseleave(function(){ setTimeout(hidepanel, 4000); });
    
    0 讨论(0)
  • 2021-01-23 08:28

    This is a very simple solution. Idea is, if you don't move your mouse over the div-container.. it will slideUp() the container itself in 2000ms (I put 2000ms, because its boring to wait 10sec).

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
        <style>
            div {width: 400px; border: 1px solid;}
        </style>
    </head>
    <body>
        <div>
            This is the paragraph to end all paragraphs.  You should feel <em>lucky</em> to have seen such a paragraph in your life. Congratulations!
        </div>
        <script>
            $(document).ready(function () {
                var mouseover_to = setTimeout(function () {
                    $("div").slideUp("slow");
                }, 2000); // Change it to 10000 to be 10sec
                $('div').mouseover(function () {
                    clearTimeout(mouseover_to);
                });
            });
        </script>
    </body>
    </html>
    

    [ View output ]

    1. First it will wait till the document is ready
    2. It will start the countdown to 2000ms with setTimeout() and sets it as resource to mouseover_to variable.
    3. However, if mouseover() is detected over the div then the countdown will be canceled with clearTimeout(), thanks to the help of the resource mouseover_to
    0 讨论(0)
提交回复
热议问题