Vue實(shí)現(xiàn)返回頂部按鈕實(shí)例代碼
前言
本文主要介紹了Vue 實(shí)現(xiàn)返回頂部按鈕的方法,下面話不多說(shuō),來(lái)直接看代碼吧
實(shí)例代碼:
<template> <div class="scrollTop"> <div class="backTop" @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 }, //滑動(dòng)超過(guò)200時(shí)顯示按鈕 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>
總結(jié)
到此這篇關(guān)于Vue實(shí)現(xiàn)返回頂部按鈕的文章就介紹到這了,更多相關(guān)Vue返回頂部按鈕內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3封裝el-pagination分頁(yè)組件的完整代碼
這篇文章主要介紹了vue3封裝el-pagination分頁(yè)組件的完整代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-02-02vue使用echarts實(shí)現(xiàn)地圖的方法詳解
這篇文章主要為大家詳細(xì)介紹了vue使用echarts實(shí)現(xiàn)地圖的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-03-03一份超級(jí)詳細(xì)的Vue-cli3.0使用教程【推薦】
這篇文章主要介紹了一份超級(jí)詳細(xì)的Vue-cli3.0使用教程,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11Vue Element UI 中 el-table 樹(shù)形數(shù)據(jù) 
這篇文章主要介紹了Vue Element UI 中 el-table 樹(shù)形數(shù)據(jù) tree-props 多層級(jí)使用避坑指南,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-01-01