简体   繁体   English

当元素到达视口的一半时,我可以触发 animation 吗?

[英]Can I trigger an animation when an element reaches half way in the viewport?

(JavaScript) I can see that it is possible to trigger an animation using an IntersectionObserver when an element enters and leaves the viewport - but is there a way to trigger an animation when an element reaches half way in the view port? (JavaScript)我可以看到当元素进入和离开视口时使用 IntersectionObserver 触发 animation 是可能的 - 但是当元素到达视口的一半时有没有办法触发 animation?

you can use something like你可以使用类似的东西

new IntersectionObserver(yourAnimation, {rootMargin: "0px 0px -50% 0px"})

your element will be intersecting when it enters the top half of the viewport.当你的元素进入视口的上半部分时,它就会相交。 basically adding a minus margin of half its height to the bottom edge of your root (here viewport)基本上将其高度的一半的负边距添加到根的底部边缘(这里是视口)

You can use您可以使用

if(elment.offsetLeft==window.offsetWidth) {startAnimation()}
// or element.offsetTop and window.offsetHeight
//you should write the startAnimation function, it not a built-in function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM