Home > Web Front-end > JS Tutorial > Implementation of native js pull-down refresh and pull-up loading effects (code attached)

Implementation of native js pull-down refresh and pull-up loading effects (code attached)

云罗郡主
Release: 2018-10-11 15:35:56
forward
5157 people have browsed it

The content this article brings to you is about the implementation of native js pull-down refresh and pull-up loading effects (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Implementation of native js pull-down refresh and pull-up loading effects (code attached)

You can see the effect by switching to the mobile terminal in the console

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        .header {
            position: fixed;
            left: 0;
            top: 0;
            width: 100%;
            height: 60px;
            background: pink;
            line-height: 60px;
            text-align: center;
            color: white;
        }
        .content {
            width: 200px;
            height: 1000px;
            border: 2px solid green;
            background: linear-gradient(#fff, #000);
            -webkit-flex-shrink: 0;
            flex-shrink: 0;
            margin: 60px auto 0;
        }
        img {
            width: 100%;
            height: 180px;
            position: absolute;
            top: 60px;
            left: 0;
            display: none;
            z-index: -1
        }
        p{
            line-height: 30px;
            text-align: center;
            display: none
        }
    </style>
</head>
<body>
    <div>
        <div>首页</div>
        <img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1536127130721&di=f192c5e1749dd2aa73b8a5b7297bd0cc&imgtype=0&src=http%3A%2F%2Fs10.sinaimg.cn%2Fmiddle%2F4ac1551583fc20f4db299%26690"
            alt="">
        <div></div>
        <p>加载中...</p>
    </div>
    <script>
        var oImg = document.getElementsByTagName(&#39;img&#39;)[0];
        var oP=document.getElementsByTagName(&#39;p&#39;)[0];
        var oHeader = document.getElementsByClassName(&#39;header&#39;)[0];
        var oContent = document.getElementsByClassName(&#39;content&#39;)[0];
        var screenH = document.documentElement.clientHeight || document.body.clientHeight;
        var startY,
            moveY;
        oContent.addEventListener(&#39;touchstart&#39;, (e) => {
            startY = e.touches[0].pageY;
        })
        oContent.addEventListener(&#39;touchmove&#39;, (e) => {
            moveY = e.touches[0].pageY - startY;
        });
        oContent.addEventListener(&#39;touchend&#39;, () => {
            if (moveY > 0) {//下拉
                oImg.style.display = &#39;block&#39;;
                if (moveY > 180) moveY = 180;
                oContent.style.marginTop = moveY + oHeader.clientHeight + &#39;px&#39;;
                //刷新数据后再走下面的
                var timer = setInterval(() => {
                    moveY -= 1;
                    if (moveY <= 0) {
                        oImg.style.display = &#39;none&#39;;
                        clearInterval(timer);
                        moveY = 0
                    }
                    oContent.style.marginTop = moveY + oHeader.clientHeight + &#39;px&#39;;
                }, 4)
            } else {//上拉
                var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
                if (scrollTop + screenH + 20 >= oContent.scrollHeight) {
                    oP.style.display=&#39;block&#39;
                    //加载下条数据
                    setTimeout(() => {
                        oP.style.display=&#39;none&#39;
                        oContent.style.height = oContent.clientHeight + 300 + &#39;px&#39;
                    }, 500)
                }
            }
        })
    </script>
</body>
</html>
Copy after login

The above is the implementation of the native js pull-down refresh and pull-up loading effects (code attached) ), if you want to know more about JavaScript video tutorial, please pay attention to the PHP Chinese website.

The above is the detailed content of Implementation of native js pull-down refresh and pull-up loading effects (code attached). For more information, please follow other related articles on the PHP Chinese website!

source:qdfuns.com
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