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

Specific use of the wx.getUserProfile interface in the applet

Recently, WeChat Mini Program has proposed adjust...

Using JS to determine the existence of elements in an array in ten minutes

Preface In front-end development, you often need ...

Vue.js implements the nine-grid image display module

I used Vue.js to make a nine-grid image display m...

Sharing tips on using Frameset to center the widescreen

Copy code The code is as follows: <frameset co...

Detailed explanation of Vue's caching method example

Recently, a new requirement "front-end cache...

Which one should I choose between MySQL unique index and normal index?

Imagine a scenario where, when designing a user t...

Overview of the basic components of HTML web pages

<br />The information on web pages is mainly...

How to use Lottie animation in React Native project

Lottie is an open source animation library for iO...

How to draw special graphics in CSS

1. Triangle Border settings Code: width: 300px; h...

Detailed explanation of Mybatis special character processing

Preface: Mybatis special character processing, pr...

Future-oriented all-round web design: progressive enhancement

<br />Original: Understanding Progressive En...

HTML table tag tutorial (44): table header tag

<br />In order to clearly distinguish the ta...

Using jQuery to implement the carousel effect

This article shares the specific code for impleme...

Detailed steps to install MYSQL8.0 on CentOS7.6

1. Generally, mariadb is installed by default in ...