vue判斷內(nèi)容是否滑動到底部的三種方式
方式一:直接給滾動的部分加個 @scroll="handleScroll" 然后js里面進行業(yè)務(wù)處理
<div class="tip-info" @scroll="handleScroll">
<div class="tip-blank" :key="outerIndex" v-for="(item, outerIndex) in htmlCaption">
</div>methods: {
// 滾動事件
handleScroll(event) {
const dialog = event.target;
if (dialog.scrollHeight - dialog.scrollTop === dialog.clientHeight) {
// 當(dāng)內(nèi)容滑動到底部時,執(zhí)行想要的操作
}
}
}方式二:可以采用給滾動內(nèi)容,在最后一個內(nèi)容的div后面追加一個新的元素,然后IntersectionObserver 進行觀察
<div class="tip-info">
<div class="tip-blank" :key="outerIndex" v-for="(item, outerIndex) in htmlCaption">
</div>mounted() {
this.addNewElementToTipBlank();
},
methods: {
addNewElementToTipBlank() {
// 創(chuàng)建新元素
const newElement = document.createElement('div');
newElement.className = 'tip-box';
newElement.textContent = 'New Tip Box Added';
// 找到 tip-blank 類所在的 div 元素
const tipBlankDivs = document.querySelectorAll('.tip-blank');
const lastTipBlankDiv = tipBlankDivs[tipBlankDivs.length - 1]; // 獲取最后一個 tip-blank 元素
// 在最后一個 tip-blank 元素后面插入新的 div 元素
if (lastTipBlankDiv) {
lastTipBlankDiv.insertAdjacentElement('afterend', newElement);
}
// 創(chuàng)建一個觀察者實例
const observer = new IntersectionObserver((entries) => {
console.log(entries);
entries.forEach((entry) => {
// entry.isIntersecting 判斷目標(biāo)元素是否在視口中
if (entry.isIntersecting) {
console.log('目標(biāo)元素在視口中!');
}
else {
console.log('目標(biāo)元素不在視口中.');
}
});
});
// 開始觀察某個元素
const targetElement = document.querySelector('.tip-box');
if (targetElement) {
observer.observe(targetElement);
}
// 停止觀察
// 如果你不再需要觀察某個元素,你可以調(diào)用:
observer.unobserve(targetElement);
// 如果你想停止觀察所有元素,你可以調(diào)用:
observer.disconnect();
},
}IntersectionObserver具體的用法:
IntersectionObserver 是一個現(xiàn)代的瀏覽器 API,允許開發(fā)者在某個元素與其祖先元素或頂層文檔視口發(fā)生交叉時得到通知。它非常適合實現(xiàn)圖片懶加載、無限滾動、廣告曝光率等功能。
1. 瀏覽器的兼容性
IntersectionObserver目前在大多數(shù)現(xiàn)代瀏覽器中都得到了支持。但是在一些老版本的瀏覽器,如 IE 中,則沒有支持。點擊查看 IntersectionObserver 的兼容性
2. 如何使用?
const observer = new IntersectionObserver((entries, observer) => {
entries.forEach(entry => {
// entry.isIntersecting 判斷目標(biāo)元素是否在視口中
if (entry.isIntersecting) {
console.log('目標(biāo)元素在視口中!');
} else {
console.log('目標(biāo)元素不在視口中.');
}
});
});
// 開始觀察某個元素
const targetElement = document.querySelector('.some-class');
observer.observe(targetElement);
// 停止觀察
// 如果你不再需要觀察某個元素,你可以調(diào)用:
observer.unobserve(targetElement);
// 如果你想停止觀察所有元素,你可以調(diào)用:
observer.disconnect();
// 配置選項
當(dāng)創(chuàng)建 IntersectionObserver 實例時,你可以提供一個配置對象,該對象有以下屬性:
const options = {
root: document.querySelector('.scroll-container'), // 觀察目標(biāo)的父元素,如果不設(shè)置,默認(rèn)為瀏覽器視口
rootMargin: '10px', // 增加或減少觀察目標(biāo)的可見區(qū)域大小
threshold: [0, 0.25, 0.5, 0.75, 1] // 當(dāng)觀察目標(biāo)的可見比例達到這些閾值時會觸發(fā)回調(diào)函數(shù)
};
const observer = new IntersectionObserver(callback, options);3. 一些常見的應(yīng)用場景
// 圖片懶加載
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const img = entry.target;
img.src = img.dataset.lazy;
observer.unobserve(img);
}
});
});
document.querySelectorAll('img[data-lazy]').forEach(img => {
observer.observe(img);
});
// 無線滾動加載
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
loadMoreContent(); // 你的加載更多內(nèi)容的函數(shù)
observer.unobserve(entry.target); // 如果你只想加載一次,你可以在這里停止觀察
}
});
});
const loadMoreTrigger = document.querySelector('.load-more-trigger');
observer.observe(loadMoreTrigger);方式三 如果前2種方式不可行,可試試這一種
<template>
<div class="tip-info" @scroll.passive="handleScroll">
<div class="sn-f-c-c tip-blank" :key="i" v-for="(item, i) in caption">
{{item}}
</div>
</div>
</template>
<script>
data() {
return {
caption: []
};
},
methods: {
// 接口返回數(shù)據(jù)
interface() {
this.caption = ''; // 接口返回數(shù)據(jù)
if (this.caption.length > 0) {
this.$nextTick(() => {
this.handleScroll({
target: document.querySelector('.tip-info')
});
});
}
},
handleScroll(e) {
const { scrollTop, clientHeight, scrollHeight } = e.target;
// 條件判斷(scrollHeight - (scrollTop + clientHeight)) / clientHeight <= 0.05
// 是在計算滾動條距離底部的距離與可視區(qū)域高度的比例。如果這個比例小于或等于5%(0.05),則認(rèn)為滾動條已經(jīng)非常接近底部。
if ((scrollHeight - (scrollTop + clientHeight)) / clientHeight <= 0.05) {
console.log('內(nèi)容到底了');
}
}
}
</script>以上就是vue判斷內(nèi)容是否滑動到底部的三種方式的詳細內(nèi)容,更多關(guān)于vue判斷內(nèi)容是否滑動到底部的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
分分鐘學(xué)會vue中vuex的應(yīng)用(入門教程)
本篇文章主要介紹了vue中vuex的應(yīng)用(入門教程),詳細的介紹了vuex.js和應(yīng)用方法,有興趣的可以了解一下2017-09-09
vue中Element-ui 輸入銀行賬號每四位加一個空格的實現(xiàn)代碼
我們在輸入銀行賬號會設(shè)置每四位添加一個空格,輸入金額,每三位添加一個空格。那么,在vue,element-ui 組件中,如何實現(xiàn)呢?下面小編給大家?guī)砹藇ue中Element-ui 輸入銀行賬號每四位加一個空格的實現(xiàn)代碼,感興趣的朋友一起看看吧2018-09-09
vue radio單選框,獲取當(dāng)前項(每一項)的value值操作
這篇文章主要介紹了vue radio單選框,獲取當(dāng)前項(每一項)的value值操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
vue3的defineExpose宏函數(shù)是如何暴露方法給父組件使用
當(dāng)子組件使用setup后,父組件就不能像vue2那樣直接就可以訪問子組件內(nèi)的屬性和方法,這個時候就需要在子組件內(nèi)使用defineExpose宏函數(shù)來指定想要暴露出去的屬性和方法,本文介紹vue3的defineExpose宏函數(shù)是如何暴露方法給父組件使用,需要的朋友可以參考下2024-05-05

