当前位置:网站首页>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)
}
})
}
},
}
边栏推荐
- numpy vstack 和 column_stack
- EhLib 数据库记录的下拉选择
- Cadeus has never stopped innovating. Decentralized edge rendering technology makes the metauniverse no longer far away
- 【微信小程序】协同工作与发布
- At the right time, the Guangzhou station of the city chain science and Technology Strategy Summit was successfully held
- 学习突围3 - 关于精力
- Daily question -leetcode1200- minimum absolute difference - array - sort
- Use of redis publish subscription
- new IntersectionObserver 使用笔记
- Application practice | Shuhai supply chain construction of data center based on Apache Doris
猜你喜欢

LambdaQueryWrapper用法

Interpreting the development of various intelligent organizations in maker Education

Lambdaquerywrapper usage

每日一题-LeetCode556-下一个更大元素III-字符串-双指针-next_permutation

WGCNA analysis basic tutorial summary

Shutter textfield example

Case sharing | integrated construction of data operation and maintenance in the financial industry

什么是商业智能(BI),就看这篇文章足够了

Maidong Internet won the bid of Beijing life insurance

QT—绘制其他问题
随机推荐
Solve the problem of data disorder caused by slow asynchronous interface
Golang interview finishing three resumes how to write
Compréhension approfondie du symbole [langue C]
Enlightenment of maker thinking in Higher Education
Daily question -leetcode1200- minimum absolute difference - array - sort
gtest从一无所知到熟练使用(3)什么是test suite和test case
Jerry's ad series MIDI function description [chapter]
Caduceus从未停止创新,去中心化边缘渲染技术让元宇宙不再遥远
Le module minidom écrit et analyse XML
旋变串判断
How to implement Devops with automatic tools
Jerry's ad series MIDI function description [chapter]
Hash table
Acwing 2022 daily question
Redis has three methods for checking big keys, which are necessary for optimization
Flutter 返回按钮的监听
做BI开发,为什么一定要熟悉行业和企业业务?
flink1.13 sql基础语法(一)DDL、DML
Maidong Internet won the bid of Beijing life insurance
Bookmark