vue——回到顶部监听滚动事件
生活随笔
收集整理的這篇文章主要介紹了
vue——回到顶部监听滚动事件
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
鼠標(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)畫(huà)快慢的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)聽(tīng)(綁定)滾輪滾動(dòng)事件},destroyed() {window.removeEventListener('scroll', this.handleScroll); //離開(kāi)頁(yè)面需要移除這個(gè)監(jiān)聽(tīng)的事件}} </script>如果遇到scroll一直打印是0,看是否樣式寫(xiě)了overflow:auto去掉即可;或者給父級(jí)撐滿(mǎn)屏幕;
總結(jié)
以上是生活随笔為你收集整理的vue——回到顶部监听滚动事件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 海湾5000汉字编码app
- 下一篇: 字体编码—Unicode16进制编码转字