简体   繁体   English

jQuery的自动滚动网站暂停

[英]jquery auto scroll website with pause

How can I set jQuery auto scroll web page and with pause / stop for a specific px and continue auto scrolling? 如何设置jQuery自动滚动网页并为特定的px设置暂停/停止并继续自动滚动? It's something like a user scrolling on the web page reading an article, like scroll and stop and continue scrolling something like that. 这就像用户在网页上滚动以阅读文章一样,例如滚动并停止并继续滚动类似的内容。 I can't seem to find a good example on the internet and all I got the answer from searching is only jQuery auto scroll example only. 我似乎无法在互联网上找到一个很好的示例,而我从搜索中得到的答案仅仅是jQuery自动滚动示例而已。

If you can't understand my question it's something looks like this: Example from codepen 如果您听不懂我的问题,它看起来像这样: 来自Codepen的示例

Here is my code: 这是我的代码:

$("html, body").animate({ scrollTop: $(document).height() }, 1000);

setTimeout(function() {
$('html, body').animate({scrollTop:0}, 1000); // 1000 is the duration of the animation
},500);

setInterval(function(){

$("html, body").animate({ scrollTop: $(document).height() }, 500); //  Speed from Bottom to top

setTimeout(function() {
$('html, body').animate({scrollTop:0}, 5000); // Speed from Top to bottom
},500); // What is this speed refer to?

},1000); // What is this speed refer to?

By the way, I am new in jQuery, do you mind explain a little bit to me what is the meaning of both of the 500 and 1000 second meaning? 顺便说一句,我是jQuery的新手,您介意向我解释一下500秒和1000秒的含义是什么吗? I know it refers to second but what is the meaning of adding 2 of it? 我知道它是指第二个,但是加上2个是什么意思? Thanks! 谢谢!

Here is an working example 这是一个有效的例子

 setInterval(function scroll() { $("section").each(function(i, e) { $("html, body").animate({ scrollTop: $(e).offset().top }, 500).delay(500); // First value is a speed of scroll, and second time break }); setTimeout(function() { $('html, body').animate({ scrollTop: 0 }, 5000); // This is the speed of scroll up }, 4000); //This means after what time should it begin (after scrolling ends) return scroll; }(), 9000); //This value means after what time should the function be triggered again //(It should be sum of all animation time and delay) 9000 = 5000 + 4000 
 main { background: #EEE; } main section { background: #DDD; width: 90%; margin: 30px auto; padding: 10px 15px; min-height: 1000px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <main> <section> </section> <section> </section> <section> </section> <section> </section> </main> 

EDIT 编辑

I edited a little bit snippet so that the code was not twice. 我编辑了一些代码段,以使代码不会重复两次。 I declare function ( scroll() ) and use it inside interval. 我声明了函数( scroll() )并在interval内部使用它。 Thanks to that there is no need for the same code at the begining. 因此,一开始就不需要相同的代码。

EDIT2 EDIT2

If you want the scroll to stop depending on px and not section just change this: 如果您希望滚动停止取决于px而不是部分,则只需更改以下内容即可:

setInterval(function scroll() {
  $("section").each(function(i, e) {
    $("html, body").animate({
      scrollTop: $(e).offset().top
    }, 500).delay(500); // First value is a speed of scroll, and second time break
  });
  ...

To this: 对此:

setInterval(function scroll() {
  for (var i = 0; i < 4000; i += 800) {
    $("html, body").animate({
      scrollTop: i
    }, 500).delay(500);
  }
  ...

EDIT3 EDIT3

If you want to scroll to bottom at the end, you can do it like this: 如果您想滚动到底部的底部,可以这样做:

setInterval(function scroll() {
  for (var i = 0; i < 4000; i += 800) {
    $("html, body").animate({
      scrollTop: i
    }, 500).delay(500);
    if (i + 800 >= 4000) {
      $("html, body").animate({
        scrollTop: $(document).height()
      }, 500).delay(500);
    }
  }
  ...

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

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