当前位置:网站首页>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)
}
})
}
},
}
边栏推荐
- Case sharing | integrated construction of data operation and maintenance in the financial industry
- Redis cache
- 类方法和类变量的使用
- Redis transaction
- Redis 排查大 key 的3种方法,优化必备
- 案例分享|金融业数据运营运维一体化建设
- Caduceus从未停止创新,去中心化边缘渲染技术让元宇宙不再遥远
- TCP shakes hands three times and waves four times. Do you really understand?
- 2022 version of stronger jsonpath compatibility and performance test (snack3, fastjson2, jayway.jsonpath)
- Cloudcompare & open3d DBSCAN clustering (non plug-in)
猜你喜欢
CloudCompare&Open3D DBSCAN聚类(非插件式)
Enlightenment of maker thinking in Higher Education
什么是商业智能(BI),就看这篇文章足够了
Analyzing the maker space contained in steam Education
创客思维在高等教育中的启迪作用
VS2019 C# release下断点调试
Arcgis 10.2.2 | arcgis license server无法启动的解决办法
WGCNA analysis basic tutorial summary
Redis 排查大 key 的3种方法,优化必备
[leetcode] 17. Letter combination of telephone number
随机推荐
Enlightenment of maker thinking in Higher Education
Caduceus从未停止创新,去中心化边缘渲染技术让元宇宙不再遥远
QT - plot other problems
面试官:说说XSS攻击是什么?
LambdaQueryWrapper用法
Golang面试整理 三 简历如何书写
2022 version of stronger jsonpath compatibility and performance test (snack3, fastjson2, jayway.jsonpath)
Delphi SOAP WebService 服务器端多个 SoapDataModule 实现相同的接口方法,接口继承
GTEST from ignorance to proficiency (4) how to write unit tests with GTEST
每日一题-LeetCode556-下一个更大元素III-字符串-双指针-next_permutation
2021 CCPC Harbin I. power and zero (binary + thinking)
【微信小程序】协同工作与发布
How was MP3 born?
[public class preview]: basis and practice of video quality evaluation
奋斗正当时,城链科技战略峰会广州站圆满召开
Cloudcompare & open3d DBSCAN clustering (non plug-in)
置信区间的画法
Interviewer: what is XSS attack?
Acwing 2022 daily question
Jerry's ad series MIDI function description [chapter]