简体   繁体   English

自动滚动启用和禁用链接

[英]Auto Scroll enable and disable link

How do I modify the following script to be able to stop and enable auto-scrolling using a simple text link like this html link <a href="javascript:stopScroll()">Stop Scrolling</a> ? 如何修改以下脚本,以便能够使用简单的文本链接(例如html链接<a href="javascript:stopScroll()">Stop Scrolling</a>停止并启用自动<a href="javascript:stopScroll()">Stop Scrolling</a> Thanks. 谢谢。

<script>
(function() {
   'use strict';

   var dbh,sto,num=3,temp=0,scrolldelay=70;

function init(){ 
    dbh=document.body.offsetHeight;
    pageScroll();
 }

function pageScroll() {
    window.scrollBy(0,num);
    temp+=num;
if((temp>=dbh)||(temp<=0)){
    num=-num;
 }
   sto=setTimeout(function(){pageScroll();},scrolldelay);
 }
   window.addEventListener?
   window.addEventListener('load',init,false):
   window.attachEvent('onload',init);
})();
</script>

The script I finally got to work is a little different from my original post. 我终于开始使用的脚本与我的原始帖子有些不同。 This seems to work but not as fluid in Google Chrome for some reason. 这似乎可行,但由于某些原因在Google Chrome中不那么流畅。

Here's what I finally put together and there are people to thank here as well... 这是我最终汇总的内容,这里还有很多人要感谢...

<script>
var xMax, yMax, xNeg=1, yNeg=1;

function pageScroll() {
    window.scrollBy(1 * xNeg, 1 * yNeg);
    if(xMax == window.scrollX)xNeg = xNeg * -1;
    if(yMax == window.scrollY)yNeg = xNeg * -1;
    scrolldelay = setTimeout(pageScroll,1);
    console.log(window.scrollY);
    xMax = window.scrollX;
    yMax = window.scrollY;
}
function stopScroll() {
        clearTimeout(scrolldelay);
}
</script>

Now, for the body section... 现在,对于身体部分...

<a href="javascript:stopScroll()">Stop or Slow Auto Scroll.</a><br>
<a href="javascript:pageScroll()">Start or Speed Up Auto Scroll.</a>

The main script came from another article here at stackoverflow, SajithNair ( This Article Here ) 主要的脚本来自另一篇文章在这里计算器,SajithNair( 该文章在这里

stopScroll() function was merely added and the function call pageScroll() was just invoked as a link. 仅添加了stopScroll()函数,并且仅将函数调用pageScroll()作为链接调用。 This was demonstrated in this Article. 这在本文中得到了证明

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

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