简体   繁体   English

div在屏幕外快速动画

[英]a div animates outside the screen with rapid clicking

I have this code: 我有以下代码:

$(document).ready(function(){

    $(".subscriptions").click(function (){

        if($(".pollSlider").css("margin-right") == "-200px"){
            $('.pollSlider').animate({"margin-right": '+=200'});
            }else{
            $('.pollSlider').animate({"margin-right": '-=200'});
            }
        });

//other exceptions

if($(".pollSlider").css("margin-right") > "200px"){
    $(".pollSlider").css({'margin-righ':'200px'});
    }
if($(".pollSlider").css("margin-right") < "0px"){
    $(".pollSlider").css({'margin-righ':'0px'});
    }
 }); 

this is the css: 这是CSS:

.pollSlider{
    overflow: auto;
    top:0px;
    position:fixed;
    height:100%;
    background:red;
    width:200px;
    right:0px;
    margin-right: -200px;
    z-index:100;
}

I have a div, when the user click a button the div slides from the left 200px into the screen and when the user clicks the same button again the div animate to the right 200px. 我有一个div,当用户单击按钮时,div从左侧的200px滑入屏幕,而当用户再次单击相同的按钮时,div动画至右侧的200px。 This system works very good but the problem is, if the user kept clicking while the div animating left or right, the div will animate out side the screen (to the right) and does not come back again when the user clicks again. 该系统效果很好,但问题是,如果用户在div左右移动时继续单击,则div将在屏幕外侧(右侧)进行动画处理,并且当用户再次单击时不会再次回来。 So I tried to add the //other exceptions but those if statements did not work. 所以我试图添加其他异常,但是那些if语句不起作用。 How can I fix this problem ? 我该如何解决这个问题?

I've created a Demo where I tried to achieve the effect you're trying to produce. 我创建了一个演示 ,在其中尝试实现您要产生的效果。 As mentioned in the comments, jQuery .stop(…) is used to empty the animation queue before starting the next animation. 如评论中所述,jQuery .stop(…)用于在开始下一个动画之前清空动画队列。

I also changed the units from relative ( +=200 and -=200 ) to fixed numbers ( 0 and -width ). 我还将单位从相对( +=200-=200 )更改为固定数字( 0-width )。 If you assume the animation will be stopped midstream and you'll need to animate back, you'll need to use either fixed numbers, or do the calculation on the fly before each new animation. 如果您假设动画将在中途停止播放,并且需要向后进行动画处理,则需要使用固定数字,或者在每次播放新动画之前即时进行计算。

My solution for this kind of problem is to add a global variable nad set it to false after user click and set it back to tru when the animation is done: 对于此类问题,我的解决方案是添加一个全局变量nad,在用户单击后将其设置为false,并在完成动画后将其设置回tru:

var allowAnimate = true;
$(document).ready(function(){

$(".subscriptions").click(function (){
    if(allowAnimate){
    if($(".pollSlider").css("margin-right") <= "-200px"){
        allowAnimate = false;
        $('.pollSlider').animate({"margin-right": '+=200'}, function(){allowAnimate = true});
        }else if($(".pollSlider").css("margin-right") >= "200px")){
        $('.pollSlider').animate({"margin-right": '-=200'}, function(){allowAnimate = true});
        }
    }
    });

//other exceptions
....

So if User click a button the variable allowAnimate is checked if its true , I set it to false , and the animation will began, on the animation callback function I set it back to true . 因此,如果用户单击按钮,将检查变量allowAnimate是否为true ,则将其设置为false ,动画将开始,在动画回调函数上,将其设置为true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM