Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

smooth auto scroll by using javascript

I am trying to implement some code on my web page to auto-scroll after loading the page. I used a Javascript function to perform auto-scrolling, and I called my function when the page loads, but the page is still not scrolling smoothly! Is there any way to auto scroll my page smoothly?

Here is my Javascript function:

function pageScroll() {
        window.scrollBy(0,50); // horizontal and vertical scroll increments
        scrolldelay = setTimeout('pageScroll()',100); // scrolls every 100 milliseconds
}
like image 996
hiteshtr Avatar asked Mar 23 '12 10:03

hiteshtr


People also ask

How do I smooth a scrollTo an element?

Scrolling to an element can be achieved in Javascript using the scrollIntoView() method. Smooth animation and customizing the alignment can be set through the scrollIntoViewOptions parameter.

How do I automatically scroll my website?

To use you just need to press CTRL+ Left click of your mouse and drag the mouse a bit in the direction you want to scroll the page. For example, if you want to scroll up to the page automatically, click CTRL+ left click and slightly move your mouse upwards, the tool will start scrolling up the page.

How do I create an automatic scroll in HTML?

The first one is with javascript: set the scrollTop property of the scrollable element (e.g. document. body. scrollTop = 1000; ). The second is setting the link to point to a specific id in the page e.g.


2 Answers

It's not smooth because you've got the scroll incrementing by 50 every 100 milliseconds.

change this and the amount you are scrolling by to a smaller number to have the function run with the illusion of being much more 'smooth'.

turn down the speed amount to make this faster or slower.

function pageScroll() {
    window.scrollBy(0,1);
    scrolldelay = setTimeout(pageScroll,10);
}

will appear to be much smoother, try it ;)

like image 63
Michael Zaporozhets Avatar answered Oct 21 '22 10:10

Michael Zaporozhets


Try to use jQuery, and this code:

$(document).ready(function(){
     $('body,html').animate({scrollTop: 156}, 800); 
});

156 - position scroll to (px), from top of page.
800 - scroll duration (ms)

like image 43
Antonix Avatar answered Oct 21 '22 09:10

Antonix