Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 设置jQuery中的超时。每个都不工作_Javascript_Jquery_Animation_Settimeout - Fatal编程技术网

Javascript 设置jQuery中的超时。每个都不工作

Javascript 设置jQuery中的超时。每个都不工作,javascript,jquery,animation,settimeout,Javascript,Jquery,Animation,Settimeout,我想模仿的菜单项动画 以下是我为制作动画而编写的关键代码: li{transition:transform 600ms} li.animated{transform:translateY(20px)} /* Javascript */ $('nav ul li').each(function(i){ setTimeout(function(){ $('ul li').addClass('animated'); },400*i) }) 但它不起作用,在这种情

我想模仿的菜单项动画

以下是我为制作动画而编写的关键代码:

li{transition:transform 600ms}
li.animated{transform:translateY(20px)}

/* Javascript */
  $('nav ul li').each(function(i){
    setTimeout(function(){
      $('ul li').addClass('animated');
    },400*i)
  })

但它不起作用,在这种情况下,4个项目被翻译在一起,而不是“超时”;奇怪的是,在我的实际站点中,代码似乎更坏,类根本没有添加。我检查了我站点的代码并再次拨弄,但我找不到问题所在。

您必须使用
来分别针对每个元素

  $('ul li').each(function(i){
    setTimeout(function(){
      $(this).addClass('animated');
    }.bind(this),400*i)
  });
或者您可以使用箭头功能来修复此问题

  $('ul li').each(function(i){
    setTimeout(()=>{
      $(this).addClass('animated');
    },400*i)
  });

您必须使用
分别针对每个元素

  $('ul li').each(function(i){
    setTimeout(function(){
      $(this).addClass('animated');
    }.bind(this),400*i)
  });
或者您可以使用箭头功能来修复此问题

  $('ul li').each(function(i){
    setTimeout(()=>{
      $(this).addClass('animated');
    },400*i)
  });

可以使用.each方法中的第二个参数来确定元素。比如:

$('.inOrder').click(function(){
  $('ul li').each(function(i, ele){
    setTimeout(function(){
      $(ele).addClass('animated');
    },400*i);
  })
})

您可以使用.each方法中的第二个参数来确定元素。比如:

$('.inOrder').click(function(){
  $('ul li').each(function(i, ele){
    setTimeout(function(){
      $(ele).addClass('animated');
    },400*i);
  })
})

EWW。。。这是一个输入错误,在我的实际站点中,我使用的是“this”,而不是“bind”,那么为什么
bind
?我想如果不是
bind
,当
$(this)调用addClass时,
this
可能是
窗口,对吗?@shenkwen Inside setTimeout,this将指向窗口。因此,如果您想针对外部上下文,必须使用bind或arrow函数。这不仅对你的问题有帮助,而且对很多情况都有帮助。+1对于箭头函数,我以前看到过,不理解也不知道如何搜索它。我把你的解决方案应用到我的网站上,效果很好。然而奇怪的是,菜单项的移动并不均匀,你知道为什么吗?@shenkwen它在这里工作得很好。在你的网站上,它也运行良好。我不明白你的问题。你能再解释一下吗。。。这是一个输入错误,在我的实际站点中,我使用的是“this”,而不是“bind”,那么为什么
bind
?我想如果不是
bind
,当
$(this)调用addClass时,
this
可能是
窗口,对吗?@shenkwen Inside setTimeout,this将指向窗口。因此,如果您想针对外部上下文,必须使用bind或arrow函数。这不仅对你的问题有帮助,而且对很多情况都有帮助。+1对于箭头函数,我以前看到过,不理解也不知道如何搜索它。我把你的解决方案应用到我的网站上,效果很好。然而奇怪的是,菜单项的移动并不均匀,你知道为什么吗?@shenkwen它在这里工作得很好。在你的网站上,它也运行良好。我不明白你的问题。你能再解释一下吗?