Skip to main content
Use return false rather than e.preventDefault()
Source Link
Jonathan
  • 11.3k
  • 9
  • 67
  • 82

With this solution you do not need any plugin and there's no setup required besides placing the script before your closing </body> tag.

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

if ($(window.location.hash).length > 1) {
  $("html, body").animate({
    scrollTop: $(window.location.hash).offset().top
  }, 1000);
}

On load, if there is a hash in the address, we scroll to it.

And - whenever you click an a link with an href hash e.g. #top, we scroll to it.

##Edit 2020

If you want a pure JavaScript solution: you could perhaps instead use something like:

var _scrollToElement = function (selector) {
  try {
    document.querySelector(selector).scrollIntoView({ behavior: 'smooth' });
  } catch (e) {
    console.warn(e);
  }
}

var _scrollToHashesInHrefs = function () {
  document.querySelectorAll("a[href^='#']").forEach(function (el) {
    el.addEventListener('click', function (e) {
      e_scrollToElement(el.preventDefaultgetAttribute('href'));
      _scrollToElement(el.getAttribute('href'));return false;
    })
  })
  if (window.location.hash) {
    _scrollToElement(window.location.hash);
  }
}

_scrollToHashesInHrefs();

With this solution you do not need any plugin and there's no setup required besides placing the script before your closing </body> tag.

$("a[href^='#']").on("click", function(e) {
  e.preventDefault();
  $("html, body").animate({
    scrollTop: $($(this).attr("href")).offset().top
  }, 1000);
});

if ($(window.location.hash).length > 1) {
  $("html, body").animate({
    scrollTop: $(window.location.hash).offset().top
  }, 1000);
}

On load, if there is a hash in the address, we scroll to it.

And - whenever you click an a link with an href hash e.g. #top, we scroll to it.

##Edit 2020

If you want a pure JavaScript solution: you could perhaps instead use something like:

var _scrollToElement = function (selector) {
  try {
    document.querySelector(selector).scrollIntoView({ behavior: 'smooth' });
  } catch (e) {
    console.warn(e);
  }
}

var _scrollToHashesInHrefs = function () {
  document.querySelectorAll("a[href^='#']").forEach(function (el) {
    el.addEventListener('click', function (e) {
      e.preventDefault();
      _scrollToElement(el.getAttribute('href'));
    })
  })
  if (window.location.hash) {
    _scrollToElement(window.location.hash);
  }
}

_scrollToHashesInHrefs();

With this solution you do not need any plugin and there's no setup required besides placing the script before your closing </body> tag.

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

if ($(window.location.hash).length > 1) {
  $("html, body").animate({
    scrollTop: $(window.location.hash).offset().top
  }, 1000);
}

On load, if there is a hash in the address, we scroll to it.

And - whenever you click an a link with an href hash e.g. #top, we scroll to it.

##Edit 2020

If you want a pure JavaScript solution: you could perhaps instead use something like:

var _scrollToElement = function (selector) {
  try {
    document.querySelector(selector).scrollIntoView({ behavior: 'smooth' });
  } catch (e) {
    console.warn(e);
  }
}

var _scrollToHashesInHrefs = function () {
  document.querySelectorAll("a[href^='#']").forEach(function (el) {
    el.addEventListener('click', function (e) {
      _scrollToElement(el.getAttribute('href'));
      return false;
    })
  })
  if (window.location.hash) {
    _scrollToElement(window.location.hash);
  }
}

_scrollToHashesInHrefs();
Add pure JS solution
Source Link
Jonathan
  • 11.3k
  • 9
  • 67
  • 82

With this solution you do not need any plugin and there's no setup required besides placing the script before your closing </body> tag.

$("a[href^='#']").on("click", function(e) {
  e.preventDefault();
  $("html, body").animate({
    scrollTop: $($(this).attr("href")).offset().top
  }, 1000);
});

if ($(window.location.hash).length > 1) {
  $("html, body").animate({
    scrollTop: $(window.location.hash).offset().top
  }, 1000);
}

On load, if there is a hash in the address, we scroll to it.

And - whenever you click an a link with an href hash e.g. #top, we scroll to it.

##Edit 2020

