Getting the initial value of a CSS style property in jQuery / Javascript

I know that the value of the original css from the stylesheet can be assigned with: $ ('# id'). css ('property', ''); but is there a way to get the value without changing the property in the object? Note that this may differ from getting the current value , as it can be changed from the value of the stylesheet.


Possible "solutions" (untested)

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

I use a lot of images, though ... so it's a little more complicated than that.


(I want to animate it from the current to the original value, and not just move from one value to another)

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

....

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

:

$(this).attr("customAttributeName");
+3

, . , ( JQuery) script $(document).ready(...), .

- $(this).css("style-name","") ( ).

, . myVar = $(this).css("style-name") . var myVar = .... myVar. .

+1

'none'

<div id="test" style="display:none;"></div>

var property = $('#test').css('display');
alert(property);
0
source

Is this what you are looking for?

<html>
<head>
    <title></title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(document).ready(function() {
        newColor = $('#id').css('color');
        $('#newid').css('color',newColor);
        $('#id').css('color','green');
    });
    </script>
    <style type="text/css" media="screen">
    #id{color:red;}
    </style>
</head>
<body>
<div id="id"><h1>Boom!</h1></div>
<div id="newid"><h1>Boom!</h1></div>
</body>
</html>
0
source

Assuming I understand the question, you just need something like object.style.property, no jQuery needed.

-1
source

All Articles