Tim*_*Tim 5 html javascript css intersection-observer
我想在元素完全居中时修复它的位置。目前,当它完全可见时,它正在被修复,因此会捕捉到中心。- 我可以以某种方式观察元素何时居中吗?
const box = document.querySelector(".box");
const obs = new IntersectionObserver(entries => {
if (entries[0].isIntersecting) {
box.style.position = "fixed";
box.style.top = "50%";
box.style.transform = "translateY(-50%)"
}
}, {threshold: 1})
obs.observe(box);Run Code Online (Sandbox Code Playgroud)
.v-detail__video {
width: 100vw;
height: 300vh;
display: flex;
padding-top: 70vh;
justify-content: center;
}
.box {
background-color: black;
width: 50vw;
height: 200px;
}Run Code Online (Sandbox Code Playgroud)
<div class="v-detail__video">
<div class="box"></div>
</div>Run Code Online (Sandbox Code Playgroud)