If mouse over for over 2 seconds then show else don't?

前端 未结 2 2081
孤街浪徒
孤街浪徒 2020-12-02 15:42

Here is a jQuery slide function I have applied to a div on hover in order to slide a button down.

It works fine except that now everytime someone moves in and out o

相关标签:
2条回答
  • 2020-12-02 16:17
    var time_id;
    
    $("#NewsStrip").hover(
    function () {
        if (time_id) {
            clearTimeout(time_id);
        } 
        time_id = setTimeout(function () {
            $("#SeeAllEvents").stop(true, true).slideDown('slow');
        }, 2000);
    }, function () {
        if (time_id) {
            clearTimeout(time_id);
        } 
        time_id = setTimeout(function () {
            $("#SeeAllEvents").stop(true, true).slideUp('slow');
        }, 2000);
    });
    
    0 讨论(0)
  • 2020-12-02 16:23

    You need to set a timer on mouseover and clear it either when the slide is activated or on mouseout, whichever occurs first:

    var timeoutId;
    $("#NewsStrip").hover(function() {
        if (!timeoutId) {
            timeoutId = window.setTimeout(function() {
                timeoutId = null; // EDIT: added this line
                $("#SeeAllEvents").slideDown('slow');
           }, 2000);
        }
    },
    function () {
        if (timeoutId) {
            window.clearTimeout(timeoutId);
            timeoutId = null;
        }
        else {
           $("#SeeAllEvents").slideUp('slow');
        }
    });
    

    See it in action.

    0 讨论(0)
提交回复
热议问题