当前位置:编程学习 > JAVA >>

解决jQuery在FireFox火狐下无法实现背景图片定位动画的问题

一般情况下,实现背景图片定位动画的方法很简单,能兼容各种iyjt:
1
$(elem).animate({backgroundPositionX : '-39px', backgroundPositionY : '-39px'}, 500);
 但是这种写法火狐不支持,用Google搜索了半天,终于找到了解决方法,即扩展jQuery 的 $.fx.step 方法:

01
(function($) {
02
    $.extend($.fx.step,{
03
        backgroundPosition: function(fx) {
04
            if (fx.state === 0 && typeof fx.end == 'string') {
05
                var start = $.curCSS(fx.elem,'backgroundPosition');
06
                start = toArray(start);
07
                fx.start = [start[0],start[2]];
08
                var end = toArray(fx.end);
09
                fx.end = [end[0],end[2]];
10
                fx.unit = [end[1],end[3]];
11
            }
12
            var nowPosX = [];
13
            nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
14
            nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
15
            fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];
16
 
17
           function toArray(strg){
18
               strg = strg.replace(/left|top/g,'0px');
19
               strg = strg.replace(/right|bottom/g,'100%');
20
               strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
21
               var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
22
               return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
23
           }
24
        }
25
    });
26
})(jQuery);
使用方法:
1
$(elem).animate({backgroundPosition : '(0 -39px)'}, 500);
这种方法的出处:
文章: http://snook.ca/archives/javascript/jquery-bg-image-animations/
示例: http://snook.ca/technical/jquery-bg/

最后附上一个完整的示例:
 
01
<!DOCTYPE html>
02
<html>
03
<head>
04
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
05
<meta name="author" content="pandao QQ:272383090" />
06
<title>jQuery 背景图片定位动画(能解决FF不支持的问题)</title>
07
<style type="text/css">
08
*{margin:0;padding:0;}
09
body{font-size:14px;color:#444;font-family:"微软雅黑",Arial;background:#fff;}
10
img{border:none;vertical-align: middle;}
11
#test{width:200px;height:50px;margin:100px auto;border:3px solid red;padding:10px;cursor: pointer;background:#fff url(/2012/0814/20120814094115952.png) no-repeat left top;}
12
</style>
13
</head>
14
<body>
15
<div id="test">TEST</div>
16
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
17
<script type="text/javascript">
18
    (function($) {
19
        $.extend($.fx.step,{
20
            backgroundPosition: function(fx) {
21
                if (fx.state === 0 && typeof fx.end == 'string') {
22
                    var start = $.curCSS(fx.elem,'backgroundPosition');
23
                    start = toArray(start);
24
                    fx.start = [start[0],start[2]];
25
                    var end = toArray(fx.end);
26
                    fx.end = [end[0],end[2]];
27
                    fx.unit = [end[1],end[3]];
28
                }
29
                var nowPosX = [];
30
                nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
31
                nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
32
                fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];
33
 
34
               function toArray(strg){
35
                   strg = strg.replace(/left|top/g,'0px');
36
                   strg = strg.replace(/right|bottom/g,'100%');
37
                   strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
38
                   var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
39
                   return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
40
&nb

补充:web前端 , JavaScript ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,