CSS Back to Top Code Example

CSS Back to Top Code Example

Most websites nowadays have long pages, some are four or five screens long, and some are two or three screens long. If a page is too long, in order to improve the user experience, a back to top button will appear on the right side of the page so that users can quickly return to the top to avoid visual screens when sliding the page. There are generally four ways to return to the top.

1. To return to the top through an anchor link, you need to add a tag called top to the body:

<a href="#top" target="_self">Back to top</a>

2. Use JavaScript to scroll back to the top and control the horizontal and vertical directions:

<a href="javascript:scroll(0,0)">JavaScript back to top<s/a>

3. Slide up slowly through JavaScript control, but it is not smooth enough. The code is as follows:

<a onclick="goScrollTop()">JavaScript slowly slides up</a>
function goScrollTop() {
    //Scroll the content by the specified number of pixels (the first parameter is the number of pixels to scroll right, the second parameter is the number of pixels to scroll down)
    //Upward is negative, downward is positive window.scrollBy(0, -100);
    //Delay recursive call to simulate scrolling up effect scrolldelay = setTimeout('goScrollTop()', 100);
    //Get the scrollTop value. For standard web pages that declare DTD, take document.documentElement.scrollTop. Otherwise, take document.body.scrollTop. Because only one of the two will take effect and the other will always be 0, so taking the sum of the two values ​​can get the real scrollTop value of the web page var sTop = document.documentElement.scrollTop + document.body.scrollTop;
    //Judge when the page reaches the top and cancel the delay code (otherwise the page will not be able to browse the page normally when it scrolls to the top)
    if (sTop == 0) clearTimeout(scrolldelay);
}

4. When the scroll bar is scrolled to a certain position, it is displayed. When the scroll bar rolls back upward, the upward back to top button is hidden. This method is the most commonly used method:

<div class="goTop">
    <span>Go</span>
</div>

jQuery code:

function goTop(min_height) {
    $(".goTop").click(
        function() {
            $('html,body').animate({
                scrollTop: 0
            }, 700);
        });
    //Get the minimum height of the page. If no value is passed in, the default is 600 pixels min_height=min_height?min_height:400;
    //Bind the processing function for the window's scroll event $(window).scroll(function() {
        //Get the vertical position of the window's scroll bar var s = $(window).scrollTop();
        //When the vertical position of the window's scroll bar is greater than the minimum height of the page, let the return to the top element appear gradually, otherwise fade out if (s > min_height) {
            $(".goTop").fadeIn(100);
        } else {
            $(".goTop").fadeOut(200);
        }
    });
}
 
 
$(function() {
    goTop();
});

CSS code:

.goTop {
    height: 40px;
    width: 40px;
    background: red;
    border-radius: 50px;
    position: fixed;
    top: 90%;
    right: 3%;
    display: none;
}
 
.goTop span {
    color: #fff;
    position: absolute;
    top: 12px;
    left: 8px;
}


This is the end of this article about CSS return to the top code examples. For more relevant CSS return to the top content, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

Author:FlyElephant
Source: http://www.cnblogs.com/xiaofeixiang

<<:  Html+CSS floating advertisement strip implementation

>>:  Using Docker+jenkins+python3 environment to build a super detailed tutorial

Recommend

How to get datetime data in mysql, followed by .0

The data type of MySQL is datetime. The data stor...

HTML Nine-grid Layout Implementation Method

Diversifying website layouts is our front-end spe...

HTML code example: detailed explanation of hyperlinks

Hyperlinks are the most frequently used HTML elem...

Analysis of the solution to Nginx Session sharing problem

This article mainly introduces the solution to th...

Tudou.com front-end overview

1. Division of labor and process <br />At T...

JavaScript implements the detailed process of stack structure

Table of contents 1. Understanding the stack stru...

Practical method of deleting a row in a MySql table

First, you need to determine which fields or fiel...

Detailed steps to use Arthas in a Docker container

What can Arthas do for you? Arthas is Alibaba'...

In-depth study of vue2.x--Explanation of the h function

Table of contents Solution, Summarize: vue projec...

25 advanced uses of JS array reduce that you must know

Preface Reduce is one of the new conventional arr...

Usage of if judgment in HTML

In the process of Django web development, when wr...

MySQL cursor principle and usage example analysis

This article uses examples to explain the princip...

Handwriting implementation of new in JS

Table of contents 1 Introduction to the new opera...