This article shares the specific code for JavaScript to implement the back to top button for your reference. The specific content is as follows 1. Build the scaffolding first a { text-decoration: none; } body { height: 5000px; } .backtotop { position: fixed; bottom: 80px; right: 80px; width: 80px; height: 80px; background-color: #ccc; font-size: 20px; text-align: center; padding-top: 12px; box-sizing: border-box; cursor: pointer; color: #000; /* Hide the button first */ /*display: none;*/ } <a href="javascript:;" class="backtotop" id="backtotop">Back to<br>Top</a> 2. Logical part When the mouse clicks the "Back to Top" button, it will return to the top at a certain "speed" every 50 milliseconds. After returning to the top, it must be cleared, otherwise the page will automatically return to the top as soon as it is pulled down. Two methods are used here, one is setInterval, and the other is clearInterval. The former is to set the timer, and the latter is to clear the timer. .backtotop { /* Hide the button first */ display: none; } (function() { // 1. Get the element to be operated let oBackBtn = document.querySelector("#backtotop"); // 2. Monitor the scrolling of the web page window.onscroll = function() { // Get the scroll distance let offsetY = getPageScroll().y; // console.log(offsetY); // Determine whether to display the rollback button if (offsetY >= 200) { oBackBtn.style.display = "block"; } else { oBackBtn.style.display = "none"; } } let timerId = null; // 3. Listen for clicks on the rollback button oBackBtn.onclick = function() { //Set the table to close first to prevent timer conflict clearInterval(timerId); //Set the timer timerId = setInterval(function() { let begin = getPageScroll().y; //Current position let target = 0; //Target position let step = (target - begin) * 0.3; begin += step; //Judge whether to end if (Math.abs(Math.floor(step)) <= 1) { //Clear timer clearInterval(timerId); // window.scrollTo(x, y); // x indicates the position to which the webpage should be scrolled horizontally // y indicates the position to which the webpage should be scrolled vertically window.scrollTo(0, 0); return; } window.scrollTo(0, begin); }, 50); }; function getPageScroll() { let x, y; if (window.pageXOffset) { x = window.pageXOffset; y = window.pageYOffset; } else if (document.compatMode === "BackCompat") { x = document.body.scrollLeft; y = document.body.scrollTop; } else { x = document.documentElement.scrollLeft; y = document.documentElement.scrollTop; } return { x: x, y: y } } })(); The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: In-depth understanding of MySQL slow query log
>>: Two ways to use IIS to call X-Forwarded-For Header (XFF) to record the visitor's real IP
After MySQL 5.7.18 is successfully installed, sin...
1. Find out whether MySQL was installed before Co...
If we introduce the nesting rules of basic HTML w...
Table of contents Preface Introduction Live Easy ...
Table of contents Problem Overview Problem Reprod...
!DOCTYPE Specifies the Document Type Definition (...
Table of contents Preface 1. Routing lazy loading...
1. What is scaffolding? 1. Vue CLI Vue CLI is a c...
1. <div></div> and <span></s...
Recently, I have been working on a project and ne...
This article shares the specific code of js to im...
This article uses examples to illustrate the comm...
Mininet Mininet is a lightweight software defined...
Detailed example of IOS database upgrade data mig...