<template>
<view>
<view class="btn" @tap="toTop" :style="{'display':(flag===false? 'none':'block')}">
<text class="cuIcon-top"></text>
</view>
</view>
</template>
<script>
export default {
name: "upTop",
data() {
return {
flag: false,
}
},
methods: {
// 返回頂部
toTop() {
uni.pageScrollTo({
scrollTop: 0,
duration: 100,
});
},
onPageScroll(e) { //根據距離頂部距離是否顯示回到頂部按鈕
if (e.scrollTop > 10) { //當距離大於10時顯示回到頂部按鈕
this.flag = true
} else {
this.flag = false
}
}
}
}
</script>
<style>
.btn {
position: fixed;
z-index: 9999;
right: 16px;
bottom: 100px;
background-color: #007AFF;
padding: 5px;
display: none;
border-radius: 4px;
}
.btn .cuIcon-top {
font-size: 30px;
color: #FFFFFF;
}
</style>