未封装的原生下拉加载实现

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
        font-family: "Courier New", Courier, monospace;
    }

    .content {
        transition: margin-top 0.2s;
        width: 100vw;
        height: 300px;
        background: skyblue;
        display: grid;
        grid-template-columns: repeat(3, 1fr);
        grid-template-rows: repeat(3, 1fr);
    }

    .con {
        grid-row: 2 / 3;
        grid-column: 2 / 3;
        display: flex;
        justify-content: center;
        align-items: center;
    }

    .pullRefresh {
        transition: height 0.2s;
        position: absolute;
        top: 0;
        z-index: -1;
        width: 100vw;
        /* height: 50px; */
        display: flex;
        justify-content: center;
        align-items: center;
        color: white;
        background-size: 200%;
        animation: alphaGradient 3s alternate infinite;
        background-image: linear-gradient(45deg, #ff5252 30%, #b33939 90%, #ff793f 100%);
    }

    @keyframes alphaGradient {
        0% {
            background-position: 0 50%;
        }

        50% {
            background-position: 100% 50%;
        }

        100% {
            background-position: 50% 0;
        }
    }
</style>

<body>
    <div class="pullRefresh">准备加载...</div>
    <div class="content">
        <div class="con">
            下拉加载
        </div>
    </div>
</body>
<script>
    //获取到操作的dom元素
    const content = document.querySelector('.content');
    const pullRefresh = document.querySelector('.pullRefresh');
    let startY, endY;
    content.addEventListener("touchstart", function (e) {
        startY = parseInt(e.changedTouches[0].pageY)
    });
    content.addEventListener("touchmove", function (e) {
        endY = parseInt(e.changedTouches[0].pageY);
        if (endY - startY > 0) {
            content.style.marginTop = `${endY - startY}px`;
            pullRefresh.style.height = `${endY - startY}px`;
        }
    });
    content.addEventListener("touchend", function (e) {
        endY = parseInt(e.changedTouches[0].pageY)
        if (endY - startY > 200 && (window.pageYOffset === 0 || document.documentElement.scrollTop === 0)) {
            pullRefresh.innerHTML = "加载中"
            content.style.marginTop = '50px';
            pullRefresh.style.height = '50px';
            setTimeout(() => {
                pullRefresh.innerHTML = "加载成功"
                setTimeout(() => {
                    content.style.marginTop = 0;
                    pullRefresh.style.height = 0;
                    setTimeout(() => {
                        pullRefresh.innerHTML = "准备加载..."
                    }, 1000);
                }, 1000);
            }, 2000);
        } else {
            console.log('下拉幅度不够');
            content.style.marginTop = 0;
            pullRefresh.style.height = 0;
        }
    });
</script>

</html>
全部评论

相关推荐

10-29 16:42
门头沟学院 Java
1.今天什么国标的公司打电话约面试,还得准备ppt,好麻烦,网上查薪资一般,打算拒了,不面了2.字节又复活了,什么安全开发,也不知道怎么样,面一面试试吧,还是挺想去字节的,但好难,随缘吧所以今天没面试
嵌入式的小白:面试前可以好好准备下 1.看看你投递的岗位的岗位描述,分析下是哪个业务线,同使要罗列他们描述中提到的技术点 2.根据1中的两点准备 3.岗位描述中应该还有语言要求,这个刷刷八股,要是对自己语言能力很有把握,那就不用看这点了 4.找下你简历中项目部分,看有没有和岗位描述中技术点重合的,这种在面试提到项目时,是高概率问题 好好准备,祝你面试顺利
我的求职进度条
点赞 评论 收藏
分享
09-22 09:42
门头沟学院 Java
牛客37185681...:马德,我感觉这是我面过最恶心的公司,一面是两个女hr,说什么实习前几个月属于试用期,试用期过了才能转成正式实习生,我***笑了,问待遇就是不说,问能不能接受全栈,沙币公司
如果可以选,你最想去哪家...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务