Matthew C.
—You want to scroll to an element when a certain event occurs. A common use case is when you want to add a “scroll back to top” button to your website. How do you do this using jQuery?
Let’s say that you have a button with an id
of “scrollBtn” that you want to use to scroll to the header with an id
of “header1” when the button is clicked. Using jQuery, you can get the button and header element, attach a “click” event listener to the button and then use the jQuery animate()
method to scroll the page to the header when the button is clicked:
import $ from 'jquery'; const scrollToBtn = $('#scrollBtn'); const scrollToEl = $('#header1'); scrollToBtn.click(() => { $('html').animate( { scrollTop: scrollToEl.offset().top, }, 800 //speed ); });
The animate()
method takes in four arguments: properties
, duration
, easing
, and complete
. The properties
argument is the only required argument. It’s an object of CSS properties, as well as some non-CSS style properties such as scrollTop
and scrollLeft
, as well as custom properties, and their values that will be animated. The animate()
method will change these properties and animate the transition. The duration
argument is a string or number that determines how long the animation will run in milliseconds. The easing
argument is a string that determines the easing function to use for the animation. The default easing is “swing”, which has a slower transition at the start and end of the animation than in the middle of the animation. The other easing is “linear”, which has a consistent transition speed. You can use other transitions using jQuery plugins such as jQuery UI.
The scrollTop
property of the HTML element is used to set the number of pixels that the page is scrolled vertically. We call the jQuery offSet()
method on the header to get its top
property. This is the distance between the header and the top of the page. This is where we scroll to.
You can also scroll to an element using vanilla JavaScript:
const scrollToBtn = document.getElementById('scrollBtn'); const scrollToEl = document.getElementById('header1'); scrollToBtn.addEventListener('click', () => { scrollToEl.scrollIntoView({ behavior: 'smooth', }); });
The easy-to-read scrollIntoView()
method is used to scroll to an element so that it’s visible. It has an optional argument that can be a boolean or an object. The optional alignTop
argument is a boolean. If set to true
, the top of the element to scroll to will be aligned with the top of the visible area of the scrollable ancestor. This is the default value. If set to false
, the bottom of the element to scroll to will be aligned to the bottom of the visible area of the scrollable ancestor.
The optional scrollIntoViewOptions
argument is an object with the following properties: behavior
, block
, and inline
. The behavior
property defines the transition animation. The value can be auto
or smooth
. The default value is auto
.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.