In my page footer I have a "Back to Top" button which instantly jumps to the top of the page. How can I make it so it scrolls fast to the top instead?
Asked
Active
Viewed 94 times
0
-
1https://stackoverflow.com/questions/1144805/scroll-to-the-top-of-the-page-using-javascript-jquery – Jonathan Nicol Nov 19 '17 at 20:04
-
Have you googled this? Many answers already there, you just need to search for them: https://stackoverflow.com/questions/10744299/scroll-back-to-the-top-of-div – CrazyTim Nov 19 '17 at 21:41
1 Answers
-1
you can add these javascript functions:
function ScrollTo(name) {
ScrollToResolver(document.getElementById(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;
}
}
and than on the button add this function call:
<button onclick="ScrollTo('top')">Back to Top</button>
where the very forst element of the content has class "top"

1GR3
- 349
- 2
- 12