解決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(/wp-content/images1/20181020/20120814094115952384.png) no-repeat left top;}
12
</style>
13
</head>
14
<body>
15
<p id="test">TEST</p>
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
               }
41
            }
42
        });
43
    })(jQuery);
44
 
45
    //使用方法
46
    $(function() {
47
        //css加不加都沒關系
48 www.2cto.com
        $('#test').css( {backgroundPosition: "0 0"} ).hover(function() {
49
            $(this).animate({backgroundPosition:"(0 -250px)"}, {duration:500});
50
        }, function() {
51
            $(this).animate({backgroundPosition:"(0 0)"}, {duration:500});
52
        });
53
    });
54
</script>  
55
</body>
56
</html>

作者:pandao

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *