我为我的画廊使用图像精灵。为了在正确的位置获得正确的背景图像,我使用了这个:
<style>
#container div:nth-child(1){background-position:0 0}
#container div:nth-child(2){background-position:200px 0}
#container div:nth-child(3){background-position:400px 0}
#container div:nth-child(4){background-position:600px 0}
#container div:nth-child(5){background-position:800px 0}
#container div:nth-child(6){background-position:1000px 0}
</style>
当有许多 div 标签并且要添加的数字是例如 73px 而不是 200 时,计算所有背景值是很耗时的。而且它占用了大量的 css 代码。
我可以使用 CSS3 以另一种方式实现相同的结果吗?
否则我想知道是否有人可以查看我的 jquery 脚本,看看是否有可以以更好的方式完成的事情(脚本有效):
<div id="container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script>
$(function() {
$("#container div:first-child").fadeIn(400, showNext);
});
function showNext() {
var test1 = $(this).index(),
test2 = test1*200;
if (navigator.appName=='Microsoft Internet Explorer'){
$(this).css('backgroundPositionX', -test2);
}
else{
$(this).css('backgroundPosition', -test2);
}
$(this).next().fadeIn(400, showNext);
}
</script>