Categories
JavaScript Answers

How to scroll down with JavaScript?

Spread the love

Sometimes, we want to scroll down with JavaScript.

In this article, we’ll look at how to scroll down with JavaScript.

How to scroll down with JavaScript?

To scroll down with JavaScript, we can use the window.scrollBy method.

For instance, we write:

<div>
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras vulputate neque vel dolor lobortis, a rutrum turpis sollicitudin. Praesent faucibus imperdiet euismod. Donec vitae nisl pretium, elementum nibh ac, aliquam massa. Proin eleifend mi sit amet urna consectetur, vel convallis nunc dictum. Nulla elementum, eros in congue dignissim, mi lectus aliquet augue, sed sodales odio nunc vitae nibh. Nunc id enim purus. Cras sollicitudin rhoncus enim, non tincidunt leo pellentesque nec. Etiam tristique urna eget arcu euismod, et vestibulum sem imperdiet. Morbi ultricies laoreet purus at viverra. Sed tincidunt diam quis lorem hendrerit, vitae dapibus augue congue. Aliquam erat volutpat. Curabitur lacinia libero vel metus consequat congue. Praesent nec magna quis est egestas facilisis sed id turpis. Sed finibus nulla eget purus viverra viverra. Mauris ornare quam vitae sapien tempor, eget consectetur orci maximus.
</div>

to add some content.

Then we write:

window.scrollBy(0,180);

to scroll right by 0 pixels and down by 180 pixels.

Conclusion

To scroll down with JavaScript, we can use the window.scrollBy method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *