jQueryがFireFox火狐の下で背景画像の位置決めアニメーションを実現できないという問題を解決する
4177 ワード
一般的に、背景画像の位置決めアニメーションを実現する方法は簡単で、各種iyjtと互換性があります.
しかし、この書き方は火狐がサポートせず、Googleで半日検索し、jQueryの$を拡張する解決策を見つけた.fx.Stepメソッド:
使用方法:
この方法の出典:
記事:http://snook.ca/archives/javascript/jquery-bg-image-animations/
例:http://snook.ca/technical/jquery-bg/最後に、完全な例を添付します.
$(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>