How to set top position using jquery

I am creating a custom div scroller and want to set the top position of the contents of the div. My jquery code is as follows:

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:

 <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>' 
+48
jquery
May 15 '12 at 9:23 a.m.
source share
5 answers

You can use CSS to do the trick:

 $("#yourElement").css({ top: '100px' }); 
+103
May 15 '12 at 9:28 a.m.
source share

Accessing and managing CSS properties is fairly easy with . css () . For example, to change one property:

 $("selector").css('top', '50px'); 
+54
May 15 '12 at 9:29 am
source share

You can also do

  var x = $('#element').height(); // or any changing value $('selector').css({'top' : x + 'px'}); 
+9
Dec 11 '14 at 16:34
source share

And with Prototype :

 $('yourDivId').setStyle({top: '100px', left:'80px'}); 
+2
Nov 29 '13 at 9:23
source share

Just for reference, if you use:

  $(el).offset().top 

To get a position, the position of the parent element can affect it. Thus, you may want to be consistent and use the following to install it:

 $(el).offset({top: pos}); 

Unlike the CSS methods above.

0
Dec 14 '17 at 5:14
source share



All Articles