IE中几乎没有问题,有没有完美的解决方案/解决方案?
寻找在子div
上使用CSS属性以下的解决方案display: inline-block;
当父div
有2个子div
div
已预先定义width
div
width
。 当子div具有下面片段中给出的CSS属性时,IE中的父div滚动指标存在有线行为。与其他浏览器(如Chrome,Firefox,Edge)相比,IE中的scrollHeight
变化几个像素。
在显示inline-block
并隐藏内容overflow
时,是否有适当的解决方案来定位子div并且没有任何副作用。
function metrics() {
var parent = document.querySelector("#parent");
var parentMetrics = document.querySelector("#parentMetrics");
parentMetrics.innerText = 'Parent Scroll Height: ' + parent.scrollHeight + '\n' + 'Parent Client Height: ' + parent.clientHeight + '\n' + 'Parent Offset Height: ' + parent.offsetHeight;
}
metrics();
#parent {
background-color: orange;
white-space: nowrap;
}
#firstChildDiv {
background-color: brown;
color: white;
display: inline-block;
width: calc(100% - 200px);
height: 100px;
overflow: hidden;
vertical-align: top;
}
#secondChildDiv {
background-color: skyblue;
display: inline-block;
width: 200px;
height:100px;
}
<div id="parent">
<div id="firstChildDiv">FIRST CHILD: Please RUN this snippet in IE, there is scroll problem in IE, other browsers like, Chrome, Firefox, Edge behave properly.
</div><!--
FIX FOR WHITE SPACE
--><div id="secondChildDiv">
SECOND CHILD
</div>
</div>
<div id="parentMetrics">
</div>
function metrics() {
var parent = document.querySelector("#parent");
var parentMetrics = document.querySelector("#parentMetrics");
parentMetrics.innerText = 'Parent Scroll Height: ' + parent.scrollHeight + '\n' + 'Parent Client Height: ' + parent.clientHeight + '\n' + 'Parent Offset Height: ' + parent.offsetHeight;
}
metrics();
#parent {
background-color: orange;
white-space: nowrap;
font-size:0;
}
#firstChildDiv {
font-size:14px;
background-color: brown;
color: white;
display: inline-block;
width: calc(100% - 200px);
height: 100px;
overflow: hidden;
vertical-align: top;
}
#secondChildDiv {
font-size:14px;
background-color: skyblue;
display: inline-block;
width: 200px;
height:100px;
}
<div id="parent">
<div id="firstChildDiv">FIRST CHILD: Please RUN this snippet in IE, there is scroll problem in IE, other browsers like, Chrome, Firefox, Edge behave properly.
</div>
<div id="secondChildDiv">
SECOND CHILD
</div>
</div>
<div id="parentMetrics">
</div>
答案 0 :(得分:0)
这只是因为默认的浏览器样式。要规范化,您可以通过以下方式攻击line-height
:
#parent {
line-height: 1;
}
答案 1 :(得分:0)
解决了IE中不必要的scrollHeight问题
height
vertical-allign: middle;
function metrics() {
var parent = document.querySelector("#parent");
var parentMetrics = document.querySelector("#parentMetrics");
parentMetrics.innerHTML = '<hr>Parent Scroll Height: <span>' + parent.scrollHeight + '</span><hr>' + 'Parent Client Height: <span>' + parent.clientHeight +'</span><hr>';
}
metrics();
&#13;
#parent {
background-color: orange;
font-size: 0;
height:100px;
}
#firstChildDiv {
background-color: brown;
color: white;
display: inline-block;
width: calc(100% - 300px);
overflow: hidden;
vertical-align: middle;
height:100px;
font-size: 18px;
}
#secondChildDiv {
background-color: skyblue;
display: inline-block;
width: 300px;
vertical-align: middle;
height:100px;
font-size: 18px;
}
#parentMetrics{
background-color: yellowgreen;
padding:10px;
}
span{
color:red;
}
&#13;
<div id="parent">
<div id="firstChildDiv">FIRST CHILD: Test in IE to see the difference.
</div>
<div id="secondChildDiv">
SECOND CHILD
</div>
</div>
<div id="parentMetrics">
</div>
&#13;