0

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> ? 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>
James
  • 1
  • 1

1 Answers1

0

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.

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 )

stopScroll() function was merely added and the function call pageScroll() was just invoked as a link. This was demonstrated in this Article.

Community
  • 1
  • 1
James
  • 1
  • 1