使用jQuery和sprites

时间:2010-10-10 02:11:48

标签: jquery css sprite

我正在寻找一种用jquery为我的精灵制作动画的方法。当我将鼠标悬停在图像上时,它会淡入。然后当我将鼠标悬停时。它淡出了。

CSS:

nav ul { position: relative; z-index: 1000;  margin: -26px 0 0 11px; overflow: hidden; }
nav ul li { display: inline; }
nav ul li a { background: url(../images/nav-sprite.png); display: block; float: left; text-indent: -999px; margin: 0 auto; width: 667px; }
nav ul a#nav-1 { background-position: 0 0;  height: 48px; width: 103px; }
nav ul a#nav-2 { background-position: -103px 0;  height: 48px; width: 129px; }
nav ul a#nav-3 { background-position: -234px 0;  height: 48px; width: 156px; }
nav ul a#nav-4 { background-position: -392px 0;  height: 48px; width: 147px; }
nav ul a#nav-5 { background-position: -541px 0;  height: 48px; width: 124px; }
nav ul a#nav-1:hover { background-position: 0 48px; }
nav ul a#nav-2:hover { background-position: -103px 48px; }
nav ul a#nav-3:hover { background-position: -234px 48px; }
nav ul a#nav-4:hover { background-position: -392px 48px; }
nav ul a#nav-5:hover { background-position: -541px 48px; }

2 个答案:

答案 0 :(得分:1)

这个问题中没有特定的精灵。您始终可以使用常用方法,将hover事件与fadeTo函数结合使用,以获得所需的不透明度动画。

$("nav ul a").hover(function(){
    $(this).stop().fadeTo(300, 1);
}, function(){
    $(this).stop().fadeTo(300, 0.4);
}).fadeTo(0, 0.4);

如果需要,您还可以添加CSS后备:

nav ul a {
    opacity: 0.4;
}

nav ul a:hover {
    opacity: 1;
}

看到它在这里工作:http://www.jsfiddle.net/yijiang/CNC6W/


嗯,修复起来很简单 - 尝试在ul元素中添加背景,然后给它border-radius以确保圆角保持不变。

nav ul {
    background: #000;
    -moz-border-radius: 8px;
    -webkit-border-radius: 8px;
    border-radius: 8px;   /* Not sure about the actual radius 
                             you're using - these are guesses */
}

您可能还希望将起始不透明度从0.4提高到更高,将持续时间提高到低于300.

答案 1 :(得分:0)

查看来自CSS Tricks的this屏幕投射。它涉及动画CSS精灵。