简体   繁体   English

进入视口时触发 CSS 动画

[英]Trigger CSS animation when it comes into viewport

I'm trying to make a border around a bootstrap 4 circular image.我正在尝试在 bootstrap 4 圆形图像周围制作边框。 I want it to animate like the border in the codepen example below, but I want it to load when the user scrolls down.我希望它像下面 codepen 示例中的边框一样动画,但我希望它在用户向下滚动时加载。 As I understand it I need to use Javascript to trigger it, but I'm not sure on how I can do that.据我了解,我需要使用 Javascript 来触发它,但我不确定如何做到这一点。 Can someone help me?有人能帮我吗?

https://codepen.io/katmai7/pen/jCAhv https://codepen.io/katmai7/pen/jCAhv

<div class="wrap">
<div class="circle">
   <i class="icon i1 icon-terminal"></i>
   <i class="icon i2 icon-code-fork"></i>
   <i class="icon i3 icon-keyboard"></i>
   <i class="icon i4 icon-code"></i>
   <div class="line1"></div>
   <div class="line2"></div>
   <span class="text">hover on me</span>
 </div>
</div>

You can detect the scroll using您可以使用检测滚动

window.onscroll = function(e) {
    // Scroll up or down
}

The animation appears when动画出现时

&:hover{
    animation: border .4s ease 1 forwards;
    cursor: none;
    [...]

So you'll need to add all that CSS to a new id and when you detect scroll add it to your element using JavaScript:因此,您需要将所有 CSS 添加到新 id 中,并在检测到滚动时使用 JavaScript 将其添加到您的元素中:

.newClass{
    animation: border .4s ease 1 forwards;
    cursor: none;
    [...]

Add it:添加它:

var element = document.getElementById("element-id");
element.className += " newClass";

The animation-fill-mode property specifies a style for the element when the animation is not playing (before it starts, after it ends, or both). animation-fill-mode 属性指定动画未播放时元素的样式(开始前、结束后或两者)。 CSS animations do not affect the element before the first keyframe is played or after the last keyframe is played. CSS 动画不会在播放第一个关键帧之前或播放最后一个关键帧之后影响元素。

You can use the new Intersection observer api https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API where a polyfill is available here: https://github.com/w3c/IntersectionObserver/tree/master/polyfill您可以使用新的 Intersection 观察者 api https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API ,这里有一个 polyfill: https : //github.com/w3c/IntersectionObserver/tree/主/polyfill

the code would be something like this:代码将是这样的:

let options = {
root: document.querySelector('.wrap'),
rootMargin: '0px',
threshold: 1.0
}
let element = document.querySelector('.circle')
let observer = new IntersectionObserver(() => element.classList.add('onviewport') , options);

also you should modify your css to not listen on hover triggers but "respond" to a class : modifying你也应该修改你的 css 来不监听悬停触发器而是“响应”一个类:修改

&:hover{

to

&.onviewport {

If you don't want to use the Intersection observer api you can use a plugin like this one https://github.com/imakewebthings/waypoints , the logic would still be similar.如果您不想使用 Intersection 观察者 api,您可以使用这样的插件https://github.com/imakewebthings/waypoints ,逻辑仍然是相似的。

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

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