Native js imitates mobile phone pull-down refresh

Native js imitates mobile phone pull-down refresh

This article shares the specific code of js imitating the pull-down refresh on the mobile phone for your reference. The specific content is as follows

Without further ado, let’s take a look at the renderings:

When the pull-down is less than 40px, the text is displayed:

When the pull-down is greater than 40px, the text is displayed

Show text when releasing

Implementation principle

<div class="content">
        <div class="left"></div>
        <div class="top">
            <p id="p"></p>
            <div id="buttom">
            </div>
        </div>
</div>

CSS:

<style>
    * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }
    
    .content {
        width: 350px;
        height: 600px;
        position: relative;
        margin: 0 auto;
    }
    
    .top {
        width: 100%;
        height: 100%;
        background-color: #ccc;
        border: 12px solid black;
        border-radius: 10px;
        overflow: hidden;
        margin-top: 50px;
        border-top: 35px solid black;
    }
    
    #button {
        width: 100%;
        height: 100%;
        background-color: rgb(47, 196, 47);
        position: relative;
        padding: 10px;
        border-top: 2px solid red;
    }
    
    #p {
        display: inline-block;
        height: 30px;
        width: 100%;
        text-align: center;
        line-height: 30px;
        color: rgb(2, 2, 2);
        font-size: 15px;
        position: absolute;
        top: 40px;
        left: 0;
        display: none;
    }
    
    .left {
        height: 10px;
        width: 100px;
        background-color: #ccc;
        position: absolute;
        top: 12px;
        left: 110px;
        border-radius: 5px;
    }
    
    .left::after {
        display: inline-block;
        content: "";
        width: 15px;
        height: 15px;
        background-color: #ccc;
        border-radius: 50%;
        position: absolute;
        left: 120px;
        top: -2px;
    }
</style>

JS:

<script>
    var but = document.getElementById("buttom");
    var p = document.getElementById("p");
    var moveY = 0 //Initialize the position when pressed var tops = 0; //Initialize tops. tops is the distance of the pull-downbut.onmousedown = function(e) { //Mouse press eventmoveY = e.offsetY //Get the Y-axis position when the mouse is pressedbut.onmousemove = function(e) { //Mouse move eventp.innerHTML = "Pull down to refresh"
            p.style.display = "block"; //When the mouse moves, the text is displayed as "pull down to refresh"
            tops = e.offsetY - moveY //tops size is the distance the mouse moves on the Y axis minus the distance when pressed if (tops < 0) {
                tops = 0 //Stop pull-up } else if (tops > 40) {
                //When tops is greater than 40, it prompts that you can release the mouse to refresh immediately p.innerHTML = "Release to refresh immediately"
            }
            this.style.top = tops + 'px'; //Let the top value of the element relative positioning equal the value of tops // console.log(tops)
        }
        but.onmouseup = function() { //Mouse release event but.onmousemove = null //Clear mouse movement events to prevent elements from continuing to follow the mouse if (tops < 40) {
                //If the pull-down distance b is less than 40px, the element will be reset immediately without refreshing, and the prompt text will disappear this.style.top = 0;
                p.style.display = "none"
            } else {
                //If the pull-down distance is greater than 40px, it will prompt that it is refreshing p.innerHTML = "Refreshing . . ."
                setTimeout(() => { //Reset after 1.3 seconds delay. This is just a simulation. In actual projects, tops = 0 needs to be reset after successfully re-requesting data.
                    this.style.top = tops;
                    p.style.display = "none"
                }, 1300);
            }
        }
    }
</script>

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:
  • Implement pull-down refresh and pull-up loading effects based on iscroll.js
  • Pure javascript to achieve simple pull-down refresh function
  • Pull-up loading and pull-down refreshing in vue.js mobile app
  • Vue.js integrates vux's pull-up loading and pull-down refreshing example tutorial
  • Simple implementation of Javascript pull-down refresh
  • Summary of using JS plugin dropload to refresh and pull up to load
  • Solution to the problem that iscroll.js cannot bounce back when pulling up and down to refresh
  • Detailed explanation of pull-down refresh and pull-up loading using dropload.js plugin
  • js imitates the pull-down refresh effect of mobile page files
  • JS+CSS to implement pull-down refresh/pull-up loading plug-in

<<:  Methods and steps for deploying go projects based on Docker images

>>:  Detailed graphic and text tutorial on downloading, installing and configuring mysql-5.7.28 under Windows

Recommend

Summary and examples of vue3 component communication methods

The communication modes of vue3 components are as...

HTML uses marquee to achieve text scrolling left and right

Copy code The code is as follows: <BODY> //...

VMware ESXi installation and use record (with download)

Table of contents 1. Install ESXi 2. Set up ESXi ...

HTML checkbox Click the description text to select/uncheck the state

In web development, since the checkbox is small an...

Example of using JSX to build component Parser development

Table of contents JSX environment construction Se...

Pure CSS3 to create page switching effect example code

The one I wrote before is too complicated, let’s ...

Native JS music player

This article example shares the specific code of ...

How to use regular expression query in MySql

Regular expressions are often used to search and ...

A brief discussion on the design of Tomcat multi-layer container

Table of contents Container Hierarchy The process...

Implementation of Linux command line wildcards and escape characters

If we want to perform batch operations on a type ...

getdata table table data join mysql method

public function json_product_list($where, $order)...

Linux file system operation implementation

This reading note mainly records the operations r...

Nginx operation and maintenance domain name verification method example

When configuring the interface domain name, each ...