没有用于scrollToElement的用户交互

时间:2017-04-20 16:09:03

标签: javascript jquery html

我希望创建一个网页,在不需要任何用户交互的情况下,从一个元素到下一个元素的连续循环滚动。我已经搜索过了,看起来我正在寻找的是scrollToElement(实际上this answer on another post中的小提琴类似,但确实有用户交互)。不幸的是,一切似乎都是通过点击或不循环来完成的。我对JS很新,但我觉得它应该是可行的。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:2)

如何创建一个调用scrollToElement然后在setInterval()中将其设置为CB函数的函数。阅读更多:MDN

答案 1 :(得分:1)

以下是jQuery使用setInterval()$.animate()滚动到各个部分的快速示例。

var $sections = $('section'),
    count = 1,
    speed = 250,
    delay = 2000;

var interval = setInterval(function() {
  $('html, body').animate({
    scrollTop: $sections.eq(count).offset().top
  }, speed);
  count = (count + 1) % $sections.length;
}, delay)
section {
  height: 200vh;
  background: red;
}
section:nth-child(2) {
  background: blue;
}
section:nth-child(3) {
  background: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section></section>
<section></section>
<section></section>