当前位置:网站首页>New intersectionobserver usage notes
New intersectionobserver usage notes
2022-07-04 21:51:00 【Love front end not love love】
MDN API
With the development of web pages , To detect a ( some ) There are more and more requirements related to whether elements appear in the visual window ? such as :
When the page scrolls , Lazy loading pictures or other content .
Realization “ Unlimited scrolling ” Website , That is, when users scroll the web page, they directly load more content , No need to turn the page .
Some elements are exposed by buried point
Scroll to the corresponding area to perform the corresponding animation or other tasks .
all the time , It is not easy to detect the visual state of an element or the relative visual state of two elements , Most solutions are not entirely reliable , The implementation method is ugly , It is also very easy to slow down the performance of the entire website .
IntersectionObserver That's why it happened
demand : Some elements are exposed by buried point
reportDatail() Method Listen to the contents of the container
If dom Initialization exists It's in mounted() Just listen ;
If dom It's a component It's based on Requested returned data Then render the component dom Then this method is used after the request returns
asiosDemo(){
... request
if(success) {
this.list = data // Returned data then list Render components
this.$nextTick(() => {
this.reportDatail()
})
}
}
methods:{
reportContent(index, name) {
console.log(' Exposure content request ', index, name)
},
// Yes dom monitor
reportDatail() {
// Containers dom
const dom = document.querySelector('.monitor-list')
setTimeout((_) => {
this.$_reportContent(
{
observeNodes: dom,
childNodes: dom.querySelectorAll('.content-task-item') // Listen for child elements dom
},
// Callback function Bury the returned data
this.reportContent
)
}, 0)
},
$_reportContent(options, callBackFn) {
// observeNodes Container elements dom
// childNodes Monitored exposure sub elements dom
const {
observeNodes, childNodes } = options
const io = new IntersectionObserver(
function(entrier) {
entrier.forEach((el, i) => {
if (el.isIntersecting) {
// _index _name It's a child element dom Created value
let _index = el.target.getAttribute('data-index')
let _name = el.target.getAttribute('data-name')
callBackFn(_index, _name)
// Unbind the monitored elements
io.unobserve(el.target)
}
})
},
{
root: observeNodes,
rootMargin: '0px'
}
)
if (childNodes && childNodes.length !== 0) {
childNodes.forEach((el) => {
let _index = el.getAttribute('data-index') * 1
// this.exposureList Record the latest exposure The previously exposed ones are no longer monitored
if (this.exposureList.includes(_index)) {
io.observe(el)
}
})
}
},
}
边栏推荐
猜你喜欢
Maidong Internet won the bid of Beijing life insurance
TCP三次握手,四次挥手,你真的了解吗?
VS2019 C# release下断点调试
每日一题-LeetCode556-下一个更大元素III-字符串-双指针-next_permutation
Redis03 - network configuration and heartbeat mechanism of redis
创客思维在高等教育中的启迪作用
Lambdaquerywrapper usage
【C语言】符号的深度理解
Master the use of auto analyze in data warehouse
Methods of improving machine vision system
随机推荐
创客思维在高等教育中的启迪作用
QT—绘制其他问题
面试官:说说XSS攻击是什么?
【C语言】符号的深度理解
类方法和类变量的使用
IIC (STM32)
【公开课预告】:视频质量评价基础与实践
[weekly translation go] how to code in go series articles are online!!
Analysis of maker education technology in the Internet Era
Flutter WebView示例
Why does invariant mode improve performance
2021 CCPC Harbin B. magical subsequence (thinking question)
Arcgis 10.2.2 | arcgis license server无法启动的解决办法
Master the use of auto analyze in data warehouse
Caduceus从未停止创新,去中心化边缘渲染技术让元宇宙不再遥远
如何借助自动化工具落地DevOps
股票开户流程是什么?使用同花顺手机炒股软件安全吗?
Analyzing the maker space contained in steam Education
A quick start to fastdfs takes you three minutes to upload and download files to the ECS
Compréhension approfondie du symbole [langue C]