JavaScript to implement the back to top button

JavaScript to implement the back to top button

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.
One thing to note here is that in order to avoid conflicts, you must "set the timer first" before setting the timer.
Finally, in order to increase the user experience, we need to design it so that if the current page is at the top, the "Back to Top" button will be automatically hidden; if the current page is not at the top, the "Back to Top" button will be displayed.

.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:
  • JavaScript implementation of the back to top button example
  • JavaScript monitors WeChat browser and comes with back button time
  • Implementing the back to top button based on Javascript
  • How to implement the javascript return to top button
  • Implementation of the return to top button in javascript
  • How to prevent the browser back button using JavaScript
  • JavaScript implements the return to top button in the lower right corner of the blog page
  • JS returns to the previous page example code through pictures and buttons respectively
  • js Click the button to pop up another page, select the value and return to the current page
  • Implementation of returning selected data by clicking a button in a JavaScript pop-up form

<<:  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

Recommend

mysql 5.7.18 winx64 password change

After MySQL 5.7.18 is successfully installed, sin...

How to uninstall MySQL 5.7.19 under Linux

1. Find out whether MySQL was installed before Co...

The implementation of Youda's new petite-vue

Table of contents Preface Introduction Live Easy ...

DHTML objects (common properties of various HTML objects)

!DOCTYPE Specifies the Document Type Definition (...

Detailed explanation of Vue project optimization and packaging

Table of contents Preface 1. Routing lazy loading...

Vue scaffolding learning project creation method

1. What is scaffolding? 1. Vue CLI Vue CLI is a c...

Summary of commonly used SQL statements for creating MySQL tables

Recently, I have been working on a project and ne...

...

Implementing a puzzle game with js

This article shares the specific code of js to im...

Analysis of common usage examples of MySQL process functions

This article uses examples to illustrate the comm...

Problems and solutions encountered when installing mininet on Ubuntu 16.04.4LTS

Mininet Mininet is a lightweight software defined...

Detailed example of IOS database upgrade data migration

Detailed example of IOS database upgrade data mig...