【问题标题】:How do I use CSS3 animate to move the background image of a div up and down?如何使用 CSS3 animate 上下移动 div 的背景图像?
【发布时间】:2025-12-13 07:00:01
【问题描述】:
<div class="abc">
</div>


.abc{
    background: url("blah.jpg") no-repeat;
    width:200px;
    height:200px;
}

我的背景图像是 200 x 600。如何在 div 中上下移动它(我希望 div 保持 200x200,但较长的图像会上下移动。)

我不想使用 Javascript 来执行此操作。 (因为这样做不会使用硬件并且会很慢。)

【问题讨论】:

    标签: javascript html templates css


    【解决方案1】:
    @keyframes bounce-background {
        from {
            background-position: top;
        }
        50% {
            background-position: bottom;
        }
        to {
            background-position: top;
        }
    }
    .abc {
        animation-name: bounce-background;
        animation-timing-function: ease-in-out;
        animation-duration: 2s;
        animation-iteration-count: infinite;
    
        /* For Chrome & Safari */
       -webkit-animation-name: bounce-background;
       -webkit-animation-timing-function:ease-in-out;
       -webkit-animation-duration:2s;
       -webkit-animation-iteration-count:infinite;
    }
    

    您应该在您支持的浏览器的供应商前缀下重复这些未最终确定的 CSS3 属性(例如,@-moz-keyframes@-o-keyframes@-webkit-keyframes 等)。

    【讨论】: