本文講的是在uniapp項目中實現(xiàn)頁面回頂效果的方法。以下是代碼(回頂可能多個頁面都需要用到建議封裝成一個組件)
一、方法一
<template>
<view class="content">
<view class="" v-for="(item,index) in 100" :key="index">
{{index}}
</view>
<view class="upward" v-if="isShow" @click="Totop()">
<u-icon name="arrow-upward" color="#434343" size="28"></u-icon>
</view>
</view>
</template>
<script>
export default {
data() {
return {
isShow:false,
}
},
onPageScroll(e){
? ? ? ? ? ? ?// 監(jiān)聽頁面滾動
if(e.scrollTop>200){
this.isShow=true;
}else{
this.isShow=false;
}
},
methods: {
Totop(){
uni.pageScrollTo({
scrollTop: 0,//滾動到頁面的目標位置
duration: 300
});
}
}
}
</script>
<style lang="less">
.content{
width: 100%;
position: relative;
.u-tabs{
width: 100%;
// margin: 18rpx auto;
height: 80rpx;
display: flex;
align-items: center;
background-color: #fff;
}
.upward{
width: 70rpx;
height: 70rpx;
display: flex;
justify-content: center;
align-items: center;
border-radius: 100%;
border: 3rpx solid #d0d0d0;
margin-bottom: 20rpx;
background-color: rgba(255, 255, 255, 0.4);
position: fixed;
bottom: 300rpx;
right: 30rpx;
}
}
</style>
onPageScroll是頁面生命周期,監(jiān)聽頁面滾動,參數(shù)為Object
uni.pageScrollTo相關(guān)參數(shù)在官方文檔可以查看文章來源:http://www.zghlxwxcb.cn/news/detail-503058.html
效果圖(頁面滾動距離大于200顯示回頂按鈕)文章來源地址http://www.zghlxwxcb.cn/news/detail-503058.html

二、使用uView組件

<template>
<view class="wrap">
<text>滑動頁面,返回頂部按鈕將出現(xiàn)在右下角</text>
<u-back-top :scroll-top="scrollTop"></u-back-top>
</view>
</template>
<script>
export default {
data() {
return {
scrollTop: 0
}
},
onPageScroll(e) {
this.scrollTop = e.scrollTop;
}
};
</script>
<style lang="scss" scoped>
.wrap {
height: 200vh;
}
</style>
到了這里,關(guān)于uniapp頁面回到頂部方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!