vue如何判斷組件進(jìn)入可視區(qū)域
vue判斷組件進(jìn)入可視區(qū)域
1、mounted 監(jiān)聽 監(jiān)聽元素是否進(jìn)入/移出可視區(qū)域
window.addEventListener("scroll", this.scrollHandle, true); // 監(jiān)聽 監(jiān)聽元素是否進(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)聽
window.removeEventListener('scroll', this.scrollHandle, true);vue判斷組件是否出現(xiàn)在可視區(qū),使用動(dòng)畫
<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)聽 監(jiān)聽元素是否進(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)畫
.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)文章
vue3使用threejs實(shí)現(xiàn)3D卡片水平旋轉(zhuǎn)效果的示例代碼
這篇文章主要介紹了在vue3中使用threejs實(shí)現(xiàn)3D卡片水平旋轉(zhuǎn)效果,文中通過代碼示例講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-04-04
vue.js 實(shí)現(xiàn)a標(biāo)簽href里添加參數(shù)
今天小編就為大家分享一篇vue.js 實(shí)現(xiàn)a標(biāo)簽href里添加參數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11
vue.js與element-ui實(shí)現(xiàn)菜單樹形結(jié)構(gòu)的解決方法
本文通過實(shí)例給大家介紹了vue.js與element-ui實(shí)現(xiàn)菜單樹形結(jié)構(gòu),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-04-04
ubuntu中利用nginx部署vue項(xiàng)目的完整步驟
Nginx是一款輕量級(jí)的Web服務(wù)器/反向代理服務(wù)器及電子郵件(IMAP/POP3)代理服務(wù)器,在BSD-like 協(xié)議下發(fā)行,下面這篇文章主要給大家介紹了關(guān)于ubuntu中利用nginx部署vue項(xiàng)目的相關(guān)資料,需要的朋友可以參考下2022-02-02
Vue自定義指令實(shí)現(xiàn)卡片翻轉(zhuǎn)功能
這篇文章主要給大家介紹了Vue自定義指令實(shí)現(xiàn)卡片翻轉(zhuǎn)功能的代碼示例,文章通過代碼示例講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的參幫助,需要的朋友可以參考下2023-11-11
vue監(jiān)聽滾動(dòng)事件實(shí)現(xiàn)滾動(dòng)監(jiān)聽
本文主要介紹了vue監(jiān)聽滾動(dòng)事件實(shí)現(xiàn)滾動(dòng)監(jiān)聽的相關(guān)資料。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-04-04

