jQuery 如何使用jquery设置顶部位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10597769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:34:37  来源:igfitidea点击:

How to set top position using jquery

jquery

提问by Rajanikant Shukla

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

我正在创建自定义 div 滚动条并希望设置内容 div 的顶部位置。我的jquery代码如下:

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:

和 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>'

回答by Ravi Gadag

You can use CSS to do the trick:

您可以使用 CSS 来解决这个问题:

$("#yourElement").css({ top: '100px' });

回答by Starx

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

使用.css()访问 CSS 属性和操作非常容易。例如,要更改单个属性:

$("selector").css('top', '50px');

回答by CG_DEV

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

.css( "height" )和之间的区别在于,.height()后者返回无单位像素值(例如400),而前者返回单位完整的值(例如400px)。当需要在数学计算中使用元素的高度时,建议使用 .height() 方法。jQuery 文档

回答by Cameron

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.

与上面的 CSS 方法相反。

回答by ehrhardt

And with Prototype:

并使用原型

$('yourDivId').setStyle({top: '100px', left:'80px'});