【问题标题】:jquery wait for .animate then go to anchor locationjquery 等待 .animate 然后去锚点位置
【发布时间】:2010-09-28 17:27:48
【问题描述】:

我正在使用 jquery animate 将 div 滑出页面,这很好,但我想将动画更改为:

  1. 只发生一次(因为如果有人双击它,div 会返回),并且
  2. 在跟随 href 之前等待动画完成。

这是执行 div 幻灯片的代码:

$(".choose").click(function(event) {
    var $contentDiv = $("#content");

    $contentDiv.stop().animate({
        marginLeft: parseInt($contentDiv.css('marginLeft'),10) == 0 ?
        $contentDiv.outerWidth() : 0
    });
});

【问题讨论】:

    标签: jquery jquery-animate


    【解决方案1】:

    您可以调用event.preventDefault() 来阻止默认的点击动作,然后通过在动画后使用回调设置location.href 进行手动重定向...

    $(".choose").click(function(event) {
        var $contentDiv = $("#content");
        var redir = $(this).attr("href");
    
        $contentDiv.stop().animate({
            marginLeft: parseInt($contentDiv.css('marginLeft'),10) == 0 ?
            $contentDiv.outerWidth() : 0
        }, function() { location.href = redir; });
    
        event.preventDefault();
    });
    

    【讨论】:

    • 这就是我想要的,我一直在用 event.prenetDefault 拐弯抹角,但它并没有像我那样工作,哈哈。谢谢
    猜你喜欢
    • 2016-11-04
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多