2 回答
TA贡献2012条经验 获得超12个赞
下面是使用交集观察者 API 的简单示例
const inViewport = (entries, observer) => {
entries.forEach(entry => {
entry.target.classList.toggle("active", entry.isIntersecting);
});
};
let observer = new IntersectionObserver(inViewport);
observer.observe(document.querySelector('#box'));
body { height:300vh; }
#box { width:100px; height:100px; position:absolute; top:150vh; background:red; }
/* InViewport transitions */
[data-inviewport="fade-rotate"] { /* Default state */
transition: 2s;
opacity: 0.1;
}
[data-inviewport="fade-rotate"].active { /* Active state */
transform: rotate(180deg);
opacity: 1;
}
Scroll down...
<div id="box" data-inviewport="fade-rotate"></div>
TA贡献1806条经验 获得超5个赞
当元素进入页面底部时,将触发此操作:
if(element.getBoundingClientRect().top <= window.innerHeight)
当元素丰富屏幕中间时,将触发
if(element.getBoundingClientRect().top <= window.innerHeight / 2)
当然,所有事件都应该在窗口事件上onscroll
添加回答
举报
