vue如何判斷組件進入可視區(qū)域
更新時間:2023年10月21日 16:47:49 作者:文i
這篇文章主要介紹了vue如何判斷組件進入可視區(qū)域問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
vue判斷組件進入可視區(qū)域
1、mounted 監(jiān)聽 監(jiān)聽元素是否進入/移出可視區(qū)域
window.addEventListener("scroll", this.scrollHandle, true); // 監(jiān)聽 監(jiān)聽元素是否進入/移出可視區(qū)域2、 methods 執(zhí)行事件
scrollHandle() {
const offset = this.$el.getBoundingClientRect();
const offsetTop = offset.top;
const offsetBottom = offset.bottom;
// const offsetHeight = offset.height;
// 進入可視區(qū)域
// console.log(offsetTop,offsetBottom)
if (offsetTop <= window.innerHeight && offsetBottom >= 0) {
// console.log('進入可視區(qū)域');
} else {
// console.log('移出可視區(qū)域');
}
}3、記得在適當?shù)臅r候移除事件監(jiān)聽
window.removeEventListener('scroll', this.scrollHandle, true);vue判斷組件是否出現(xiàn)在可視區(qū),使用動畫
<div class="real-time-data">
<h1 class="title">實時招生數(shù)據(jù)</h1>
<div class="detail">結合三大平臺考生行為數(shù)據(jù),為入駐高校實時分析當前報考情況,并精準預測當年招生趨勢</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)聽元素是否進入/移出可視區(qū)域
},
methods: {
scrollHandle() {
const offset = this.$el.getBoundingClientRect();
const offsetTop = offset.top;
const offsetBottom = offset.bottom;
// const offsetHeight = offset.height;
// 進入可視區(qū)域
// console.log(offsetTop,offsetBottom)
if (offsetTop <= window.innerHeight && offsetBottom >= 0) {
// console.log('進入可視區(qū)域');
this.isShowAnimation = true
} else {
this.isShowAnimation = false
// console.log('移出可視區(qū)域');
}
}
}
}
//動畫
.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);
}
}
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
vue3使用threejs實現(xiàn)3D卡片水平旋轉效果的示例代碼
這篇文章主要介紹了在vue3中使用threejs實現(xiàn)3D卡片水平旋轉效果,文中通過代碼示例講解的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下2024-04-04
vue.js 實現(xiàn)a標簽href里添加參數(shù)
今天小編就為大家分享一篇vue.js 實現(xiàn)a標簽href里添加參數(shù),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11
vue.js與element-ui實現(xiàn)菜單樹形結構的解決方法
本文通過實例給大家介紹了vue.js與element-ui實現(xiàn)菜單樹形結構,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2018-04-04
vue監(jiān)聽滾動事件實現(xiàn)滾動監(jiān)聽
本文主要介紹了vue監(jiān)聽滾動事件實現(xiàn)滾動監(jiān)聽的相關資料。具有很好的參考價值。下面跟著小編一起來看下吧2017-04-04

