原生js仿寫(xiě)手機(jī)端下拉刷新
本文實(shí)例為大家分享了js仿寫(xiě)手機(jī)端下拉刷新的具體代碼,供大家參考,具體內(nèi)容如下
話不多說(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的話,元素馬上復(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í)有所幫助,也希望大家多多支持腳本之家。
- 基于iscroll.js實(shí)現(xiàn)下拉刷新和上拉加載效果
- 純javascript實(shí)現(xiàn)簡(jiǎn)單下拉刷新功能
- vue.js移動(dòng)端app之上拉加載以及下拉刷新實(shí)戰(zhàn)
- vue.js整合vux中的上拉加載下拉刷新實(shí)例教程
- Javascript下拉刷新的簡(jiǎn)單實(shí)現(xiàn)
- JS 插件dropload下拉刷新、上拉加載使用小結(jié)
- iscroll.js的上拉下拉刷新時(shí)無(wú)法回彈的解決方法
- dropload.js插件下拉刷新和上拉加載使用詳解
- js仿手機(jī)頁(yè)面文件下拉刷新效果
- JS+CSS實(shí)現(xiàn)下拉刷新/上拉加載插件
相關(guān)文章
基于JavaScript短信驗(yàn)證碼如何實(shí)現(xiàn)
我們?cè)谑褂靡苿?dòng)、電信等運(yùn)營(yíng)商網(wǎng)上營(yíng)業(yè)廳的時(shí)候,為確保業(yè)務(wù)的完整和正確性,經(jīng)常會(huì)需要用到短信的驗(yàn)證碼。最近因?yàn)槟呈I(yè)務(wù)需要,也做了個(gè)類(lèi)似的功能2016-01-01
微信小程序?qū)崿F(xiàn)手機(jī)獲取驗(yàn)證碼倒計(jì)時(shí)60s
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)手機(jī)獲取驗(yàn)證碼后倒計(jì)時(shí)60s,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
利用js判斷數(shù)據(jù)是否是數(shù)組或字符串的常見(jiàn)方法
這篇文章主要給大家介紹了關(guān)于利用js判斷數(shù)據(jù)是否是數(shù)組或字符串的常見(jiàn)方法,其實(shí)有很多方法可以判斷數(shù)據(jù)是否是數(shù)組或字符串,需要的朋友可以參考下2023-07-07
javascript輕量級(jí)庫(kù)createjs使用Easel實(shí)現(xiàn)拖拽效果
這篇文章主要介紹了javascript輕量級(jí)庫(kù)createjs使用Easel實(shí)現(xiàn)拖拽效果的相關(guān)資料,需要的朋友可以參考下2016-02-02
帝國(guó)cms首頁(yè)列表頁(yè)實(shí)現(xiàn)點(diǎn)贊功能
這篇文章主要介紹了帝國(guó)cms首頁(yè)列表頁(yè)實(shí)現(xiàn)點(diǎn)贊功能的相關(guān)資料,需要的朋友可以參考下2017-10-10
使用bootstrap插件實(shí)現(xiàn)模態(tài)框效果
今天我們選擇使用著名的 bootstrap 庫(kù)的模態(tài)框插件 modal.js 來(lái)實(shí)現(xiàn)模態(tài)框效果,同時(shí)也使大家進(jìn)一步熟悉 bootstrap 的插件使用,需要的朋友可以參考下2017-05-05
JS在onclientclick里如何控制onclick的執(zhí)行
這篇文章主要介紹了JS在onclientclick里如何控制onclick的執(zhí)行的相關(guān)資料,需要的朋友可以參考下2016-05-05
微信小程序onLaunch異步,首頁(yè)onLoad先執(zhí)行?
這篇文章主要介紹了微信小程序onLaunch異步,首頁(yè)onLoad先執(zhí)行? 文章底部給大家介紹了小程序_onLaunch異步回調(diào)數(shù)據(jù)加載問(wèn)題的兩種解決方案,需要的朋友可以參考下2018-09-09

