jquery禁用单击直到动画完全完成 [英] jquery disable click until animation is fully complete

查看:30
本文介绍了jquery禁用单击直到动画完全完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想禁用单击功能,直到其中的每个代码;初始化并完成.

我阅读了这些文章并尝试了他们所说的但无法实现:event.preventDefault() vs. return false+在 jQuery 中删除事件处理程序的最佳方法?+jQuery - 禁用点击直到所有链接动画完成

我为这个问题准备了一个简单的例子:

这是演示版

html

1

<div class="ele">2</div><div class="ele">3</div>

jQuery

$('.ele').click(function() {if ( !$('.ele').is(':animated') ) {//这也有效,但是;//is:animate 持续时间返回 = 2000 但需要为 4000$('.ele').stop().animate({'top':'0px'},2000);$(this).stop().animate({'top':'100px'},4000);}返回假;});

解决方案

使用 on() 和 off() 打开/关闭点击功能:

$('.ele').on('click', animateEle);函数 animateEle(e) {$('.ele').stop().animate({'top':'0px'},2000);$(e.target).off('click').stop().animate({'top':'100px'},4000, function() {$(e.target).on('click', animateEle);});}

演示

i want to disable click function untill every code in it; initialized and completed.

i read these articles and tried what they said but cant make it happen: event.preventDefault() vs. return false + Best way to remove an event handler in jQuery? + jQuery - Disable Click until all chained animations are complete

i prepared simple example for this question:

This is DEMO

html

<div class="ele">1</div>
<div class="ele">2</div>
<div class="ele">3</div>

jQuery

$('.ele').click(function() {

    if ( !$('.ele').is(':animated') ) {//this works too but;
                         //is:animate duration returns = 2000 but need to be 4000
        $('.ele').stop().animate({'top':'0px'},2000);
        $(this).stop().animate({'top':'100px'},4000);
    } 
    return false;
});
​

解决方案

Use on() and off() to turn the click function on/off :

$('.ele').on('click', animateEle);

function animateEle(e) {
    $('.ele').stop().animate({'top':'0px'},2000);
    $(e.target).off('click').stop().animate({'top':'100px'},4000, function() {
        $(e.target).on('click', animateEle);
    });
}​

DEMONSTRATION

这篇关于jquery禁用单击直到动画完全完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