uniapp頁面回到頂部兩種實現(xiàn)方法
本文講的是在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ù)在官方文檔可以查看
效果圖(頁面滾動距離大于200顯示回頂按鈕)

二、使用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>總結(jié)
到此這篇關(guān)于uniapp頁面回到頂部兩種實現(xiàn)方法的文章就介紹到這了,更多相關(guān)uniapp頁面回到頂部內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JS 中document.write()的用法和清空的原因淺析
這篇文章主要介紹了JS 中document.write()的用法和清空的原因淺析,需要的朋友可以參考下2017-12-12
js字符串替換所有的指定字符或文字(推薦replaceAll方法)
要實現(xiàn)js字符串替換所有的某個字符,推薦大家使用replaceAll方法,默認不是所有瀏覽器都兼容,所以這里給出一個解決方案,需要的朋友可以參考下2014-07-07
JS實現(xiàn)兩個大數(shù)(整數(shù))相乘
大數(shù),即超出語言所能表示的數(shù)字最大范圍的數(shù)字,那么如何實現(xiàn)兩個大數(shù)相乘呢?下面有個不錯的方法,大家可以參考下2014-04-04
javascript將數(shù)組插入到另一個數(shù)組中的代碼
下面的代碼主要功能就是將數(shù)組arr2插入到數(shù)組arr1的index位置,需要的朋友可以參考下2013-01-01

