【问题标题】:On and off animation with css - better on animation使用 css 开启和关闭动画 - 动画效果更好
【发布时间】:2015-08-13 20:52:42
【问题描述】:

我正在玩here 的开/关监视器效果,虽然我的动画不是在开关上,而是在窗口滚动上。我是这样设置的:

$(window).on('scroll', function () {
    var sctop = $(this).scrollTop();
    var element_top = $('.image_animation').offset().top;
    if (sctop > element_top) {
        $('.image_animation').addClass('off');
    } else {
        $('.image_animation').removeClass('off');
    }
});
.before_content {
    height: 300px;
}
.after_content {
    height: 800px;
}
.container {
    position: relative;
}
.image_animation {
    display: inline-block;
    position: relative;
}
.image_animation img {
    z-index: 100;
}
.image_animation .background_image {
    width: 94%;
    height: 73%;
    z-index: -1;
    position: absolute;
    top: 4%;
    left: 3%;
    -webkit-background-size: cover!important;
    background-size: cover!important;
    background-repeat: no-repeat!important;
    animation: imac 10s linear 2s infinite;
}
@keyframes turn-off {
    0% {
        transform: scale(1, 1.3) translate3d(0, 0, 0);
        -webkit-filter: brightness(1);
        filter: brightness(1);
        opacity: 1;
    }
    60% {
        transform: scale(1, 0.001) translate3d(0, 0, 0);
        -webkit-filter: brightness(10);
        filter: brightness(10);
    }
    100% {
        animation-timing-function: cubic-bezier(0.755, 0.05, 0.855, 0.06);
        transform: scale(0, 0.0001) translate3d(0, 0, 0);
        -webkit-filter: brightness(50);
        filter: brightness(50);
    }
}
.image_animation.off > .background_image {
    animation: turn-off 0.55s cubic-bezier(0.23, 1, 0.32, 1);
    animation-fill-mode: forwards;
}
.image_animation:before {
    content:"";
    width: 94%;
    height: 73%;
    z-index: -1;
    position: absolute;
    top: 4%;
    left: 3%;
    background: #000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="before_content"></div>
<div class="container">
    <div class="image_animation">
        <img src="http://i.imgur.com/DL7AUy3.png" alt="">
        <div class="background_image" style="background:url(http://i.imgur.com/VRcQKtY.jpg);"></div>
    </div>
</div>
<div class="after_content"></div>

关闭动画效果很好,但打开动画有问题。我不需要像原始代码笔那样花哨的东西,淡入就足够了。我尝试添加类on,然后用它来启动某种css更改,但没有运气。我也尝试过动画,但效果不佳。目前我的图片刚刚出现,有点蹩脚。

知道如何在重新滚动时进行这种转换吗?

【问题讨论】:

    标签: jquery html css animation


    【解决方案1】:

    为要添加的.on 类添加额外样式

    .image_animation.on > .background_image {
        animation: turn-off 0.55s cubic-bezier(0.23, 1, 0.32, 1) reverse;
        animation-fill-mode: backwards;
    }
    

    并将您的 javascript 更改为

     $(window).on('scroll', function () {
         var sctop = $(this).scrollTop();
         var $image_animation = $('.image_animation');
         var element_top = $image_animation.offset().top;
         if (sctop > element_top) {
             $image_animation.removeClass('on');
             $image_animation[0].offsetWidth = $image_animation[0].offsetWidth;
             $image_animation.addClass('off');
         } else {
             $image_animation.removeClass('off');
             $image_animation[0].offsetWidth = $image_animation[0].offsetWidth;
             $image_animation.addClass('on');
         }
     });
    

    我还更改了0% 关键帧以使动画看起来更好

    0% {
        transform: scale(1, 1) translate3d(0, 0, 0); 
        -webkit-filter: brightness(1);
        filter: brightness(1);
        opacity: 1;
    }
    

    请注意,现在比例为 (1, 1),因此反向看起来更好。

    offsetWidth 行是刷新动画的小技巧,因此它们不会出现更改问题。

    去看看

    https://jsfiddle.net/hh7r4jes/1/

    【讨论】:

    • 如果我处于严格模式,我无法使用offsetWidth hack,有什么办法可以解决吗?
    • 你有没有试过把它注释掉,看看它在严格模式下是否可以工作?
    • 是的,效果不太好,图像在启动时闪烁,并将图像拉伸到监视器容器之外。
    • 你是否像我告诉你的那样改变了css? transform: scale(1, 1) translate3d(0, 0, 0); 对阻止图像伸出容器很重要
    • 是的,不行。问题是它会在您开始滚动时添加on 类。
    猜你喜欢
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 2017-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多