Home  >  Article  >  Web Front-end  >  js hide and show back to top button and window.onscroll event application_javascript skills

js hide and show back to top button and window.onscroll event application_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:42:471057browse

Now most websites will add this function: when the scroll bar scrolls to the bottom of the page, a "back to top" button or link will be displayed in the lower right corner of the page. After clicking the button, the page will jump to the top, or A suitable location for viewing the content.

So, how to control the display or hiding of the "Back to Top" button? In fact, we only need to implement the window.onscroll event. The code is as follows:

Copy the code The code is as follows:

window.onscroll = function () {
if (document.documentElement.scrollTop document.body.scrollTop > 100) {
document.getElementById("scrollto").style.display = "block" ;
}
else {
document.getElementById("scrollto").style.display = "none";
}
}

scrollto is the page Container (div tag) on ​​"back to top"
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn