How to set top position using jquery

后端 未结 5 650
一向
一向 2021-02-04 23:22

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

containerOuterHeight=$(\"#messagePopUpContainer\").out         


        
相关标签:
5条回答
  • 2021-02-04 23:36

    You can use CSS to do the trick:

    $("#yourElement").css({ top: '100px' });
    
    0 讨论(0)
  • 2021-02-04 23:43

    You could also do

       var x = $('#element').height();   // or any changing value
    
       $('selector').css({'top' : x + 'px'});
    

    OR

    You can use directly

    $('#element').css( "height" )
    

    The difference between .css( "height" ) and .height() is that the latter returns a unit-less pixel value (for example, 400) while the former returns a value with units intact (for example, 400px). The .height() method is recommended when an element's height needs to be used in a mathematical calculation. jquery doc

    0 讨论(0)
  • 2021-02-04 23:51

    Accessing CSS property & manipulating is quite easy using .css(). For example, to change single property:

    $("selector").css('top', '50px');
    
    0 讨论(0)
  • 2021-02-04 23:57

    Just for reference, if you are using:

     $(el).offset().top 
    

    To get the position, it can be affected by the position of the parent element. Thus you may want to be consistent and use the following to set it:

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

    As opposed to the CSS methods above.

    0 讨论(0)
  • 2021-02-05 00:01

    And with Prototype:

    $('yourDivId').setStyle({top: '100px', left:'80px'});
    
    0 讨论(0)
提交回复
热议问题