javascript - 如何让用户在滑动之前悬停两秒

标签 javascript jquery delay slide

我下面有一个工作代码,它具有 slideDown 功能,但我希望用户在下滑之前至少悬停 2 秒。如果用户没有将鼠标悬停 2 秒,则不应向下滑动。

我尝试过延迟,但即使我没有在链接上悬停 10 秒,它仍然会触发。

下面是我的代码和 fiddle :

$(document).ready(function() {
    $(".item-content").hide();

    $(".item-show").on("mouseover", function() {
        $(this).delay(10020).hide().parent('p').next(".item-content").delay(1520).slideDown( "slow" );
    });

    $("span.close-icon").on("click", function() {
        $(this).parent('.item-content').slideUp('slow').parent().find('.item-show').show();
    });
});

$("div.previewCardPageClose").on("click", function() {
    $(this).parent('.previewCard-content').slideUp('slow').parent().find('.previewCard-showhide').show();
});

最佳答案

jsFiddle

您需要(除了代码中的注释之外)对动态生成的元素使用 .on() 方法

$(document).ready(function() {

   var timeo = null;

   $(".item-content").hide(); // Hide all!!!

    $("body").on("mouseenter", ".item-show", function() { // Use rather mouseenter!

        var $that = $(this);           // Store the `this` reference
        clearTimeout( timeo );         // Clear existent timeout on m.Enter 

        timeo = setTimeout(function(){  // Before setting a new one
            $that.hide().closest('p').next(".item-content").slideDown( "slow" );
        }, 2000);

    }).on("mouseleave", ".item-show", function(){   // mouse leaves? Clear the timeout again!
        clearTimeout( timeo );
    });  

    $(".close-icon").on("click", function() {
        var $itemB = $(this).closest(".item-b");
        $itemB.find(".item-content").slideUp();
        $itemB.find(".item-show").show();
    });

});

关于javascript - 如何让用户在滑动之前悬停两秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33425485/

相关文章:

javascript - HTML 多种表单,带有 2 个 JavaScript

javascript - 如何让ckeditor停止删除空的div

javascript - 下拉列表中未选择值,请将内联 js 代码转换为外部 j 查询

javascript - 一个Span内的Span,为什么样式沿用了外层的Span?

javascript - 在 OnKeyUp 事件问题中引用 Javascript 变量

javascript - 计算总和并将其值相乘

javascript - 使用 jquery 动态生成目标内容

Python代码,控制台文本的时间延迟

CSS 关键帧动画隐藏在 Firefox 开始之前,但不在 Internet Explorer 中

c# - Entity Framework 代码优先遇到延迟