简体   繁体   English

如何向下推网页以适合顶部栏

[英]how to push webpage down to fit a top bar

Is there a way to measure height of a div element using javascript? 有没有一种方法可以使用javascript来测量div元素的高度?

Here is my situation: I created a top fixed position bar on my website, so even if I scroll the page up/down, the bar stays on top, but my problem is bar overlaying the webpage, what I want is bar pushing the webpage down for the height of the bar so they look like stacked, I managed to do it by fixed height, say 50ox, I set html page top margin and they looked ok, when the height of bar changes, for example looking the same page in mobiles goes wrong because height of bar increases in mobile but the top margin I set is still 50, so I need a way to measure actual height of bar when ti is rendered and adjust the page according to it. 这是我的情况:我在网站上创建了一个顶部固定位置栏,所以即使我上下滚动页面,该栏也位于顶部,但是我的问题是栏重叠在网页上,我想要的是栏推动网页降低条形的高度,使它们看起来像堆叠的一样,我设法以固定高度进行操作,例如50ox,我设置了html页面的上边距,当条形的高度发生变化时(例如,在移动设备出现错误,因为移动设备中栏的高度增加了,但是我设置的最高边距仍然是50,所以我需要一种方法来测量渲染ti时的栏的实际高度,并根据其调整页面。

If you fix the top bar in position, you shouldn't have to worry about the contents below it actually showing below it unless you've modified their positions as well. 如果将顶部栏固定在适当的位置,则不必担心其下方的内容实际显示在其下方,除非您也修改了它们的位置。 If that's the case then you can calculate the height of an element using window.getComputedStyle() and you can use that value to set the margin. 如果是这种情况,则可以使用window.getComputedStyle()计算元素的高度,然后可以使用该值来设置边距。

 var wrapper = document.getElementById("wrapper"); var banner = document.getElementById("banner"); var content = document.getElementById("content"); var bannerHeight = window.getComputedStyle(banner).height; console.log("Height of the banner is: " + bannerHeight); content.style.marginTop = bannerHeight; 
 #wrapper { background: #e0e0e0; } #banner { background: #ff0; position:fixed; top:0; width:100%; z-index:99; } #content { background: #66f; position:relative; } 
 <div id="wrapper"> <div id="banner"> <h1>This is the page banner</h1> </div> <div id="content"> <h1>This is FIRST LINE of the main content area of the document.</h1> <p>This is the main content area of the document.</p> <p>This is the main content area of the document.</p> <p>This is the main content area of the document.</p> <p>This is the main content area of the document.</p> <p>This is the main content area of the document.</p> <p>This is the main content area of the document.</p> <p>This is the main content area of the document.</p> <p>This is the main content area of the document.</p> <p>This is the main content area of the document.</p> <p>This is the main content area of the document.</p> <p>This is the main content area of the document.</p> <p>This is the main content area of the document.</p> <p>This is the main content area of the document.</p> <p>This is the main content area of the document.</p> <p>This is the main content area of the document.</p> <p>This is the main content area of the document.</p> <p>This is the main content area of the document.</p> <p>This is the main content area of the document.</p> <p>This is the main content area of the document.</p> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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