简体   繁体   English

上下滚动一次运行动画

[英]run animation once while scroll down and up

I am working on one project and i am having difficultly to solve this issue. 我正在一个项目上,很难解决这个问题。 Here is problem:- When user scroll down and lime color div visible animation will start. 这是问题所在:-当用户向下滚动并且石灰色div时,可见动画就会开始。 when user scroll up animation will stop. 当用户向上滚动时,动画将停止。 But real problem is animation running multiple times when lime color div visible. 但是真正的问题是动画在可见div时会运行多次。 I want to run animation only once when lime color div visible. 当石灰色div可见时,我只想运行一次动画。 Please see jsfiddle demo. 请参阅jsfiddle演示。 Here is javascript code snippet. 这是javascript代码段。

function scollPosition(){
var sotpScroll = 0;
    $(window).scroll(function(){
        if(sotpScroll == 0){
            sotpScroll = 1;
            var cPosition = $('.c').offset();
            var animationStartPoint = cPosition.top - 100;
            console.log(animationStartPoint);
            // console.log('c class position' + cPosition.top);
            var dPosition = $('.d').offset();
            // console.log('d class position' + dPosition.top);
            var windowPosition = window.pageYOffset;
            console.log('window position:- ' + windowPosition + ' dPosition.top:- ' + dPosition.top);
            if (windowPosition > animationStartPoint && windowPosition < dPosition.top){
                animation();
            }
        }
        setTimeout(function(){sotpScroll=0},10);
    });
}

Thankx in advance and apologies for my english. 提前谢谢,并为我的英语道歉。 Please help me to solve this bug 请帮我解决这个错误

Add a global flag that indicates if the area is visible or not: 添加一个全局标志,指示该区域是否可见:

var areaVisible

[...]

if (windowPosition > animationStartPoint && windowPosition < dPosition.top) {
// Area is visible
   if (!areaVisible) { // If just became visible
      animation(); 
   }
   areaVisible = true; // Prevent more animations
} else {
   areaVisible = false;
}

Fiddle: http://jsfiddle.net/bortao/BB2k5/ 小提琴: http : //jsfiddle.net/bortao/BB2k5/

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

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