最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - set padding top of div to minus the height of the header - Stack Overflow

programmeradmin3浏览0评论

I can set padding to my div with

<div id="Products" style="background-color:red; width:100%; padding-top:70px">
</div>

but instead of using 70px is it possible to use a value I have generated from the size of the header...

<script>
    function resizeDiv() {
    var headerHeight = $('header').outerHeight();
    }
</script>

So set top padding to headerHeight....is this done in css or js?

I can set padding to my div with

<div id="Products" style="background-color:red; width:100%; padding-top:70px">
</div>

but instead of using 70px is it possible to use a value I have generated from the size of the header...

<script>
    function resizeDiv() {
    var headerHeight = $('header').outerHeight();
    }
</script>

So set top padding to headerHeight....is this done in css or js?

Share Improve this question edited Jun 23, 2015 at 14:44 John asked Jun 23, 2015 at 14:42 JohnJohn 3,94523 gold badges84 silver badges174 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

Use the css() function.

You can create a function that runs on load and (if you want) on resize if the header changes it's height:

var setHeight = function() {
  var top = $('header').outerHeight();
  $('#products').css({'padding-top': top + 'px'});
}

$(window).load(function() {
  //On load you can be sure that the target element has been loaded 
  //(except if it is loaded from an ajax call)
  setHeight();
});

$(window).resize(function() {
  setHeight();
});
发布评论

评论列表(0)

  1. 暂无评论