最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

原生js仿寫(xiě)手機(jī)端下拉刷新

 更新時(shí)間:2021年08月18日 14:13:12   作者:前端挖掘機(jī)  
這篇文章主要為大家詳細(xì)介紹了原生js仿寫(xiě)手機(jī)端下拉刷新,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了js仿寫(xiě)手機(jī)端下拉刷新的具體代碼,供大家參考,具體內(nèi)容如下

話(huà)不多說(shuō)先看效果圖:

當(dāng)下拉小于40px時(shí)顯示文字:

當(dāng)下拉大于40px時(shí)現(xiàn)實(shí)文字

松開(kāi)時(shí)顯示文字

實(shí)現(xiàn)原理

<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;
    }
    
    #buttom {
        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 //初始化按下時(shí)的位置
    var tops = 0; //初始化tops。tops為下拉的距離
    but.onmousedown = function(e) { //鼠標(biāo)按下事件
        moveY = e.offsetY //獲取鼠標(biāo)按下時(shí)Y軸的位置
        but.onmousemove = function(e) { //鼠標(biāo)移動(dòng)事件
            p.innerHTML = "下拉刷新"
            p.style.display = "block"; //鼠標(biāo)移動(dòng)時(shí)現(xiàn)實(shí)提升文字并且讓文字為“下拉刷新”
            tops = e.offsetY - moveY //tops大小為鼠標(biāo)Y軸移動(dòng)的距離減去按下時(shí)的距離
            if (tops < 0) {
                tops = 0 //阻止上拉
            } else if (tops > 40) {
                //tops大于40時(shí)提示可以松開(kāi)鼠標(biāo)馬上刷新
                p.innerHTML = "松開(kāi)立刻刷新"
            }
            this.style.top = tops + 'px'; //讓元素相對(duì)定位的top值等于tops的值
            // console.log(tops)
        }
        but.onmouseup = function() { //鼠標(biāo)松開(kāi)事件
            but.onmousemove = null //清空鼠標(biāo)移動(dòng)事件,阻止元素繼續(xù)跟著鼠標(biāo)移動(dòng)
            if (tops < 40) {
                //如果下拉距離b不足40px的話(huà),元素馬上復(fù)位不進(jìn)行刷新,并且提示文字消失
                this.style.top = 0;
                p.style.display = "none"
            } else {
                //如果下拉距離大于40px提示正在刷新
                p.innerHTML = "正 在 刷 新 . . ."
                setTimeout(() => { //延遲1.3秒后復(fù)位,這里做的只是模仿,實(shí)際項(xiàng)目需要在重新請(qǐng)求數(shù)據(jù)成功后復(fù)位
                    tops = 0
                    this.style.top = tops;
                    p.style.display = "none"
                }, 1300);
            }
        }
    }
</script>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

双城市| 理塘县| 佛教| 长垣县| 平顺县| 辉南县| 广州市| 密云县| 九寨沟县| 九江市| 芦山县| 日照市| 韩城市| 修武县| 桦甸市| 翁牛特旗| 彭山县| 封丘县| 新民市| 泊头市| 永仁县| 招远市| 高雄县| 高唐县| 伊金霍洛旗| 无为县| 油尖旺区| 蓬莱市| 习水县| 满洲里市| 晴隆县| 定日县| 阜南县| 漳浦县| 兰西县| 赣榆县| 武陟县| 河源市| 武鸣县| 保山市| 广水市|