javascript – 如何知道IntersectionObserver滚动方向?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 如何知道IntersectionObserver滚动方向?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2845字,纯文字阅读大概需要5分钟。
内容图文
![javascript – 如何知道IntersectionObserver滚动方向?](/upload/InfoBanner/zyjiaocheng/705/67eb38b211dc4cd48eb34921bf98c165.jpg)
那么,当触发事件时,如何知道滚动方向?
在返回的对象中,我看到的最接近的可能性是与boundingClientRect类型进行交互,保存最后一个滚动位置,但我不知道处理boundingClientRect是否会最终导致性能问题.
是否可以使用交集事件来确定滚动方向(向上/向下)?
我添加了这个基本代码段,所以如果有人可以帮助我.
我会非常感激的.
这是片段:
var options = {
rootMargin: '0px',
threshold: 1.0
}
function callback(entries, observer) {
entries.forEach(entry => {
if (entry.isIntersecting) {
console.log('entry', entry);
}
});
};
var elementToObserve = document.querySelector('#element');
var observer = new IntersectionObserver(callback, options);
observer.observe(elementToObserve);
#element {
margin: 1500px auto;
width: 150px;
height: 150px;
background: #ccc;
color: white;
font-family: sans-serif;
font-weight: 100;
font-size: 25px;
text-align: center;
line-height: 150px;
}
<div id="element">Observed</div>
我想知道这一点,所以我可以在固定标题菜单上应用它来显示/隐藏它
解决方法:
I don’t know if handling boundingClientRect will end up on performance issues.
MDN声明IntersectionObserver不在主线程上运行:
This way, sites no longer need to do anything on the main thread to watch for this kind of element intersection, and the browser is free to optimize the management of intersections as it sees fit.
MDN, “Intersection Observer API”
我们可以通过保存值IntersectionObserverEntry.boundingClientRect.y来计算滚动方向,并将其与之前的值进行比较.
运行以下代码段作为示例:
const state = document.querySelector('.observer__state')
const target = document.querySelector('.observer__target')
const thresholdArray = steps => Array(steps + 1)
.fill(0)
.map((_, index) => index / steps || 0)
let previousY = 0
let previousRatio = 0
const handleIntersect = entries => {
entries.forEach(entry => {
const currentY = entry.boundingClientRect.y
const currentRatio = entry.intersectionRatio
const isIntersecting = entry.isIntersecting
// Scrolling down/up
if (currentY < previousY) {
if (currentRatio > previousRatio && isIntersecting) {
state.textContent ="Scrolling down enter"
} else {
state.textContent ="Scrolling down leave"
}
} else if (currentY > previousY && isIntersecting) {
if (currentRatio < previousRatio) {
state.textContent ="Scrolling up leave"
} else {
state.textContent ="Scrolling up enter"
}
}
previousY = currentY
previousRatio = currentRatio
})
}
const observer = new IntersectionObserver(handleIntersect, {
threshold: thresholdArray(20),
})
observer.observe(target)
html,
body {
margin: 0;
}
.observer__target {
position: relative;
width: 100%;
height: 350px;
margin: 1500px 0;
background: rebeccapurple;
}
.observer__state {
position: fixed;
top: 1em;
left: 1em;
color: #111;
font: 400 1.125em/1.5 sans-serif;
background: #fff;
}
<div class="observer__target"></div>
<span class="observer__state"></span>
如果thresholdArray辅助函数可能会让您感到困惑,那么它会根据给定的步数构建一个介于0.0到1.0之间的数组.传递5将返回[0.0,0.2,0.4,0.6,0.8,1.0].
内容总结
以上是互联网集市为您收集整理的javascript – 如何知道IntersectionObserver滚动方向?全部内容,希望文章能够帮你解决javascript – 如何知道IntersectionObserver滚动方向?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。