开发者

jquery wait for .animate then go to anchor location

开发者 https://www.devze.com 2023-01-18 11:48 出处:网络
I\'m using jquery animate to slide a div off the page, this works fine but i would like to change the animate to:

I'm using jquery animate to slide a div off the page, this works fine but i would like to change the animate to:

  1. only happen once(because if somebody double clicks it, the div comes back), and
  2. wait for animation to finish before the href is followed.

This is the code that performs the div slide:

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

    $contentDiv.stop().animate({
        marginLeft: parseInt($contentDiv.css('marginLeft'),10) == 0 ?
        开发者_如何学编程$contentDiv.outerWidth() : 0
    });
});


You can call event.preventDefault() to prevent the default click action, and then do a manual redirect by setting location.href after the animation using a callback...

$(".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();
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号