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>'
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.
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.
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.
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.
You can use CSS to do the trick:
$("#yourElement").css({ top: '100px' });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With