How to scroll HTML page to given anchor?

How to scroll HTML page to given anchor?

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:



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.


Solution 1:

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

No jQuery required at all!

Solution 2:

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:

Solution 3:

You can use jQuerys .animate(), .offset() and scrollTop. Like

    'scrollTop':   $('#anchorName2').offset().top
}, 2000);

example link:

If you don’t want to animate use .scrollTop() like


or javascripts native location.hash like

location.hash = '#' + anchorid;

Solution 4:

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

Related:  Angular2 5 minute install bug - require is not defined

Writing it this way works in Firefox as well.

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

Solution 5:

A pure javascript solution without JQuery. Tested on Chrome & I.e, not tested on IOS

function ScrollTo(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;


Solution 6:

2018 Pure js:

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

  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 he 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 top = element.getBoundingClientRect().top

  top, // scroll so that the element is at the top of the view
  behavior: 'smooth' // smooth scroll

Demonstration example on Codepen

Related:  Convert XML to JSON (and back) using Javascript

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);

  top: + rect.height / 2 - viewHeight / 2,
  behavior: 'smooth' // smooth scroll

Demonstration example on Codepen


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

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

More about the method