January 6, 2011

jQuery – .css("right", posVar); does not work

Question by Tomkay

The Main objective here is to make an instand “jump” to the right. Like I said – I dont want to use .animate, because it makes a little “flashing” effect – because it is not instant. I think .css will make an instant “jump” to the right, without flashing effect.

You can find the full javascript code for my gallery here (german comments):
http://codeviewer.org/view/code:15dd

And the HTML
http://codeviewer.org/view/code:15df

I dont know why this will not work.. Instead of using .css I actually have to workaround this issue with .animate $("#gallery ul").animate({right: posVar},0);

I dont get it.. here is my actual (Javascript, JQuery) code:

setTimeout(function(){
   var posVar = 2838;        
   $('#gallery ul').css("right", posVar);
},300);

Whats wrong with that?

#gallery ul css

#gallery ul {
display:block;
position:relative;
width:14000px; /*auto calculated from javascript */
}

Answer by Starx

Css attributes:left, right, top, down will not work with display:block try display:absolute or display:fixed

I dont know what was on my mind when I wrote that answer. I mean to say…. you need position:absolute or position:fixed or position:relative

And I think the problem with your code is there is not “px” at the end…

I have fiddeled it, and here it is http://jsfiddle.net/Starx/8w6cS/

YOUR CODE IS WORKING HERE IS A PROOF

http://jsfiddle.net/Starx/8w6cS/1/


AND IF YOU TRYING TO MOVE YOUR ul to 2838px to the right Here is your solution

http://jsfiddle.net/Starx/8w6cS/2/

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!