Home Web Front-end H5 Tutorial HTML5 implements pull-down refresh on mobile terminal (principle and code)

HTML5 implements pull-down refresh on mobile terminal (principle and code)

Aug 01, 2018 am 11:48 AM
html html5 javascript

This article introduces to you about HTML5 implementation of mobile pull-down refresh (principle and code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Pull-down refresh on the mobile side is a very common function, and there are many open source libraries that implement this function. However, in order to learn, let’s write an example to learn it first. Some touch events and some DOM properties CSS3 properties are used. Go directly to the code, there are comments in the code.

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Document</title>
    <style type="text/css">
        html,
        body,
        header,
        p,
        main,
        p,
        span,
        ul,
        li {
            margin: 0;
            padding: 0;
        }

        #refreshContainer li {
            background-color: #eee;
            margin-bottom: 1px;
            padding: 20px 10px;
        }

        .refreshText {
            position: absolute;
            width: 100%;
            line-height: 50px;
            text-align: center;
            left: 0;
            top: 0;
            transform: translateY(-50px);
        }
    </style>
</head>

<body>
    <main class="parent">
        <p class="refreshText"></p>
        <ul id="refreshContainer">
            <li>111</li>
            <li>222</li>
            <li>333</li>
            <li>444</li>
            <li>555</li>
            <li>111</li>
            <li>222</li>
            <li>333</li>
            <li>444</li>
            <li>555</li>
            <li>111</li>
            <li>222</li>
            <li>333</li>
            <li>444</li>
            <li>555</li>
        </ul>
    </main>
    <script type="text/javascript">
        window.onload = function(){
            //1.获取到列表的dom,刷新显示部分的dom,列表父容器的dom
            let container = document.querySelector('#refreshContainer');
            let refreshText = document.querySelector('.refreshText');
            let parent = document.querySelector('.parent');

            //2.定义一些需要常用的变量
            let startY = 0;//手指触摸最开始的Y坐标
            let endY = 0;//手指结束触摸时的Y坐标
           
            //3.给列表dom监听touchstart事件,得到起始位置的Y坐标
            parent.addEventListener('touchstart',function(e){
                startY = e.touches[0].pageY;
            });
            //4.给列表dom监听touchmove事件,当移动到一定程度需要显示上面的文字
            parent.addEventListener('touchmove',function (e) { 
                if(isTop() && (e.touches[0].pageY-startY) > 0){
                    console.log('下拉了');
                    refreshText.style.height = "50px";
                    parent.style.transform = "translateY(50px)";
                    parent.style.transition = "all ease 0.5s";
                    refreshText.innerHTML = "释放立即刷新...";
                }
            });
            //5.给列表dom监听touchend事件,此时说明用户已经松开了手指,应该进行异步操作了
            parent.addEventListener('touchend',function (e) { 
                if(isTop()){
                    refreshText.innerHTML = "正在刷新...";
                    setTimeout(function(){
                        parent.style.transform = "translateY(0)";
                        console.log('成功刷新');
                    },2000)
                }
                return;
            })
            function isTop(){
                var t = document.documentElement.scrollTop||document.body.scrollTop;
                return t === 0 ? true : false;
            }

        }
    </script>
</body>

</html>
Copy after login
  • The transform and animate in CSS3 are used. Because since they are all mobile, these things are basically supported.

  • Please look at the code for details, there are also comments. This article only explains the principle, and will be encapsulated into an object later. Convenient to call directly.

Recommended related articles:

vue.js mobile terminal implements pull-up loading and pull-down refresh

Mobile pull-down refresh, iScroll.js usage (reprint)_html/css_WEB-ITnose

The above is the detailed content of HTML5 implements pull-down refresh on mobile terminal (principle and code). For more information, please follow other related articles on the PHP Chinese website!

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

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

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles