简体   繁体   English

以其他像素为单位的div高度减去div高度的百分比

[英]get div height in percent subtracted of other div height which is in pixel

I am trying to get percentage of a side bar height of which height is subtracted from a header of which height is in pixel. 我试图获取从高度为像素的标题中减去高度的边栏高度的百分比。

<!--html-->
<div id="header"></div>
<div id="sidebar"></div>

<!--css-->
body{padding: 0; margin: 0;}
#header {position: fixed; width: 100%; height: 50px; background-color: yellow;}
#sidebar {position: fixed; width: 20%; background-color: blue; bottom: 0px; left: 0px; }

<!--js-->
$(document).ready(function(){   
    var bodyHeight = document.getElementByTagName("body").height();
    var headerHeight = document.getElementById("header").height();
    var sideBarHeight = (bodyHeight - headerHeight) / bodyHeight;
    $("#sidebar").css("height",sideBarHeight);
});

Here is my fiddle to check it out: https://jsfiddle.net/Herza/7ks1qet1/4/ 这是我的小提琴来检查一下: https : //jsfiddle.net/Herza/7ks1qet1/4/

CSS should be able to do do this: CSS应该能够做到这一点:

 body{padding: 0; margin: 0;} #header {position: fixed; width: 100%; background-color: yellow; top:0; height: 50px; } #sidebar {position: fixed; width: 20%; background-color: blue; bottom: 0px; left: 0px; top:50px; } 
 <div id="header"></div> <div id="sidebar"></div> 

or even with clac() 甚至与clac()

 body{padding: 0; margin: 0;} #header {position: fixed; width: 100%; background-color: yellow; top:0; height: 50px; } #sidebar {position: fixed; width: 20%; background-color: blue; bottom: 0px; left: 0px; height: calc(100% - 50px); } 
 <div id="header"></div> <div id="sidebar"></div> 

you can run both snippets in fullpage mode and resize you window to check on it. 您可以在整页模式下运行两个代码片段,并调整窗口大小以对其进行检查。

Beside the CSS/JS trouble you add, i'm not too sure it is a good idea, header and sidebar might be too small for content and in fixed position , what overflow off screen is unreachable ... 除了您添加的CSS / JS麻烦之外,我不太确定这是个好主意,标题和侧边栏对于内容和固定位置而言可能太小了,无法从屏幕上溢出什么……

If you want a percentage you should convert the pixels back to a percentage. 如果需要百分比,则应将像素转换回百分比。

Is this what you meant? 这是你的意思吗?

https://jsfiddle.net/7fkfvwrf/ https://jsfiddle.net/7fkfvwrf/

 $(document).ready(function(){ var bodyHeight = $(document).height(); var headerHeight = $("#header").height(); var sideBarHeightPercentage = 100 / bodyHeight * (bodyHeight - headerHeight); $("#sidebar").css("height",sideBarHeightPercentage + "%"); }); 
 body{padding: 0; margin: 0;} #header {position: fixed; width: 100%; height: 50px; background-color: yellow;} #sidebar {position: fixed; width: 20%; background-color: blue; bottom: 0px; left: 0px; } 
 <div id="header"></div> <div id="sidebar"></div> 

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

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