If you want a pure JavaScript solution: you could perhaps instead use something like:

var _scrollToElement = function (selector) {
  try {
    document.querySelector(selector).scrollIntoView({ behavior: 'smooth' });
  } catch (e) {
    console.warn(e);
  }
}

var _scrollToHashesInHrefs = function () {
  document.querySelectorAll("a[href^='#']").forEach(function (el) {
    el.addEventListener('click', function (e) {
      e.preventDefault();
      _scrollToElement(el.getAttribute('href'));
    })
  })
  if (window.location.hash) {
    _scrollToElement(window.location.hash);
  }
}

_scrollToHashesInHrefs();

With this solution you do not need any plugin and there's no setup required besides placing the script before your closing </body> tag.

$("a[href^='#']").on("click", function(e) {
  e.preventDefault();
  $("html, body").animate({
    scrollTop: $($(this).attr("href")).offset().top
  }, 1000);
});

if ($(window.location.hash).length > 1) {
  $("html, body").animate({
    scrollTop: $(window.location.hash).offset().top
  }, 1000);
}

On load, if there is a hash in the address, we scroll to it.

And - whenever you click an a link with an href hash e.g. #top, we scroll to it.

With this solution you do not need any plugin and there's no setup required besides placing the script before your closing </body> tag.

$("a[href^='#']").on("click", function(e) {
  e.preventDefault();
  $("html, body").animate({
    scrollTop: $($(this).attr("href")).offset().top
  }, 1000);
});

if ($(window.location.hash).length > 1) {
  $("html, body").animate({
    scrollTop: $(window.location.hash).offset().top
  }, 1000);
}

On load, if there is a hash in the address, we scroll to it.

And - whenever you click an a link with an href hash e.g. #top, we scroll to it.

##Edit 2020

If you want a pure JavaScript solution: you could perhaps instead use something like:

var _scrollToElement = function (selector) {
  try {
    document.querySelector(selector).scrollIntoView({ behavior: 'smooth' });
  } catch (e) {
    console.warn(e);
  }
}

var _scrollToHashesInHrefs = function () {
  document.querySelectorAll("a[href^='#']").forEach(function (el) {
    el.addEventListener('click', function (e) {
      e.preventDefault();
      _scrollToElement(el.getAttribute('href'));
    })
  })
  if (window.location.hash) {
    _scrollToElement(window.location.hash);
  }
}

_scrollToHashesInHrefs();
added 27 characters in body
Source Link
Jonathan
  • 11.3k
  • 9
  • 67
  • 82

With this solution you do not need any plugin and there's no setup required besides placing the script before your closing </body> tag.

$("a[href^='#']").on("click", function(e) {
  e.preventDefault();
  $("html, body").animate({
    scrollTop: $($(this).attr("href")).offset().top
  }, 1000);
});

if ($(window.location.hash).length > 1) {
  $("html, body").animate({
    scrollTop: $(window.location.hash).offset().top
  }, 1000);
}

On load, if there is a hash in the address, we scroll to it.

And - whenever you click an a link with an href hash e.g. #top, we scroll to it.

With this solution you do not need any plugin and there's no setup required besides placing the script before your closing </body> tag.

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

if ($(window.location.hash).length > 1) {
  $("html, body").animate({
    scrollTop: $(window.location.hash).offset().top
  }, 1000);
}

On load, if there is a hash in the address, we scroll to it.

And - whenever you click an a link with an href hash e.g. #top, we scroll to it.

With this solution you do not need any plugin and there's no setup required besides placing the script before your closing </body> tag.

$("a[href^='#']").on("click", function(e) {
  e.preventDefault();
  $("html, body").animate({
    scrollTop: $($(this).attr("href")).offset().top
  }, 1000);
});

if ($(window.location.hash).length > 1) {
  $("html, body").animate({
    scrollTop: $(window.location.hash).offset().top
  }, 1000);
}

On load, if there is a hash in the address, we scroll to it.

And - whenever you click an a link with an href hash e.g. #top, we scroll to it.

edited body
Source Link
Jonathan
  • 11.3k
  • 9
  • 67
  • 82
Loading
Source Link
Jonathan
  • 11.3k
  • 9
  • 67
  • 82
Loading