vue實現(xiàn)移動端返回頂部
本文實例為大家分享了vue實現(xiàn)移動端返回頂部的具體代碼,供大家參考,具體內(nèi)容如下
HTML:
<template> <div class='home'> <div v-for='ys in 100' :key='ys'> <p>1</p> </div> <div @click='back' v-show='isShow'>▲</div> </div></template>
JS:
<script>export default { data() { return { isShow: true }; }, handleScroll() {// handleScroll 和 methods 是同級 if (window.pageYOffset > 300) { //window.pageYOffset:獲取滾動距離 this.isShow = true; } else { this.isShow = false; } // console.log(window.pageYOffset); }, methods: { //點擊事件: back() { //返回頂部 $這個地方需要引入在線jq //<script src='https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js'></script> $('html') .stop() .animate( //animate:動畫 點擊時讓它行動 { scrollTop: 0 //距離頂部為0 }, 1000 //多少時間完成行動 ); } }};</script>
CSS:
<style scoped>.back1 { width: 50px; height: 50px; background: #eee; position: fixed; right: 5px; bottom: 50px; z-index: 1000; text-align: center; line-height: 50px;}</style>
之前小編看到的一篇文章分享給大家:Vue實現(xiàn)返回頂部按鈕
<template> <div class='scrollTop'> <div@click='backTop'> <button v-show='flag_scroll'> 返回頂部 </button> </div> //數(shù)據(jù)源 <div></div> </div></template> <script>export default { name: ’scrollTop’, data() { return { flag_scroll: false, scroll: 0, } }, computed: {}, methods: { //返回頂部事件 backTop() { document.getElementsByClassName(’scrollTop’)[0].scrollTop = 0 }, //滑動超過200時顯示按鈕 handleScroll() { let scrollTop = document.getElementsByClassName(’scrollTop’)[0] .scrollTop console.log(scrollTop) if (scrollTop > 200) { this.flag_scroll = true } else { this.flag_scroll = false } }, }, mounted() { window.addEventListener(’scroll’, this.handleScroll, true) }, created() { },}</script><style scoped>.scrollTop{ width: 100%; height: 100vh; overflow-y: scroll;}.backTop { position: fixed; bottom: 50px; z-index: 100; right: 0; background: white;}</style>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 解決vue頁面刷新,數(shù)據(jù)丟失的問題2. python 讀txt文件,按‘,’分割每行數(shù)據(jù)操作3. python logging.info在終端沒輸出的解決4. vue路由分文件拆分管理詳解5. vue+vuex+axios從后臺獲取數(shù)據(jù)存入vuex,組件之間共享數(shù)據(jù)操作6. Python 忽略文件名編碼的方法7. SpringBoot使用Captcha生成驗證碼8. android studio實現(xiàn)簡單的計算器(無bug)9. android 控件同時監(jiān)聽單擊和雙擊實例10. 詳解android adb常見用法
