vue回到頂部監(jiān)聽滾動事件詳解
更新時間:2019年08月02日 08:29:42 作者:where_slr
這篇文章主要為大家詳細介紹了vue回到頂部監(jiān)聽滾動事件,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了vue回到頂部監(jiān)聽滾動事件,供大家參考,具體內(nèi)容如下
鼠標滾到到頁面中間出現(xiàn)的工具浮框
<template> <div class="tools"> <ul @mouseleave="mouseLeave()"> <li @click="toTop(step)">回到頂部</li> <li @mouseover="mouseOver(1)">QQ</li> <li @mouseover="mouseOver(2)">微信</li> </ul> <div v-if="showIndex === 1">玩QQ</div> <div v-if="showIndex === 2">玩微信</div> </div> </template>
<script> export default { name: 'FloatTools', props: { step: { //此數(shù)據(jù)是控制動畫快慢的 type: Number, default: 50 } }, data() { return { isActive: false, showIndex:0//默認顯示下標 } }, methods: { toTop(i) { //參數(shù)i表示間隔的幅度大小,以此來控制速度 document.documentElement.scrollTop -= i; if (document.documentElement.scrollTop > 0) { var c = setTimeout(() => this.toTop(i), 16); } else { clearTimeout(c); } }, handleScroll() { //獲取滾動距頂部的距離,顯示 let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop; if (scrollTop > 60) { this.isActive = true; } else { this.isActive = false; } }, mouseOver(index) { //鼠標移到哪個工具上,對應(yīng)內(nèi)容顯示出來 this.showIndex = index; }, mouseLeave(){ //鼠標移出工具區(qū)域后1秒,內(nèi)容區(qū)域消失 let timer = setTimeout(() => { this.showIndex = 0; clearTimeout(timer) }, 500); } }, mounted: function () { window.addEventListener('scroll', this.handleScroll, true); // 監(jiān)聽(綁定)滾輪滾動事件 }, destroyed() { window.removeEventListener('scroll', this.handleScroll); //離開頁面需要移除這個監(jiān)聽的事件 } } </script>
如果遇到scroll一直打印是0,看是否樣式寫了overflow:auto去掉即可;或者給父級撐滿屏幕;
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
稍微學一下Vue的數(shù)據(jù)響應(yīng)式(Vue2及Vue3區(qū)別)
這篇文章主要介紹了稍微學一下 Vue 的數(shù)據(jù)響應(yīng)式(Vue2 及 Vue3),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-11-11Vue數(shù)據(jù)驅(qū)動模擬實現(xiàn)3
這篇文章主要為大家詳細介紹了Vue數(shù)據(jù)驅(qū)動模擬實現(xiàn),教大家如何在某個對象中,新增某個屬性,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01Vue中使用正則表達式進行文本匹配和處理的方法小結(jié)
正則表達式在Vue中具有廣泛的應(yīng)用場景,包括文本匹配和處理、表單驗證等,通過本文的介紹和示例,希望讀者能更好地理解和應(yīng)用正則表達式在Vue中的使用方法,感興趣的朋友一起看看吧2023-11-11vue-router beforeEach跳轉(zhuǎn)路由驗證用戶登錄狀態(tài)
這篇文章主要介紹了vue-router beforeEach跳轉(zhuǎn)路由驗證用戶登錄狀態(tài),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12