一般情况下,实现背景图片定位动画的方法很简单,能兼容各种iyjt:
$(elem).animate({backgroundPositionX : '-39px', backgroundPositionY : '-39px'}, 500);
但是这种写法火狐不支持,用Google搜索了半天,终于找到了解决方法,即扩展jQuery 的 $.fx.step 方法:
(function($) {
if($.browser.mozilla) { //判断是否为火狐浏览器,IE6/7/8会报错
$.extend($.fx.step,{
backgroundPosition: function(fx) {
if (fx.state === 0 && typeof fx.end == 'string') {
var start = $.curCSS(fx.elem,'backgroundPosition');
start = toArray(start);
fx.start = [start[0],start[2]];
var end = toArray(fx.end);
fx.end = [end[0],end[2]];
fx.unit = [end[1],end[3]];
}
var nowPosX = [];
nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];
function toArray(strg){
strg = strg.replace(/left|top/g,'0px');
strg = strg.replace(/right|bottom/g,'100%');
strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
}
}
});
}
})(jQuery);
使用方法:
$(elem).animate({backgroundPosition : '(0 -39px)'}, 500);
这种方法的出处:
文章: http://snook.ca/archives/javascript/jquery-bg-image-animations/
示例: http://snook.ca/technical/jquery-bg/
最后附上一个完整的示例:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="author" content="pandao QQ:272383090" />
<title>jQuery 背景图片定位动画(能解决FF不支持的问题)</title>
<style type="text/css">
*{margin:0;padding:0;}
body{font-size:14px;color:#444;font-family:"微软雅黑",Arial;background:#fff;}
img{border:none;vertical-align: middle;}
#test{width:200px;height:50px;margin:100px auto;border:3px solid red;padding:10px;cursor: pointer;background:#fff url(http://www.google.com.hk/images/nav_logo114.png) no-repeat left top;}
</style>
</head>
<body>
<div id="test">TEST</div>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
(function($) {
if($.browser.mozilla) { //判断是否为火狐浏览器,IE6/7/8会报错
$.extend($.fx.step,{
backgroundPosition: function(fx) {
if (fx.state === 0 && typeof fx.end == 'string') {
var start = $.curCSS(fx.elem,'backgroundPosition');
start = toArray(start);
fx.start = [start[0],start[2]];
var end = toArray(fx.end);
fx.end = [end[0],end[2]];
fx.unit = [end[1],end[3]];
}
var nowPosX = [];
nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];
function toArray(strg){
strg = strg.replace(/left|top/g,'0px');
strg = strg.replace(/right|bottom/g,'100%');
strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
}
}
});
}
})(jQuery);
//使用方法
$(function() {
//css加不加都没关系
$('#test').css( {backgroundPosition: "0 0"} ).hover(function() {
$(this).animate({backgroundPosition:"(0 -250px)"}, {duration:500});
}, function() {
$(this).animate({backgroundPosition:"(0 0)"}, {duration:500});
});
});
</script>
</body>
</html>