简体   繁体   中英

Set margin top for a DIV using JQuery

Here is the situation, i have a fixed header with 120px height, and i forced the page to jump to #testdiv when the query string is #testdiv.

But the issue is when page jumps to beginning of the above DIV, part of the content will be lost,because it's behind the fixed header, is there any jquery to force page to jump to the #testdiv with not actual margin?

I tried the below code but it makes actual margin

if (url.indexOf('#testdiv') >= 0) {
$('#testdiv').css('margin-top',120);
};

I think there would be easiest way to do that, set margin-top for your field, and set the negative margin-top for your parent field.

It should be work for you.

if (url.indexOf('#testdiv') >= 0) {
$('#testdiv').css('margin-top',120);
$('#parent').css('margin-top',"-120px");
};

您可以通过jQuery设置margin-top,如下所示:

$("#mydiv").css("margin-top", "10px");

如果您有一个不想重叠内容的固定标题,则需要将padding-top添加到包含div(或者主体,如果需要),它与固定内容的高度相匹配以向下推送底层元素。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM