forked from brandonaaron/jquery-cssHooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bgpos.js
58 lines (54 loc) · 2.25 KB
/
bgpos.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*! Copyright (c) 2010 Brandon Aaron (http://brandonaaron.net)
* Licensed under the MIT License (LICENSE.txt).
*/
(function($) {
// backgroundPosition[X,Y] get hooks
var $div = $('<div style="background-position: 3px 5px">');
$.support.backgroundPosition = $div.css('backgroundPosition') === "3px 5px" ? true : false;
$.support.backgroundPositionXY = $div.css('backgroundPositionX') === "3px" ? true : false;
$div = null;
var xy = ["X","Y"];
// helper function to parse out the X and Y values from backgroundPosition
function parseBgPos(bgPos) {
var parts = bgPos.split(/\s/),
values = {
"X": parts[0],
"Y": parts[1]
};
return values;
}
if (!$.support.backgroundPosition && $.support.backgroundPositionXY) {
$.cssHooks.backgroundPosition = {
get: function( elem, computed, extra ) {
return $.map(xy, function( l, i ) {
return $.css(elem, "backgroundPosition" + l);
}).join(" ");
},
set: function( elem, value ) {
$.each(xy, function( i, l ) {
var values = parseBgPos(value);
elem.style[ "backgroundPosition" + l ] = values[ l ];
});
}
};
}
if ($.support.backgroundPosition && !$.support.backgroundPositionXY) {
$.each(xy, function( i, l ) {
$.cssHooks[ "backgroundPosition" + l ] = {
get: function( elem, computed, extra ) {
var values = parseBgPos( $.css(elem, "backgroundPosition") );
return values[ l ];
},
set: function( elem, value ) {
var values = parseBgPos( $.css(elem, "backgroundPosition") ),
isX = l === "X";
elem.style.backgroundPosition = (isX ? value : values[ "X" ]) + " " +
(isX ? values[ "Y" ] : value);
}
};
$.fx.step[ "backgroundPosition" + l ] = function( fx ) {
$.cssHooks[ "backgroundPosition" + l ].set( fx.elem, fx.now + fx.unit );
};
});
}
})(jQuery);