【发布时间】:2012-11-28 15:23:23
【问题描述】:
您好,我正在尝试使用 jQuery 为背景图像设置动画,我尝试关注 this tutorial here,但是我的测试页面没有运气,我看到蓝色 Facebook 图标,但鼠标悬停在它上面没有动画向上露出灰色标志。请帮忙!谢谢,我是 jQuery 菜鸟。
我的测试页: http://leongaban.com/_stack/bg_animation/
Facebook 图标应该向上动画,完整图片如下:
我的 CSS
<style>
#facebook_icon li {
padding: 0;
width: 120px;
height: 119px;
list-style: none;
background: red;
background:url('img/fb_120x238.png') repeat 0 0;
}
</style>
HTML
<ul id="facebook_icon">
<li><a href="#"><div class="box"></div></a></li>
</ul>
Javascript
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.bgpos.js"></script>
<script>
(function() {
$('#facebook_icon li')
.css( {backgroundPosition: "0 0"} )
.mouseover(function(){
$(this).stop().animate(
{backgroundPosition:"(0 -119px)"},
{duration:500})
})
.mouseout(function(){
$(this).stop().animate(
{backgroundPosition:"(0 0)"},
{duration:500})
})
})();
</script>
更新:有效的 CSS 3 解决方案
CSS 3
#facebook_icon li {
padding: 0;
width: 120px;
height: 119px;
list-style: none;
background: red;
background:url('img/fb_120x238.png') repeat 0 0;
-moz-transition: background-position 1s;
-webkit-transition: background-position 1s;
-o-transition: background-position 1s;
transition: background-position 1s;
background-position: 0 0;
}
#facebook_icon li:hover{
background-position: 0 -119px;
}
.box {
width: 120px;
height: 119px;
}
HTML(添加了空的 div 框以供点击)
<ul id="facebook_icon">
<li><a href="#"><div class="box"></div></a></li>
</ul>
【问题讨论】:
-
你也可以使用 CSS3 过渡。
-
为什么你的li在你的a里?反过来。
-
您的
anchor中有一个li。 -
你试过 mouseenter() 和 mouseleave() 代替吗?
-
首先,你不需要
{duration:500}。你可以通过 500。api.jquery.com/animate
标签: javascript jquery html css jquery-animate