Skip to content
Browse files

allow $('elem').css({ 'left' : '+=10px' });

  • Loading branch information...
1 parent 684c409 commit 6a60757969cf6a742f6d8c51d939ebb088748ce4 @paulirish committed Oct 23, 2010
Showing with 47 additions and 0 deletions.
  1. +47 −0 cssmonkeypatch.js
View
47 cssmonkeypatch.js
@@ -1,4 +1,51 @@
+// 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.

0 comments on commit 6a60757

Please sign in to comment.
Something went wrong with that request. Please try again.