vue如何判斷組件進(jìn)入可視區(qū)域
vue判斷組件進(jìn)入可視區(qū)域
1、mounted 監(jiān)聽(tīng) 監(jiān)聽(tīng)元素是否進(jìn)入/移出可視區(qū)域
window.addEventListener("scroll", this.scrollHandle, true); // 監(jiān)聽(tīng) 監(jiān)聽(tīng)元素是否進(jìn)入/移出可視區(qū)域2、 methods 執(zhí)行事件
scrollHandle() {
const offset = this.$el.getBoundingClientRect();
const offsetTop = offset.top;
const offsetBottom = offset.bottom;
// const offsetHeight = offset.height;
// 進(jìn)入可視區(qū)域
// console.log(offsetTop,offsetBottom)
if (offsetTop <= window.innerHeight && offsetBottom >= 0) {
// console.log('進(jìn)入可視區(qū)域');
} else {
// console.log('移出可視區(qū)域');
}
}3、記得在適當(dāng)?shù)臅r(shí)候移除事件監(jiān)聽(tīng)
window.removeEventListener('scroll', this.scrollHandle, true);vue判斷組件是否出現(xiàn)在可視區(qū),使用動(dòng)畫(huà)
<div class="real-time-data">
<h1 class="title">實(shí)時(shí)招生數(shù)據(jù)</h1>
<div class="detail">結(jié)合三大平臺(tái)考生行為數(shù)據(jù),為入駐高校實(shí)時(shí)分析當(dāng)前報(bào)考情況,并精準(zhǔn)預(yù)測(cè)當(dāng)年招生趨勢(shì)</div>
<div class="img-box p-relative ">
<img src="@/assets/images/edudata-h5/real-time-data-bg1.webp" alt="" class="bg1">
<img src="@/assets/images/edudata-h5/real-time-data-bg2.webp" alt="" class="bg2 p-absolute" :class="isShowAnimation ? 'img-showBigL' :''">
<img src="@/assets/images/edudata-h5/real-time-data-bg3.webp" alt="" class="bg3 p-absolute" :class="isShowAnimation ? 'img-showBigR' :''">
<img src="@/assets/images/edudata-h5/real-time-data-bg4.webp" alt="" class="bg4 p-absolute" :class="isShowAnimation ? 'img-showBigL' :''">
</div>
</div>
export default {
data() {
return {
isShowAnimation: false
}
},
mounted() {
window.addEventListener("scroll", this.scrollHandle, true); // 監(jiān)聽(tīng) 監(jiān)聽(tīng)元素是否進(jìn)入/移出可視區(qū)域
},
methods: {
scrollHandle() {
const offset = this.$el.getBoundingClientRect();
const offsetTop = offset.top;
const offsetBottom = offset.bottom;
// const offsetHeight = offset.height;
// 進(jìn)入可視區(qū)域
// console.log(offsetTop,offsetBottom)
if (offsetTop <= window.innerHeight && offsetBottom >= 0) {
// console.log('進(jìn)入可視區(qū)域');
this.isShowAnimation = true
} else {
this.isShowAnimation = false
// console.log('移出可視區(qū)域');
}
}
}
}
//動(dòng)畫(huà)
.img-showBigL {
animation: fadeinShowL 1.5s forwards
}
.img-showBigR {
animation: fadeinShowR 1.5s forwards
}
@keyframes fadeinShowL {
0% {
opacity: 0;
transform: translate(100px, 0) scale(0.5);
}
;
100% {
opacity: 1;
transform: translate(0px, 0) scale(1);
}
}
@keyframes fadeinShowR {
0% {
opacity: 0;
transform: translate(-100px, 0) scale(0.5);
}
;
100% {
opacity: 1;
transform: translate(0px, 0) scale(1);
}
}
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue跳轉(zhuǎn)時(shí)根據(jù)url錨點(diǎn)(#xxx)實(shí)現(xiàn)頁(yè)面內(nèi)容定位的方法
本前端仔在做頁(yè)面跳轉(zhuǎn)的時(shí)候,被要求跳轉(zhuǎn)到頁(yè)面時(shí)候,把對(duì)應(yīng)部分的內(nèi)容自動(dòng)滾動(dòng)到頂部,我一開(kāi)始想到的就是根據(jù)錨點(diǎn)<a href="#xxid">進(jìn)行處理,但是發(fā)現(xiàn)不太好實(shí)現(xiàn),于是便自己研究了一個(gè)比較取巧的方法,需要的朋友可以參考下2024-04-04
vue-element-admin項(xiàng)目導(dǎo)入和導(dǎo)出的實(shí)現(xiàn)
組件是Vue.js最強(qiáng)大的功能,這篇文章主要介紹了vue-element-admin項(xiàng)目導(dǎo)入和導(dǎo)出的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
vue二級(jí)菜單導(dǎo)航點(diǎn)擊選中事件的方法
今天小編就為大家分享一篇vue二級(jí)菜單導(dǎo)航點(diǎn)擊選中事件的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
公共Hooks封裝報(bào)表導(dǎo)出useExportExcel實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了公共Hooks封裝報(bào)表導(dǎo)出useExportExcel實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Vue如何實(shí)現(xiàn)u-form多個(gè)form表單同時(shí)校驗(yàn)
在 Vue 項(xiàng)目中使用 UView UI 的 u-form 組件時(shí),多個(gè)表單同時(shí)校驗(yàn)的需求非常常見(jiàn),本文主要介紹了如何使用 u-form 組件實(shí)現(xiàn)多個(gè)表單同時(shí)校驗(yàn),需要的可以參考一下2025-01-01

