简体   繁体   English

如何使用if语句启动/停止和重置setInterval动画?

[英]How do I start/stop and reset a setInterval animation with an if statement?

I would like to be able to "play" an infinite/looping vertical scroll animation if the user scrolls past a certain distance. 如果用户滚动超过一定距离,我希望能够“播放”无限/循环的垂直滚动动画。 I would then like to stop this animation and reset the state, to the original/starting position, if the user scrolls back up past that distance. 然后,如果用户向后滚动超过该距离,我想停止该动画并将状态重置为原始/开始位置。

I have a feeling it could be achieved with clearInterval , but can't quite get my head around it. 我感觉可以使用clearInterval来实现,但是不能完全理解。

Here is my current code: HTML 这是我当前的代码:HTML

<div id="list">

  <span>Item 1</span>
  <span>Item 2</span>
  <span>Item 3</span>
  <span>Item 4</span>
  <span>Item 5</span>

</div> <!-- /.list -->

jQuery jQuery的

const startCycle = () => {

  $('#list').stop().animate({ scrollTop: `${amount}px` }, 400, 'swing', function() {

    // update the order of the elements
    $(this).scrollTop(0).find('span:last').after($('span:first', this))

  })

}

const playAnimation = () => {

  const pixels = window.pageYOffset
  console.log("pixels:", pixels)

  if (pixels >= 30) {
    setInterval(startCycle, 1000)
  } else {
    clearInterval(startCycle)
  }

}

document.addEventListener("scroll", playAnimation)

Here is a link to view the pen: https://codepen.io/anon/pen/vMoJJe 这是查看笔的链接: https : //codepen.io/anon/pen/vMoJJe

Unfortunately, the current animation appears choppy or gets stuck with the above if statement, and then fails to stop with scroll up. 不幸的是,当前动画显得不稳定或卡在上面的if语句中,然后无法停止并向上滚动。 The aim is to stop the animation and return it to its starting position, with the first span on display if the user is above a certain distance. 目的是停止动画并将其返回到其起始位置,如果用户超过一定距离,则显示第一个span


Initial code credit: Infinite vertical scroll div element 初始代码信用: 无限垂直滚动div元素

To clear an interval you have to save the return value of setInterval 要清除间隔,您必须保存setInterval的返回值

const playAnimation = () => {

  const pixels = window.pageYOffset
  console.log("pixels:", pixels)

  let interval // declare the variable
  if (pixels >= 30) {
    interval = setInterval(startCycle, 1000) // save the interval ID
  } else {
    clearInterval(interval) // clear the interval ID
  }
}

The issue you have is that you are setting the interval more than once. 您遇到的问题是您要不止一次设置间隔。 Each time the scroll event is fired, an interval is started. 每次触发scroll事件时,都会启动一个间隔。

So, adding a boolean to make sure only one setInterval() is called at a time should fix your issue. 因此,添加一个布尔值以确保一次仅调用一个setInterval()应该可以解决您的问题。

Edit: also, as Alex Wayne mentioned, you should save the interval in a variable in order to clear it later on. 编辑:同样,正如Alex Wayne提到的那样,您应该将间隔保存在变量中,以便以后清除它。

Eg: 例如:

var interval;
var isIntervalSet = false;
const playAnimation = () => {

  const pixels = window.pageYOffset
  console.log("pixels:", pixels)

  if (pixels >= 30 && !isIntervalSet) {
    interval = setInterval(startCycle, 1000)
    isIntervalSet = true;
  } else {
    clearInterval(interval);
    isIntervalSet = false;
  }

}

document.addEventListener("scroll", playAnimation)

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

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