Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving original stylesheet CSS property value in JQuery/Javascript

I know that the original css value from the style sheet can be assigned using: $('#id').css('property', ''); but is there a way to retrieve the value without changing the property in the object? Note that may be different from retrieving the current value because it may have changed from the style sheet value.


Possible "solutions" (untested)

var originalTopPos = $("#testPic").css("top");
....
$("#testPic").animate({"top": originalTopPos + "px"},"slow");

I'm using many images though... so it is a bit more complicated than that.


(I want to animate it from the current to original value, not just jump from one value to the other)

var currentTopPos = $("#testPic").css("top");
var originalTopPos = $("#testPic").css("top", "").css("top");
$("#testPic").css("top", currentTopPos + "px")
$("#testPic").animate({"top": originalTopPos + "px"},"slow");
like image 242
Luke Wenke Avatar asked Jun 10 '11 01:06

Luke Wenke


2 Answers

I found the answer....

$(this).attr("customAttributeName", $(this).position().top);

then you can recall it with:

$(this).attr("customAttributeName");
like image 135
Luke Wenke Avatar answered Nov 15 '22 00:11

Luke Wenke


The easiest way to do this depends on whether you want to push the current value or the original. Since you say original, the surest way is to (if you're using JQuery) create a script within $(document).ready(...) that executes first and stores values for everything to variables.

The second way is to do $(this).css("style-name","") (the null argument resets to default).

If you are simply pushing the current value, you can do the similar thing with the variables. myVar = $(this).css("style-name") returns the current attribute value. Do not do var myVar = .... Its just myVar. My browser yelled at me about that.

like image 32
Anthony Avatar answered Nov 15 '22 00:11

Anthony