本文實(shí)例為大家分享了vue回到頂部監(jiān)聽滾動(dòng)事件,供大家參考,具體內(nèi)容如下
鼠標(biāo)滾到到頁(yè)面中間出現(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ù)是控制動(dòng)畫快慢的 type: Number, default: 50 } }, data() { return { isActive: false, showIndex:0//默認(rèn)顯示下標(biāo) } }, methods: { toTop(i) { //參數(shù)i表示間隔的幅度大小,以此來(lái)控制速度 document.documentElement.scrollTop -= i; if (document.documentElement.scrollTop > 0) { var c = setTimeout(() => this.toTop(i), 16); } else { clearTimeout(c); } }, handleScroll() { //獲取滾動(dòng)距頂部的距離,顯示 let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop; if (scrollTop > 60) { this.isActive = true; } else { this.isActive = false; } }, mouseOver(index) { //鼠標(biāo)移到哪個(gè)工具上,對(duì)應(yīng)內(nèi)容顯示出來(lái) this.showIndex = index; }, mouseLeave(){ //鼠標(biāo)移出工具區(qū)域后1秒,內(nèi)容區(qū)域消失 let timer = setTimeout(() => { this.showIndex = 0; clearTimeout(timer) }, 500); } }, mounted: function () { window.addEventListener('scroll', this.handleScroll, true); // 監(jiān)聽(綁定)滾輪滾動(dòng)事件 }, destroyed() { window.removeEventListener('scroll', this.handleScroll); //離開頁(yè)面需要移除這個(gè)監(jiān)聽的事件 }}</script>如果遇到scroll一直打印是0,看是否樣式寫了overflow:auto去掉即可;或者給父級(jí)撐滿屏幕;
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注