Languages
[Edit]
EN

JavaScript - scroll to left of element

0 points
Created by:
Jacob
532

In this article, we would like to show you how to scroll to the left of an element using JavaScript.

Quick solution:

Note:

Go to this article if you're looking for a solution that works in both modern and older browsers.

 

1. Using scrollLeft property

Edit

In this example, we set scrollLeft property to 0 to scroll to the left of the element on the button click event.

2. Using scroll() / scrollTo() method

Edit

In this example, we use scroll() / scrollTo() methods to scroll to the left of the element on the button click event.

Note:

The scroll() / scrollTo() methods appeared in major web browsers around 2015-2020.

3. Smooth scroll behavior

Edit

In this example, we use scroll() / scrollTo() methods with smooth behavior to scroll to the left of the element on the button click event.

Note:

The scroll() / scrollTo() methods appeared in major web browsers around 2015-2020.

 

References

Edit
  1. Element.scrollLeft - Web APIs | MDN
  2. Element.scroll() - Web APIs | MDN
  3. Element.scrollTo() - Web APIs | MDN

Alternative titles

  1. JavaScript - scroll to left of element on click event
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join