I’d like to make the browser to scroll the page to a given anchor, just by using JavaScript.

I have specified a name or id attribute in my HTML code:

 <a name="anchorName">..</a> 

or

 <h1>..</h1> 

I’d like to get the same effect as you’d get by navigating to . The page should be scrolled so that the anchor is near the top of the visible part of the page.

17 Answers

function scrollTo(hash) { location.hash = "#" + hash; } 

No jQuery required at all!

13

Way simpler:

var element_to_scroll_to = document.getElementById('anchorName2'); // Or: var element_to_scroll_to = document.querySelectorAll('.my-element-class')[0]; // Or: var element_to_scroll_to = $('.my-element-class')[0]; // Basically `element_to_scroll_to` just have to be a reference // to any DOM element present on the page // Then: element_to_scroll_to.scrollIntoView(); 
8

You can use jQuery's .animate(), .offset() and scrollTop. Like

$(document.body).animate({ 'scrollTop': $('#anchorName2').offset().top }, 2000); 

Example link:

If you don't want to animate, use .scrollTop() like:

$(document.body).scrollTop($('#anchorName2').offset().top); 

Or JavaScript's native location.hash like:

location.hash = '#' + anchorid; 
5

2018-2020 Pure JavaScript:

There is a very convenient way to scroll to the element:

el.scrollIntoView({ behavior: 'smooth', // smooth scroll block: 'start' // the upper border of the element will be aligned at the top of the visible part of the window of the scrollable area. }) 

But as far as I understand, it does not have such good support as the options below.

Enter image description here

Learn more about the method.


If it is necessary that the element is in the top:

const element = document.querySelector('#element') const topPos = element.getBoundingClientRect().top + window.pageYOffset window.scrollTo({ top: topPos, // scroll so that the element is at the top of the view behavior: 'smooth' // smooth scroll }) 

Demonstration example on CodePen


If you want the element to be in the center:

const element = document.querySelector('#element') const rect = element.getBoundingClientRect() // get rects(width, height, top, etc) const viewHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0); window.scroll({ top: rect.top + rect.height / 2 - viewHeight / 2, behavior: 'smooth' // smooth scroll }); 

Demonstration example on CodePen


Support:

введите сюда описание изображения

They write that scroll is the same method as scrollTo, but support shows better in scrollTo.

More about the method.

3

Great solution by jAndy, but the smooth scroll seems to be having issues working in Firefox.

Writing it this way works in Firefox as well.

(function($) { $(document).ready(function() { $('html, body').animate({ 'scrollTop': $('#anchorName2').offset().top }, 2000); }); })(jQuery); 
2

Here is a pure JavaScript solution without jQuery. It was tested on Chrome and Internet Explorer, but not tested on iOS.

function ScrollTo(name) { ScrollToResolver(document.getElementById(name)); } function ScrollToResolver(elem) { var jump = parseInt(elem.getBoundingClientRect().top * .2); document.body.scrollTop += jump; document.documentElement.scrollTop += jump; if (!elem.lastjump || elem.lastjump > Math.abs(jump)) { elem.lastjump = Math.abs(jump); setTimeout(function() { ScrollToResolver(elem);}, "100"); } else { elem.lastjump = null; } } 

Demo:

6

In 2018, you don't need jQuery for something simple like this. The built in scrollIntoView() method supports a "behavior" property to smoothly scroll to any element on the page. You can even update the browser URL with a hash to make it bookmarkable.

From this tutorial on scrolling HTML Bookmarks, here is a native way to add smooth scrolling to all anchor links on your page automatically:

let anchorlinks = document.querySelectorAll('a[href^="#"]')   for (let item of anchorlinks) { // relitere     item.addEventListener('click', (e)=> {         let hashval = item.getAttribute('href')         let target = document.querySelector(hashval)         target.scrollIntoView({             behavior: 'smooth',             block: 'start'         })         history.pushState(null, null, hashval)         e.preventDefault()     }) } 
3

The easiest way to to make the browser to scroll the page to a given anchor is to add *{scroll-behavior: smooth;} in your style.css file and in your HTML navigation use #NameOfTheSection.

*{scroll-behavior: smooth;}
<a href="#scroll-to">Click to Scroll<a/> <p>other sections</p> <p>other sections</p> <p>other sections</p> <p>other sections</p> <p>other sections</p> <p>other sections</p> <p>other sections</p> <p>other sections</p> <p>other sections</p> <p>other sections</p> <p>other sections</p> <p>other sections</p> <p>other sections</p> <section> <p>it will scroll down to this section</p> </section>
1

Smoothly scroll to the proper position

Get correct y coordinate and use window.scrollTo({top: y, behavior: 'smooth'})

const id = 'anchorName2'; const yourElement = document.getElementById(id); const y = yourElement.getBoundingClientRect().top + window.pageYOffset; window.scrollTo({top: y, behavior: 'smooth'}); 
1
$(document).ready -> $("a[href^='#']").click -> $(document.body).animate scrollTop: $($(this).attr("href")).offset().top, 1000 
1

The solution from CSS-Tricks no longer works in jQuery 2.2.0. It will throw a selector error:

JavaScript runtime error: Syntax error, unrecognized expression: a[href*=#]:not([href=#])

I fixed it by changing the selector. The full snippet is this:

$(function() { $("a[href*='#']:not([href='#'])").click(function() { if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) { var target = $(this.hash); target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); if (target.length) { $('html,body').animate({ scrollTop: target.offset().top }, 1000); return false; } } }); }); 

This works:

$('.scroll').on("click", function(e) { e.preventDefault(); var dest = $(this).attr("href"); $("html, body").animate({ 'scrollTop': $(dest).offset().top }, 2000); }); 

Just add the class 'scroll' to any links you wish to animate

1

Most answers are unnecessarily complicated.

If you just want to jump to the target element, you don't need JavaScript:

# the link: <a href="#target">Click here to jump.</a> # target element: <div>Any kind of element.</div> 

If you want to scroll to the target animatedly, please refer to 5hahiL's answer.

2
jQuery("a[href^='#']").click(function(){ jQuery('html, body').animate({ scrollTop: jQuery( jQuery(this).attr('href') ).offset().top }, 1000); return false; });

This is a working script that will scroll the page to the anchor. To set it up, just give the anchor link an id that matches the name attribute of the anchor that you want to scroll to.

<script> jQuery(document).ready(function ($){ $('a').click(function (){ var id = $(this).attr('id'); console.log(id); if ( id == 'cet' || id == 'protein' ) { $('html, body').animate({ scrollTop: $('[name="' + id + '"]').offset().top}, 'slow'); } }); }); </script> 

I found an easy and simple jQuery solution on CSS-Tricks. That's the one I'm using now.

$(function() { $('a[href*=#]:not([href=#])').click(function() { if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) { var target = $(this.hash); target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); if (target.length) { $('html,body').animate({ scrollTop: target.offset().top }, 1000); return false; } } }); }); 
1

A Vue.js 2 solution ... add a simple data property to simply force the update:

 const app = new Vue({ ... , updated: function() { this.$nextTick(function() { var uri = window.location.href var anchor = ( uri.indexOf('#') === -1 ) ? '' : uri.split('#')[1] if ( String(anchor).length > 0 && this.updater === 'page_load' ) { this.updater = "" // only on page-load ! location.href = "#"+String(anchor) } }) } }); app.updater = "page_load" /* Smooth scrolling in CSS - it works in HTML5 only */ html, body { scroll-behavior: smooth; }