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)

不言
Release: 2018-08-01 11:48:31
Original
17179 people have browsed it

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template