Scroll to the top of the page using JavaScript?
// Get The Id
var topPage = document.getElementById(`top-page`)
// On Click, Scroll to the Top of Page
topPage.onclick = () => window.scrollTo({ top: 0, behavior: 'smooth' })
// On scroll, Show/Hide the button
window.onscroll = () => {
window.scrollY > 500
? (topPage.style.display = 'block')
: (topPage.style.display = 'none')
}
// CSS
body {
background-color: #111;
height:5000px;
}
#top-page {
width: 50px;
position: fixed;
right: 30px;
bottom: 30px;
cursor: pointer;
color: white;
display: none;
}
// HTML
<img id="top-page" src="https://svgshare.com/i/LW3.sv" alt="Top">window.scroll({top: 0, left: 0});
//Or with jQuery
$('html,body').scrollTop(0);
//with animation
$('html, body').animate({ scrollTop: 0 }, 'fast');window.scrollTo(x-coord, y-coord);
Also in JavaScript:
- javascript keydown
- rite a function that takes in an array of numbers and outputs the maximum number.
- how to compare elements in an array
- local storage remove multiple items
- jquery run after page load
- javascript open url
- how to remove first element of array javascript
- javascript delete cookie
- get value onChange from mat-select angular
- webslesson search bar ajax
- empty array length javascript
- javascript hex to rgb
- how to go to next line in javascript
- not equal to in js
- if i pass an object to a function is it the same object javascript
- vibrate javascript
- What is the correct JavaScript syntax for opening a new window called "w2" ?
- javascript keywords
- js test if array
- .filter js
- javascript recursion example
- adonis column alter
- build angular project
- js scroll to id