我正在做一个 JQuery 动画菜单。这只是与我的问题有关的部分代码。
我有一堆动画(使用 JQuery)的 div,因此它们向左右扩展。在 div 中,我使用背景图像,当 div 扩展和收缩时,我想保持图像定位,使其不会移动。
这是一个(工作)自包含的完整示例,将其保存为 .html 以进行尝试。(你能以某种方式将它附加到stackoverflow中吗?)
<!DOCTYPE html>
<html>
<head>
<title>JQuery Jitter Bug</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<style>
body { background: #0f1923; margin:0px; padding:0px;}
div#logo {
border: 0px;
margin: 0px;
padding: 0px;
background-image: url("http://static.jquery.com/files/rocker/images/logo_jquery_215x53.gif");
background-position:20%;
position: absolute;
height: 53px;
left: 100px;
width: 100px;
top: 50px;
}
</style>
</head>
<body>
<div id="logo"></div>
<script>
$('div#logo').mouseenter(function(){$(this).animate( {left: "0px", width: "600px"}, 1000); }); /* show */
$('div#logo').mouseleave(function(){$(this).animate( {left: "100px", width: "100px"}, 1000); }); /* crop */
</script>
</body>
</html>
给你两个问题:
1)如何计算背景位置百分比?反复试验表明,这里有 20% 是正确的。div展开时为left=0,width=600像素,折叠时为left=100,width=100。怎么变成20%?它不应该是折叠大小的左边缘(100/600 = 16.666%?)显然不是,但为什么不呢?
2) 这在 Firefox 中看起来很漂亮,但在 Safari 和 Chrome(我在 OSX 上)中,动画播放时图像会抖动。关于如何解决这个问题的任何想法,以便在其他浏览器中看起来更好?