当前位置:网站首页>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)
}
})
}
},
}
边栏推荐
- [public class preview]: basis and practice of video quality evaluation
- Redis has three methods for checking big keys, which are necessary for optimization
- [wechat applet] collaborative work and release
- 创客思维在高等教育中的启迪作用
- 解析互联网时代的创客教育技术
- Cadeus has never stopped innovating. Decentralized edge rendering technology makes the metauniverse no longer far away
- 2022 version of stronger jsonpath compatibility and performance test (snack3, fastjson2, jayway.jsonpath)
- 学习突围3 - 关于精力
- Redis bloom filter
- MongoDB中的索引操作总结
猜你喜欢

IIC (STM32)

Shutter textfield example
![[C language] deep understanding of symbols](/img/4b/26cf10baa29eeff08101dcbbb673a2.png)
[C language] deep understanding of symbols

奋斗正当时,城链科技战略峰会广州站圆满召开

Maya lamp modeling

Three or two things about the actual combat of OMS system

Interpreting the development of various intelligent organizations in maker Education

Maidong Internet won the bid of Beijing life insurance

从RepVgg到MobileOne,含mobileone的代码

Word文档中标题前面的黑点如何去掉
随机推荐
Keep on fighting! The city chain technology digital summit was grandly held in Chongqing
Delphi SOAP WebService 服务器端多个 SoapDataModule 实现相同的接口方法,接口继承
Shutter textfield example
How much is the minimum stock account opening commission? Is it safe to open an account online
minidom 模块写入和解析 XML
Daily question -leetcode1200- minimum absolute difference - array - sort
Cadeus has never stopped innovating. Decentralized edge rendering technology makes the metauniverse no longer far away
电话加密,中间4为****代替
【活动早知道】LiveVideoStack近期活动一览
2022 version of stronger jsonpath compatibility and performance test (snack3, fastjson2, jayway.jsonpath)
QT - double buffer plot
Acwing 2022 daily question
At the right time, the Guangzhou station of the city chain science and Technology Strategy Summit was successfully held
Word文档中标题前面的黑点如何去掉
应用实践 | 蜀海供应链基于 Apache Doris 的数据中台建设
输入的查询SQL语句,是如何执行的?
PostgreSQL基本结构——表
Jerry's ad series MIDI function description [chapter]
Golang面试整理 三 简历如何书写
Drop down selection of Ehlib database records