uniapp頁(yè)面回到頂部?jī)煞N實(shí)現(xiàn)方法
本文講的是在uniapp項(xiàng)目中實(shí)現(xiàn)頁(yè)面回頂效果的方法。以下是代碼(回頂可能多個(gè)頁(yè)面都需要用到建議封裝成一個(gè)組件)
一、方法一
<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)聽頁(yè)面滾動(dòng) if(e.scrollTop>200){ this.isShow=true; }else{ this.isShow=false; } }, methods: { Totop(){ uni.pageScrollTo({ scrollTop: 0,//滾動(dòng)到頁(yè)面的目標(biāo)位置 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是頁(yè)面生命周期,監(jiān)聽頁(yè)面滾動(dòng),參數(shù)為Object
uni.pageScrollTo相關(guān)參數(shù)在官方文檔可以查看
效果圖(頁(yè)面滾動(dòng)距離大于200顯示回頂按鈕)
二、使用uView組件
<template> <view class="wrap"> <text>滑動(dòng)頁(yè)面,返回頂部按鈕將出現(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頁(yè)面回到頂部?jī)煞N實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)uniapp頁(yè)面回到頂部?jī)?nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
javascript中簡(jiǎn)單的進(jìn)制轉(zhuǎn)換代碼實(shí)例
這篇文章介紹了javascript中簡(jiǎn)單的進(jìn)制轉(zhuǎn)換代碼實(shí)例,有需要的朋友可以參考一下2013-10-10JS 中document.write()的用法和清空的原因淺析
這篇文章主要介紹了JS 中document.write()的用法和清空的原因淺析,需要的朋友可以參考下2017-12-12js字符串替換所有的指定字符或文字(推薦replaceAll方法)
要實(shí)現(xiàn)js字符串替換所有的某個(gè)字符,推薦大家使用replaceAll方法,默認(rèn)不是所有瀏覽器都兼容,所以這里給出一個(gè)解決方案,需要的朋友可以參考下2014-07-07JS實(shí)現(xiàn)兩個(gè)大數(shù)(整數(shù))相乘
大數(shù),即超出語(yǔ)言所能表示的數(shù)字最大范圍的數(shù)字,那么如何實(shí)現(xiàn)兩個(gè)大數(shù)相乘呢?下面有個(gè)不錯(cuò)的方法,大家可以參考下2014-04-04javascript將數(shù)組插入到另一個(gè)數(shù)組中的代碼
下面的代碼主要功能就是將數(shù)組arr2插入到數(shù)組arr1的index位置,需要的朋友可以參考下2013-01-01