当前位置:网站首页>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)
}
})
}
},
}
边栏推荐
- 淘宝商品评价api接口(item_review-获得淘宝商品评论API接口),天猫商品评论API接口
- Open3d surface normal vector calculation
- 解析steam教育中蕴含的众创空间
- QT—绘制其他问题
- GTEST from ignorance to proficient use (2) what is test fixture
- TCP三次握手,四次挥手,你真的了解吗?
- WGCNA analysis basic tutorial summary
- 面试官:说说XSS攻击是什么?
- Lambdaquerywrapper usage
- Delphi SOAP WebService 服务器端多个 SoapDataModule 实现相同的接口方法,接口继承
猜你喜欢

MP3是如何诞生的?

Shutter textfield example

一文掌握数仓中auto analyze的使用

【公开课预告】:视频质量评价基础与实践

QT - plot other problems

Analysis of maker education technology in the Internet Era
![Compréhension approfondie du symbole [langue C]](/img/4b/26cf10baa29eeff08101dcbbb673a2.png)
Compréhension approfondie du symbole [langue C]

How to use concurrentlinkedqueue as a cache queue

Cloudcompare & open3d DBSCAN clustering (non plug-in)

QT - double buffer plot
随机推荐
MP3是如何诞生的?
CloudCompare&Open3D DBSCAN聚类(非插件式)
MYSQL 用!=查询不出等于null的数据,解决办法
每日一题-LeetCode556-下一个更大元素III-字符串-双指针-next_permutation
[public class preview]: basis and practice of video quality evaluation
el-tree结合el-table,树形添加修改操作
Le module minidom écrit et analyse XML
ArcGIS 10.2.2 | solution to the failure of ArcGIS license server to start
QT—双缓冲绘图
Operation of adding material schedule in SolidWorks drawing
How was MP3 born?
面试官:说说XSS攻击是什么?
HDU - 2859 Phalanx(DP)
Bookmark
网上开户哪家证券公司佣金最低,我要开户,网上开户安全吗
【C語言】符號的深度理解
解析steam教育中蕴含的众创空间
How to remove the black dot in front of the title in word document
Flink1.13 SQL basic syntax (I) DDL, DML
WGCNA analysis basic tutorial summary