jQuery:防止点击队列

发布于 2024-10-08 06:29:09 字数 714 浏览 0 评论 0原文

$("#right").live("click", function(event) {
    $(".image_scroll").animate({"left": "+=300px"}, "slow");
});

我需要点击基本上不注册,直到动画完成。 我尝试过使用 return false 但这似乎完全杀死了链接。

还尝试使用 $(".image_scroll").stop().animate({"left": "+=300px"}, "slow"); 但这只会使动画断断续续,看起来有故障。

编辑:id #right 位于图像上,而不是 a 标记上。 HTML如下:

<img src="/images/right.png" id="right" alt="" />
<div id="container">
    <div id="image_scroll">
        <img src="1.jpg" />
        <img src="3.jpg" />
        <img src="4.jpg" />
    </div><!-- /#image_scroll -->
</div> <!-- /#container
$("#right").live("click", function(event) {
    $(".image_scroll").animate({"left": "+=300px"}, "slow");
});

I need the click to basically not register until the animation is done.
I've tried using return false but that just seems to kill the link entirely.

Tried also using $(".image_scroll").stop().animate({"left": "+=300px"}, "slow"); but that just makes the animation stutter and look glitchy.

Edit: The id #right is on an image, not an a tag. The HTML is as follows:

<img src="/images/right.png" id="right" alt="" />
<div id="container">
    <div id="image_scroll">
        <img src="1.jpg" />
        <img src="3.jpg" />
        <img src="4.jpg" />
    </div><!-- /#image_scroll -->
</div> <!-- /#container

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

别忘他 2024-10-15 06:29:09

您需要提供动画回调...

$("#right").live("click", function(event) {
    event.preventDefault();
    var src = $(this).attr('href');
    $(".image_scroll").animate({"left": "+=300px"}, "slow", function() { window.location.href = src; });
});

You need to provide a callback to animate...

$("#right").live("click", function(event) {
    event.preventDefault();
    var src = $(this).attr('href');
    $(".image_scroll").animate({"left": "+=300px"}, "slow", function() { window.location.href = src; });
});
睫毛溺水了 2024-10-15 06:29:09

您需要存储动画是否正在运行,您可以使用 jQuery.data 函数:

$("#right").live("click", function(event) {
    var el = $(this),
        state = el.data('animation_state');
    if(state == 'busy') {
       //ignore clicks while the animation is running
       return false;
    } 
    el.data('animation_state', 'busy');
    $(".image_scroll").animate({"left": "+=300px"}, "slow", function() {
       //finished animating      
       //set the state to finished so we can click the element the next time
      el.data('animation_state', 'complete'); 
    }); 
});

You'll need to store the whether the animation is running or not, you could use the jQuery.data function for that:

$("#right").live("click", function(event) {
    var el = $(this),
        state = el.data('animation_state');
    if(state == 'busy') {
       //ignore clicks while the animation is running
       return false;
    } 
    el.data('animation_state', 'busy');
    $(".image_scroll").animate({"left": "+=300px"}, "slow", function() {
       //finished animating      
       //set the state to finished so we can click the element the next time
      el.data('animation_state', 'complete'); 
    }); 
});
悲欢浪云 2024-10-15 06:29:09
var handler = function(event) {
    $("#right").die("click");
    $(".image_scroll").animate({"left": "+=300px"}, "slow", function(){
        $("#right").live("click", handler);
    });
};
$("#right").live("click", handler);
var handler = function(event) {
    $("#right").die("click");
    $(".image_scroll").animate({"left": "+=300px"}, "slow", function(){
        $("#right").live("click", handler);
    });
};
$("#right").live("click", handler);
挖鼻大婶 2024-10-15 06:29:09

检查动画是否正在运行,如果是,

$("#right").live("click", function(event) {

    if ( $(".image_scroll").is(":animated") ) {
      return false;
    }

    $(".image_scroll").animate({"left": "+=300px"}, "slow");
});

Check if the animation is running and, if so,

$("#right").live("click", function(event) {

    if ( $(".image_scroll").is(":animated") ) {
      return false;
    }

    $(".image_scroll").animate({"left": "+=300px"}, "slow");
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文