Home > Daily Programming > CSS Knowledge > How to achieve the effect of clicking on the web page to return to the top in jquery? (Pictures + Videos)

How to achieve the effect of clicking on the web page to return to the top in jquery? (Pictures + Videos)

藏色散人
Release: 2018-10-13 16:46:00
Original
6597 people have browsed it

This article mainly introduces how to use jquery code to achieve the effect of returning the web page to the top.

When we browse the pages of major websites, we must all have encountered it. When browsing a long page, when you scroll to the lower part, a button effect similar to the return to the top will appear.

This special effect of clicking back to the top can greatly improve the user experience. Then it is very simple to implement such a function.

Below we will use a simple code example to introduce you to the functional method of using jquery to realize clicking back to the top.

<!DOCTYPE html>
<html>
<head>
    <title>回到顶部特效</title>
    <meta charset="UTF-8">
    <script type="text/javascript" src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <style>
        body {
            text-align: center;
        }
        h1 {
            width: 1200px;
            height: 1500px;
            background: #eee;
            margin: 30px auto;
        }
        a {
            text-decoration: none;
            color: #fff;
        }
        p#back {
            text-align: center;
            position: fixed;
            bottom: 100px;
            right: 60px;
            background: #000;
            border-radius: 3px;
            height: 50px;
            width: 80px;
            display: none;
        }
    </style>
</head>
<body>
<h1>网页内容</h1>
<a href=""><p id="back"><br> 返回顶部</p></a>
</body>
<script>
    // 文档就绪
    $(function () {
        // 获取浏览器窗口
        $(window).scroll(function () {
            // 获取浏览器滚动条距离顶部的高度
            if ($(window).scrollTop() > 150) {
                // 设置按钮出现
                $(&#39;#back&#39;).fadeIn(1000)
            } else {
                // 设置按钮消失
                $(&#39;#back&#39;).fadeOut(1000)
            }
        })
    })
</script>
</html>
Copy after login

In this code, we mainly referenced a jquery online library, and wrote some simple HTML pages and js code to judge. When the height of the scroll bar from the top is greater than 150, the setting button appears. , otherwise the button is hidden. Finally access it through the browser, the effect is as follows:

How to achieve the effect of clicking on the web page to return to the top in jquery? (Pictures + Videos)

Then we drag the scroll bar down, and the button to return to the top will slowly appear, the effect is as follows:

How to achieve the effect of clicking on the web page to return to the top in jquery? (Pictures + Videos)

Note:

fadeIn() The method uses the fade-in effect to display the selected element if the element is hidden . (In this example, the parameter is set to 1000 milliseconds, which means that the fade-in transition time of the selected element is 1000 milliseconds. If it is set to 0, the selected element will appear immediately.)

fadeOut() Method uses the fade out effect to hide the selected element, if the element is hidden. (Parameters are the same as above)

This article is about the introduction of jquery to achieve the effect of returning the web page to the top. It is simple and easy to understand. I hope it will be helpful to friends in need!

If you want to know more about front-end related knowledge, you can follow PHP Chinese website jQuery video tutorial or HTML video tutorial, CSS video tutorial, Bootstrap video tutorial and other related video tutorials, welcome everyone to refer to and learn!

The above is the detailed content of How to achieve the effect of clicking on the web page to return to the top in jquery? (Pictures + Videos). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template