Bug Tracker

Opened 5 years ago

Closed 4 years ago

#14484 closed bug (migrated)

Relative percentage values (+= / -=) work in .animate but not in .css

Reported by: jwagner@… Owned by: gibson042
Priority: blocker Milestone: 1.12/2.2
Component: css Version: 1.10.2
Keywords: Cc:
Blocked by: Blocking:



...moves the item by 50 pixel instead of 50%, while


...works as expected (adds 50%).

The docs suggest that .css and .animate should behave identically (http://api.jquery.com/css/):

As of jQuery 1.6, .css() accepts relative values similar to .animate().

If "similar" should really mean "similar" but not identical, it would help to give a hint in the docs that relative percentage values are not supported by .css.

The problem shows up in current Chrome, Firefox and IE10.

Fiddle to reproduce: http://jsfiddle.net/2VGgg/

Change History (7)

comment:1 Changed 5 years ago by timmywil

Component: unfiledcss
Milestone: None1.11/2.1
Priority: undecidedblocker
Status: newopen

This looks valid.

comment:2 in reply to:  description Changed 5 years ago by francomalatacca

Replying to jwagner@…:


The problem is that style function that is called by css function doesn't care about percentage. A fix could be by adding support for this by checking if there is the % symbol at the end of value. If so it should compute the percentage of the parent width. I fixed the bug with something

			if ( type === "string" && (ret = rrelNum.exec( value )) ) {
				// Fixes bug #14484
				if(!/[%$]/i.exec(value) ) {
					value = (( ret[1] + 1 ) *  ret[2]) + parseFloat( jQuery.css( elem, name ) );
				} else {
					value = parseFloat( jQuery.css( elem, name ) ) + (parseFloat(jQuery(elem).parent().css("width")) * (( ret[1] + 1 ) * ret[2]/100));
				// Fixes bug #9237
				type = "number";

Thanks. Franco

comment:3 Changed 5 years ago by gibson042

Owner: set to gibson042
Status: openassigned

comment:4 Changed 5 years ago by gibson042

Milestone: 1.11/

comment:5 Changed 5 years ago by anonymous

This is reproducible for other unit "em"

There are other not so popular units like "cm","in" etc.

Need fix for them too

comment:6 Changed 5 years ago by dmethvin

Milestone: 1.11.1/

comment:7 Changed 4 years ago by m_gol

Resolution: migrated
Status: assignedclosed
Note: See TracTickets for help on using tickets.