延迟链接点击

hen*_*ron 3 javascript jquery delay

这是我正在使用的小提琴:http://jsfiddle.net/Scd9b/

如何在点击后延迟href功能?

例如,用户点击链接,消息向下滑动一会儿......在2秒后,用户继续浏览其链接的页面.

对不起,每个人都忘了提到有些锚没有链接.

tec*_*bar 6

检查这个小提琴:http://jsfiddle.net/C87wM/1/

像这样修改你的切换:

$("a.question[href]").click(function(){
    var self = $(this);
    self.toggleClass("active").next().slideToggle(2000, function() {
        window.location.href = self.attr('href'); // go to href after the slide animation completes
    });
    return false; // And also make sure you return false from your click handler.
});
Run Code Online (Sandbox Code Playgroud)

  • +1用于使用动画的完成功能而不是`setTimeout()`. (2认同)

ctc*_*rry 5

您可以通过设置 window.location 模拟导航到页面。因此,我们将使用 阻止链接的正常功能,preventDefault然后在 a 中setTimeout,我们将设置正确的 window.location:

https://codepen.io/anon/pen/PePLbv

$("a.question[href]").click(function(e){
    e.preventDefault();
    if (this.href) {
        var target = this.href;
        setTimeout(function(){
            window.location = target;
        }, 2000);
    }
});
Run Code Online (Sandbox Code Playgroud)