Javascript 滚动时固定标题和导航栏下的内容

Javascript 滚动时固定标题和导航栏下的内容,javascript,jquery,css,Javascript,Jquery,Css,我希望标题和导航栏是固定的,当在我的网站上滚动。我可以将它们都固定,但是#container div中的内容不会显示在它们下面 我目前使用以下方法使我的网站内容显示在标题下方,但不确定如何计算#标题和#导航栏ID的margintop #header{ width:100%; position:fixed; top:0px; } #navbar{ width:100%; height:50px; margin:auto; position:

我希望标题和导航栏是固定的,当在我的网站上滚动。我可以将它们都固定,但是#container div中的内容不会显示在它们下面

我目前使用以下方法使我的网站内容显示在标题下方,但不确定如何计算#标题和#导航栏ID的margintop

#header{
    width:100%;
    position:fixed;
    top:0px;
}

#navbar{
    width:100%;
    height:50px;
    margin:auto;
    position:fixed;
    top:120px;
}


$(document).ready(function () {
                    $('#container').css('marginTop', $('#header').outerHeight(true) );

                });

谢谢你的帮助。

你能设置一个jsfiddle.managed来解决它吗:$(document).ready(函数(){$('#容器').css('marginTop',$('#header')).outerHeight(true)+$('#navbar').outerHeight(true));});
 $(document).ready(function () { $('#container').css('marginTop', $('#header').outerHeight(true) + $('#navbar').outerHeight(true) ); });