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

Vue uses Canvas to generate random sized and non-overlapping circles

Table of contents Canvas related documents Effect...

Steps to set up Windows Server 2016 AD server (picture and text)

Introduction: AD is the abbreviation of Active Di...

Several ways to run Python programs in the Linux background

1. The first method is to use the unhup command d...

Notes on MySQL case sensitivity

Table of contents MySQL case sensitivity is contr...

HTML 5 Reset Stylesheet

This CSS reset is modified based on Eric Meyers...

How to view the status of remote server files in Linux

As shown below: The test command determines wheth...

MySQL InnoDB row_id boundary overflow verification method steps

background I talked to my classmates about a boun...

Detailed explanation of the mysql database LIKE operator in python

The LIKE operator is used in the WHERE clause to ...

Practice of multi-layer nested display of element table

There is a requirement for a list containing mult...

How to remove carriage return characters from text in Linux

When the carriage return character ( Ctrl+M ) mak...

An example of using Dapr to simplify microservices from scratch

Table of contents Preface 1. Install Docker 2. In...

Summary of Linux ps and pstree command knowledge points

The ps command in Linux is the abbreviation of Pr...

Use CSS to set the width of INPUT in TD

Recently, when I was using C# to make a Web progra...