javascript 使用变量调整 div 边距顶部 - jquery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5549459/
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-10-25 17:35:26  来源:igfitidea点击:

adjust div margin top using variable - jquery

javascriptjqueryjquery-ui

提问by blasteralfred Ψ

I have a diva and its height is auto calculated using jquery using

我有一个天后,它的高度是使用 jquery 自动计算的

  $(window).load(function(){
  $(window).resize(function(){
   var height = $(this).height() - $("#header").height() + $("#footer").height() - 35
   $('#content').height(height);
})
$(window).resize();
  });

I have another div with id content2and I want to set the value stored in the variable var heightas its margin-top. How can I do this??

我有另一个带有 id 的 div,content2我想将存储在变量中的值设置var height为其边距顶部。我怎样才能做到这一点??

Thanks in advance...:)

提前致谢...:)

blasteralfred

布拉拉弗雷德

回答by JohnP

Use the .css()method.

使用.css()方法。

  $(window).load(function(){
    $(window).resize(function(){
      var height = $(this).height() - $("#header").height() + $("#footer").height() - 35
      $('#content').height(height);
      $('#content2').css('marginTop', height); // <-- set here
    });
    $(window).resize();
  });

回答by Headshota

$('#content2').css('margin-top',height);

回答by vikas etagi

//Calculate the height of the offset needed.

//计算需要的偏移量的高度。

var height = $('.navbar-fixed-top').height() + $('.navbar-static-top').height() + 70;

//Then on click on the navbar tab change the offset of the div according, to the top of the window

//然后点击导航栏选项卡,根据窗口顶部更改div的偏移量

$('#r_overview').click(function () {
    $(window).scrollTop(($('#hackathon_overview').offset().top)-height);
});

$('#r_rules').click(function () {
    $(window).scrollTop(($('.rules_row').offset().top)-height);
});

Hope this helps

希望这可以帮助