上下移动背景图像

Dan*_*uła 1 css jquery transition

我有一个带有背景图像的 div。如何做到这一点:当你悬停在那个 div 上时,背景图像向上移动 5 像素,然后向下移动 5 像素?例如快起来,慢下来。

我的代码:

CSS

<style>
.place {
background:url(../img/place.png) no-repeat 6px 50%;
}
</style>
Run Code Online (Sandbox Code Playgroud)

HTML

 <div class="place">
    123 Something Street<br/>
    UB5 212 Liverpool
    </div>
Run Code Online (Sandbox Code Playgroud)

如何通过过渡做到这一点?还是jquery?

Ati*_*din 5

使用 CSS3

.place {
    background:url(http://lorempixel.com/20/20/) no-repeat;
}

div.place:hover {
    animation:myanim 0.5s;
    -webkit-animation:myanim 0.5s;
    /* Safari and Chrome */
}

@keyframes myanim {
    0% {
        background-position:0 0;
    }
    50% {
        background-position:20px 0;
    }
    100% {
        background-position:0 0;
    }
}

@-webkit-keyframes myanim
/* Safari and Chrome */
 {
    0% {
        background-position:0 0;
    }
    50% {
        background-position:20px 0;
    }
    100% {
        background-position:0 0;
    }
}
Run Code Online (Sandbox Code Playgroud)