Tricky delay on mouseover

前端 未结 5 2256
借酒劲吻你
借酒劲吻你 2021-02-14 14:10

This is what I have currently:

$(\"#cart-summary\").mouseenter(function () {
    $(\'.flycart\').delay(500).slideDown(\'fast\');
});
$(\".flycart\").mouseleave(f         


        
相关标签:
5条回答
  • 2021-02-14 14:50

    Use the setTimeout to check if a flag you set/unset (I'm using a class) is still valid.

    $("#cart-summary").mouseenter(function () {
        $("#cart-summary").addClass("hasFocus");
        setTimeout(function(){ 
            if ($("#cart-summary").hasClass("hasFocus")) {
                $('.flycart').slideDown('fast');
            }
          }, 500 );
          });
    
    $("#cart-summary").mouseleave(function () {
        $("#cart-summary").removeClass("hasFocus");
    });
    
    
    $(".flycart").mouseenter(function () {
        $("#cart-summary").removeClass("lostFocus");    
    });
    
    $(".flycart").mouseleave(function () {
        $("#cart-summary").addClass("lostFocus");
        setTimeout(function(){ 
            if ($("#cart-summary").hasClass("hasFocus")) { 
                $('.flycart').slideUp('fast');
                }).find('a.close').click(function(){
                $(this).parents('.flycart').hide();
            }
        }, 500)
    });
    
    0 讨论(0)
  • 2021-02-14 14:52

    Sounds like you might need the HoverIntent plugin.

    I don't know if it'll help you with the mouseout ATLEAST 500ms though. But maybe there's an option in there for that.

    0 讨论(0)
  • 2021-02-14 14:52

    offer a solution

    var mouseenterTimerCart;
    var mouseleaveTimerCart;
    
    $(document).on({
        mouseenter: function () {
            if (mouseleaveTimerCart) clearTimeout(mouseleaveTimerCart);
            mouseenterTimerCart = setTimeout(function() {
                $("#head-cart .cart_items").show()
            }, 500);
        },
        mouseleave: function () {
            if (mouseenterTimerCart) clearTimeout(mouseenterTimerCart);
            mouseleaveTimerCart = setTimeout(function() {
                $("#head-cart .cart_items").hide()
            }, 500);
        }
    }, "#head-cart .full, #head-cart .cart_items");
    

    will work if the basket is updated dynamically

    $("#head-cart").html(...crat-html-block...)
    
    0 讨论(0)
  • 2021-02-14 14:59

    You could do:

    var timeout,
        $flycart = $(".flycart"),
        $summary = $("#cart-summary"),
        delay = 500;
    
    $summary.mouseenter(function () {
        if (timeout) window.clearTimeout(timeout);
        timeout = window.setTimeout(function(){
            if ( $flycart.is(":visible") ) {
                $flycart.slideDown('fast');
            }
        }, delay);
    });
    $flycart.mouseleave(function () {
        if (timeout) window.clearTimeout(timeout);
        timeout = window.setTimeout(function(){
            if ( $flycart.is(":visible") ) {
                $flycart.slideUp('fast');
            }
        }, delay);
    })...
    
    0 讨论(0)
  • 2021-02-14 15:04

    You can use the hoverIntent plugin as follows:

    var config = {    
         sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
         interval: 500, // number = milliseconds for onMouseOver polling interval    
         timeout: 500, // number = milliseconds delay before onMouseOut    
    };
    
    $(".flycart").hoverIntent(function () {
            $('.flycart').slideDown('fast');
    }, function() {
            $('.flycart').slideUp('fast');
    }).find('a.close').click(function(){
       $(this).parents('.flycart').hide();
    });
    
    0 讨论(0)
提交回复
热议问题