Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set top position using jquery

Tags:

jquery

I am creating custom div scroller and want to set top position of content div. My jquery code is as below:

containerOuterHeight=$("#messagePopUpContainer").outerHeight();             contentOuterHeight=$("#content").outerHeight();             contentTopPosition=$("#content").offset().top;             alert("contentTopPosition"+contentTopPosition);             alert(contentTopPosition-(containerOuterHeight/20));             $("#content").offset().top=contentTopPosition-(containerOuterHeight/20);             //$("#content").css("top",( contentTopPosition-(containerOuterHeight/20) ) + "px");             alert("contentTopPosition"+$("#content").offset().top);             //alert("Fontsize"+$('#content').css('font-size') ); 

and html is:

<div id='messagePopUpContainer' style='background-color:#ffffff; overflow: hidden;'> <a href='javascript:void(0);' id='popupanchor' onkeydown='PopupKeyHandler("' + elmId + '");'></a> 

<div id='content' style='width:350px'>' + methods.settings[elmId].text + '</div > <div id='popupReturn'>Return</div></div></div>' 
like image 813
Rajanikant Shukla Avatar asked May 15 '12 09:05

Rajanikant Shukla


People also ask

What is offset () top in jQuery?

jQuery offset() Method The offset() method set or returns the offset coordinates for the selected elements, relative to the document. When used to return the offset: This method returns the offset coordinates of the FIRST matched element. It returns an object with 2 properties; the top and left positions in pixels.

How do I set relative position in jQuery?

jQuery position() MethodThe position() method returns the position (relative to its parent element) of the first matched element. This method returns an object with 2 properties; the top and left positions in pixels.

What is offset () Top?

The offsetTop property returns the top position (in pixels) relative to the parent. The returned value includes: the top position, and margin of the element. the top padding, scrollbar and border of the parent.

How get scroll top in jQuery?

To set scrolltop position in jQuery, use the scrollTop() method. The scrollTop( ) method gets the scroll top offset of the first matched element. This method works for both visible and hidden elements.


Video Answer


1 Answers

You can use CSS to do the trick:

$("#yourElement").css({ top: '100px' }); 
like image 163
Ravi Gadag Avatar answered Sep 24 '22 08:09

Ravi Gadag