[javascript] Smooth scrolling when clicking an anchor link

There is a css way of doing this using scroll-behavior. Add the following property.

    scroll-behavior: smooth;

And that is it. No JS required.

_x000D_
_x000D_
a {_x000D_
  display: inline-block;_x000D_
  width: 50px;_x000D_
  text-decoration: none;_x000D_
}_x000D_
nav, scroll-container {_x000D_
  display: block;_x000D_
  margin: 0 auto;_x000D_
  text-align: center;_x000D_
}_x000D_
nav {_x000D_
  width: 339px;_x000D_
  padding: 5px;_x000D_
  border: 1px solid black;_x000D_
}_x000D_
scroll-container {_x000D_
  display: block;_x000D_
  width: 350px;_x000D_
  height: 200px;_x000D_
  overflow-y: scroll;_x000D_
  scroll-behavior: smooth;_x000D_
}_x000D_
scroll-page {_x000D_
  display: flex;_x000D_
  align-items: center;_x000D_
  justify-content: center;_x000D_
  height: 100%;_x000D_
  font-size: 5em;_x000D_
}
_x000D_
<nav>_x000D_
  <a href="#page-1">1</a>_x000D_
  <a href="#page-2">2</a>_x000D_
  <a href="#page-3">3</a>_x000D_
</nav>_x000D_
<scroll-container>_x000D_
  <scroll-page id="page-1">1</scroll-page>_x000D_
  <scroll-page id="page-2">2</scroll-page>_x000D_
  <scroll-page id="page-3">3</scroll-page>_x000D_
</scroll-container>
_x000D_
_x000D_
_x000D_

PS: please check the browser compatibility.

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to jquery

How to make a variable accessible outside a function? Jquery assiging class to th in a table Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Getting all files in directory with ajax Bootstrap 4 multiselect dropdown Cross-Origin Read Blocking (CORB) bootstrap 4 file input doesn't show the file name Jquery AJAX: No 'Access-Control-Allow-Origin' header is present on the requested resource how to remove json object key and value.?

Examples related to scroll

How to window.scrollTo() with a smooth effect Angular 2 Scroll to bottom (Chat style) Scroll to the top of the page after render in react.js Get div's offsetTop positions in React RecyclerView - How to smooth scroll to top of item on a certain position? Detecting scroll direction How to disable RecyclerView scrolling? How can I scroll a div to be visible in ReactJS? Make a nav bar stick Disable Scrolling on Body Wrapping a react-router Link in an html button How to make a hyperlink in telegram without using bots? React onClick and preventDefault() link refresh/redirect? How to put a link on a button with bootstrap? How link to any local file with markdown syntax? link with target="_blank" does not open in new tab in Chrome How to create a link to another PHP page How to determine the current language of a wordpress page when using polylang? How to change link color (Bootstrap) How can I make a clickable link in an NSAttributedString?

Examples related to anchor

How to open a link in new tab using angular? Attaching click to anchor tag in angular How to create a link to another PHP page Making href (anchor tag) request POST instead of GET? Difference between _self, _top, and _parent in the anchor tag target attribute How can I create a link to a local file on a locally-run web page? How to open link in new tab on html? Getting a link to go to a specific section on another page Pure CSS scroll animation Make anchor link go some pixels above where it's linked to