首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当对SetTimeout中的每个函数使用JavaScript时,会有奇怪的反应

当对SetTimeout中的每个函数使用JavaScript时,会有奇怪的反应
EN

Stack Overflow用户
提问于 2017-07-16 12:49:25
回答 2查看 96关注 0票数 0

在运行setTimeout中的每个函数时,我使用JavaScript函数来增加延迟。

但是,第一个循环运行良好,但是,在您向上滚动并再次向下滚动之后,图标上会出现故障。

HTML代码

代码语言:javascript
复制
<div id="box"></div>
<div class="service-modules-section">
  <div class="service-modules">
    <div class="half service-row-left">

      <div class="service-item">
        <div class="icon-wrap">
          <img alt="Canadian News Reporting Icon" class="img-fluid" src="http://icons.iconarchive.com/icons/vcferreira/firefox-os/128/sms-icon.png" style="display: inline;" width=50px>
        </div>
        <div class="service-meta">
          <h3>News Reporting</h3>
          <p>From breaking national, regional and world news to the biggest events in politics, sports, business and entertainment, we are there when it matters, delivering news about Canadians to Canadians, 24/7.ok.</p>            </div>
        <div class="clearfix"></div>
      </div>  
      <div class="service-item">
        <div class="icon-wrap">
          <img alt="Canadian News Reporting Icon" class="img-fluid" src="http://icons.iconarchive.com/icons/vcferreira/firefox-os/128/sms-icon.png" style="display: inline;" width=50px>
        </div>
        <div class="service-meta">
          <h3>News Reporting</h3>
          <p>From breaking national, regional and world news to the biggest events in politics, sports, business and entertainment, we are there when it matters, delivering news about Canadians to Canadians, 24/7.ok.</p>            </div>
        <div class="clearfix"></div>
      </div>  
      <div class="service-item">
        <div class="icon-wrap">
          <img alt="Canadian News Reporting Icon" class="img-fluid" src="http://icons.iconarchive.com/icons/vcferreira/firefox-os/128/sms-icon.png" style="display: inline;" width=50px>
        </div>
        <div class="service-meta">
          <h3>News Reporting</h3>
          <p>From breaking national, regional and world news to the biggest events in politics, sports, business and entertainment, we are there when it matters, delivering news about Canadians to Canadians, 24/7.ok.</p>            </div>              
    </div>

  </div>
</div>
<div id="box"></div>

CSS代码:

代码语言:javascript
复制
#box {
  height: 600px;
}
.service-modules-section{
  position: relative;
}
img {
  display: none;
  position: relative;
  top: 90px;
}

JavaScript代码:

代码语言:javascript
复制
var speed = 500;  
var waypoint = new Waypoint({
  element: $('.icon-wrap').children(),
  handler: function(direction) {     
      $('.icon-wrap').children().each(function(k, v){
        var el = this;
        setTimeout(function (){
          $(el).animate({
        'opacity': '70'
    }, {
        step: function (now, fx) {          
            $(el).css({"transform": "translate3d(0px, " + -now + "px, 0px)"});
        },
        duration: 1000,
        easing: 'linear',
        queue: false,
        complete: function () {
            // alert('Animation is done');
        }
    }, 'linear');

    $(el).animate({ textIndent: 100 }, {
        duration : 1000,
        easing: 'linear',
        queue: false
    });   
        }, k*speed);                        

    })  

    if(direction === "up"){
      $('.icon-wrap').children().css('top', '100px');
    }
  },  offset: '75%'
});

对发生什么有什么想法吗?谢谢,

https://codepen.io/techcater/pen/jwJZZe

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-07-18 17:46:49

我最终找到了解决这一问题的办法,在上下滚动时给出了条件。

代码语言:javascript
复制
var speed = 500;
var waypoint = new Waypoint({
  element: $('.icon-wrap').children(),
  handler: function(direction) {
    if(direction === "down"){
      $('.icon-wrap').children().each(function(k, v){
        var el = this;

        setTimeout(function (){
          $(el).animate({
            'opacity': '65'
          }, {
            step: function (now, fx) {
              $(el).css({"transform": "translate3d(0px, " + -now + "px, 0px)"});
            },
            duration: 1000,
            easing: 'linear',
            queue: false,
            complete: function () {
            }
          }, 'linear');

          $(el).animate({ textIndent: 100 }, {
            duration : 1000,
            easing: 'linear',
            queue: false
          });
        }, k*speed);

      });
    }

    if(direction === "up"){
      $('.icon-wrap').children().css({"opacity": "0"});
    }
  },  offset: '75%'
});
票数 0
EN

Stack Overflow用户

发布于 2017-07-16 12:56:55

您可以创建一个变量来检查动画是否已经完成。如果没有,评估动画,如果是,不要做任何事情。

“联合来文法典”固定:

代码语言:javascript
复制
var speed = 500;
var completed = false;
var waypoint = new Waypoint({
  element: $('.icon-wrap').children(),
  handler: function(direction) {
    if (!completed) {
      $('.icon-wrap').children().each(function(k, v){
        var el = this;
        setTimeout(function (){
          $(el).animate({
        'opacity': '70'
    }, {
        step: function (now, fx) {          
            $(el).css({"transform": "translate3d(0px, " + -now + "px, 0px)"});
        },
        duration: 1000,
        easing: 'linear',
        queue: false,
        complete: function () {
             completed = true;
        }
    }, 'linear');

    $(el).animate({ textIndent: 100 }, {
        duration : 1000,
        easing: 'linear',
        queue: false
    });   
        }, k*speed);                        

    })  

    if(direction === "up"){
      $('.icon-wrap').children().css('top', '100px');
    }
    }
  },  offset: '75%'

});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45128808

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档