How to smoothly scroll the page to an element by its ID or class using JavaScript/jQuery?
Hello everyone! I'm a beginner in web development and I've encountered a task: on my page there is a long list, and at the bottom there is a "Top" button. When clicked, I need to smoothly (animated) scroll the page to the very top. But I can't get it to work.
Here is my code:
```html
```
```javascript
// Trying to scroll to the element with id="footer"
$('#goTop').click(function() {
$('#footer').scroll(); // Doesn't work
// Or like this:
window.scrollTo(0, $('#footer').offset().top); // Just jumps, no animation
});
```
I've tried different options, but nothing works. How to properly make a smooth scroll to an element? I need the page to scroll smoothly to the top when the button is clicked.
No answers yet.
Log in to leave an answer
Blogs