search
HomeDaily ProgrammingCSS KnowledgeHow to achieve the effect of clicking on the web page to return to the top in jquery? (Pictures + Videos)

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 id="网页内容">网页内容</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>

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software