首页 > 解决方案 > 使背景动画响应的问题

问题描述

我为我的背景制作了一个动画,但没有响应,我不知道如何制作它。我使用 Bootstrap 4 和 CSS 作为我的主要设计资源。

我起诉的CSS如下:

/* Animation GLitch */
.animation {
    position: relative;
    width: 100%;
    height: 100vh;
    background: url("https://images.unsplash.com/photo-1487174244970-cd18784bb4a4?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2252&q=80");
    max-width: 100%;
    max-height: 100%;
    margin: 0 auto;
    overflow: hidden;
}
.animation:before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: url("https://images.unsplash.com/photo-1487174244970-cd18784bb4a4?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2252&q=80");
    opacity: 0.5;
    mix-blend-mode: hard-light;
    display: block;
}
.animation:hover:before {
    animation: animate 0.2s linear infinite;
}
@keyframes animate {
    0% {
        background-position: 0 0;
        filter: hue-rotate(0deg);
    }
    10% {
        background-position: 5px 0;
    }
    20% {
        background-position: -5px 0;
    }
    30% {
        background-position: 15px 0;
    }
    40% {
        background-position: -5px 0;
    }
    50% {
        background-position: -25px 0;
    }
    60% {
        background-position: -50px 0;
    }
    70% {
        background-position: 0 -20px;
    }
    80% {
        background-position: -60px -20px;
    }
    81% {
        background-position: 0 0;
    }
    100% {
        background-position: 0 0;
        filter: hue-rotate(360deg);
    }
}

动画由 2 张相同的图片组成,它们会产生这种故障效果,但我不知道如何让它响应。

它在这里工作的片段: https ://codepen.io/Jakub41/pen/abbwxWb

我尝试使用大小,但它正在修改我的效果我想在所有屏幕上保持相同的效果

标签: css

解决方案


我不太明白您希望它以哪种方式响应,但尝试在background: url(...)两次之后添加这些行:

background-repeat: no-repeat;
background-size: cover;

在这里查看有关该background-size物业的更多信息。

您也可能对这个问题的第二个答案感兴趣。


推荐阅读