[javascript] jQuery scroll to element

This is the way I do it.

document.querySelector('scrollHere').scrollIntoView({ behavior: 'smooth' })

Works in any browser.

It can easily be wrapped into a function

function scrollTo(selector) {
    document.querySelector(selector).scrollIntoView({ behavior: 'smooth' })
}

Here is a working example

_x000D_
_x000D_
$(".btn").click(function() {_x000D_
  document.getElementById("scrollHere").scrollIntoView( {behavior: "smooth" })_x000D_
})
_x000D_
.btn {margin-bottom: 500px;}_x000D_
.middle {display: block; margin-bottom: 500px; color: red;}
_x000D_
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>_x000D_
_x000D_
<button class="btn">Scroll down</button>_x000D_
_x000D_
<h1 class="middle">You see?</h1>_x000D_
_x000D_
<div id="scrollHere">Arrived at your destination</div>
_x000D_
_x000D_
_x000D_

Docs

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