/
cssmonkeypatch.js
93 lines (59 loc) · 2.3 KB
/
cssmonkeypatch.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// allow $('elem').css({ 'left' : '+=10px' });
// so you dont need to do:
// $('elem').animate({ 'left' : '+=10px' }, 0);
// by paul irish
// for ralph holzmann
// http://github.com/paulirish/lazyweb-requests/issues#issue/10
(function($, oldcss) {
// magic from the core.
var rfxnum = /^([+\-]=)?([\d+.\-]+)(.*)$/;
$.fn.css = function(obj, val) {
var parts = rfxnum.exec(val),
that = this;
if ($.isPlainObject(obj)) {
$.each(obj, function(k, v) {
$(that).css(k, v);
});
// here's the magic.
} else if (val && parts && parts[1]) {
var end = parseFloat(parts[2])
return oldcss.call(this, obj, function(index, currentValue) {
return ((parts[1] === "-=" ? -1 : 1) * end) + parseFloat(currentValue);
});
// no fancypants
} else {
return oldcss.apply(this, arguments);
}
return this;
};
})(jQuery, jQuery.fn.css);
// css() upgrade for a few shorthand values
// paul irish. MIT license.
// by default you can't do $(elem).css('margin'), but instead $(elem).css('marginTop') and so on.
// this monkeypatch allows lets you retrieve all four values in shorthand style:
// e.g. $(this).css('padding')
(function($){
var css = $.fn.css, methods = {'padding':1,'margin':1}, dirs = 'Top Right Bottom Left'.split(' ');
$.fn.css = function(prop,val){
var jq = this;
if ( val || !(prop in methods)) return css.apply(jq,arguments);
return $.map(dirs, function(k){ return css.call(jq,prop+k) }).join(' ');
}
})(jQuery);
// The CSS property backgroundPosition does not exist in the accessible DOM properties within IE 8.
// this monkeypatch retifies that issue
// usage: $(elem).css('backgroundPosition');
// ticket: http://dev.jquery.com/ticket/5749
(function($){
var _css = $.fn.css;
$.fn.css = function(name,val){
if (!val && /background[-p|P]osition/.test(name)){
var value = _css.apply(this,arguments);
return value !== undefined ? value :
[_css.call(this,"backgroundPositionX"), "px ",
_css.call(this,"backgroundPositionY"), "px"].join("");
} else {
return _css.apply(this,arguments);
}
}
})(jQuery);