#10021 closed bug (fixed)
Can't add negative numbers using the relative-value syntax with .css() and .animate()
Reported by: | Owned by: | dmethvin | |
---|---|---|---|
Priority: | low | Milestone: | 1.6.3 |
Component: | css | Version: | 1.6.2 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
Description
When I use the relative-value syntax in my .css() and .animate(), I can't add negative numbers like so:
$('#foo').css('margin-left','+=-1')
This comes up when you're building up the value using string concatenation and you don't know whether the value is positive of negative:
$('#foo').css('margin-left','+='+value)
Here's a jsFiddle which highlights the problem:
Change History (7)
comment:1 Changed 12 years ago by
Component: | unfiled → css |
---|---|
Priority: | undecided → low |
Resolution: | → wontfix |
Status: | new → closed |
comment:2 Changed 12 years ago by
You're right, nobody would write out +=-1 by hand, but my point is, users shouldn't have to write out the tertiary operator by hand when what they're trying to say is perfectly valid. That kind of conditional code is messy and annoying to write. Besides, adding a negative number is a valid mathematical expression :)
comment:3 Changed 12 years ago by
Resolution: | wontfix |
---|---|
Status: | closed → reopened |
I think this may be a very easy fix, just add the minus sign to the rfxnum regexp in effects.js. The code in css.js needs a bit more tweaking to code but it shouldn't be much.
comment:4 Changed 12 years ago by
Milestone: | None → 1.6.3 |
---|---|
Owner: | set to dmethvin |
Status: | reopened → assigned |
comment:6 Changed 12 years ago by
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
Merge pull request #464 from dmethvin/fix-10021-relative-negative-values
Fixes #10021. Allow negative relative values for .css()
Changeset: 3ba72f991dccbae6020c23d6f7a65435f4ca6f03
Thanks for submitting a ticket to the jQuery bug tracker. I've never actually seen anyone use relative-value syntax in the way described for negative numbers (ie. +=-1).
Why not simply use the (more common) negative equivalent of +=1 (ie. -=1?). http://jsfiddle.net/F7Ybw/4/. If there is an uncertainty about where the value used in your string concatenation example is positive or negative, there are many ways you can use to test this.
For example, if the value passed is a string (with or without units) var myOperator = (parseInt(value) <0)? '-=' : '+='; would allow you to get the correct operator to use and you can then simply use $('#foo').css('margin-left', myOperator +value); as per your example.