【问题标题】:Nested flexbox w/ overflow not working in IE10+带有溢出的嵌套弹性框在 IE10+ 中不起作用
【发布时间】:2014-06-17 08:51:45
【问题描述】:

我的 Web 应用程序中有一个 UI 元素,用作悬停面板。面板本身具有页眉、页脚和内容元素。面板将有一个最大高度,以避免面板从窗口溢出(这是在window.onresize 事件上更新的)。

由于我只需要支持 IE10 及以上版本,因此我使用 flexbox 来支持此布局。

我已经设法让这个跨浏览器工作:http://jsfiddle.net/Pyn9e/9/

尝试减小“结果”面板的大小,您会看到内容面板开始滚动而不是溢出,但仍确保页眉和页脚不会消失。

function setMaxHeight() {
  var maxHeight = $(window).height() - 16;
  $("#panel").css({
    height: maxHeight + "px"
  });
}

$(window).on("resize", function() {
  setMaxHeight();
});
setMaxHeight();

var i = 0;

setInterval(function() {
  var ul = $("#list");
  ++i;
  if (i <= 20) {
    var li = $("<li>").text("Item " + i);
    ul.append(li);
  } else if (i <= 40) {
    ul.children().last().remove();
  } else {
    i = 0;
  }
}, 60);
.flex-container {
  display: -ms-flex;
  display: flex;
  flex-direction: column;
  -ms-flex-direction: column;
}

.flex-fixed {
  flex: 0 1 auto;
  -ms-flex: 0 1 auto;
}

.flex-var {
  flex: 0 1 auto;
  -ms-flex: 0 1 auto;
}

#panel {
  position: absolute;
  top: 8px;
  left: 8px;
  width: 200px;
  color: white;
  overflow: hidden;
}

#panel > header {
  background: red;
}

#panel > .content {
  background: blue;
  overflow-y: auto;
}

#panel > footer {
  background: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div id="panel" class="flex-container">
  <header class="flex-fixed">This is the header</header>
  <div class="flex-var content">
    <ul id="list">
    </ul>
  </div>
  <footer class="flex-fixed">This is the footer</footer>
</div>

但是,面板的内容是动态的,通常需要托管另一个弹性框(例如另一个页眉、页脚、内容)。这在 Chrome 和 Firefox 中似乎很容易做到,但我无法让 IE10 和 IE11 正常工作:http://jsfiddle.net/Pyn9e/11/

正如您将在 IE10 和 IE11 中看到的那样,面板现在溢出窗口而不是开始滚动。

function setMaxHeight() {
  var maxHeight = $(window).height() - 16;
  $("#panel").css({
    height: maxHeight + "px"
  });
}

$(window).on("resize", function() {
  setMaxHeight();
});
setMaxHeight();

var i = 0;

setInterval(function() {
  var ul = $("#list");
  ++i;
  if (i <= 20) {
    var li = $("<li>").text("Item " + i);
    ul.append(li);
  } else if (i <= 40) {
    ul.children().last().remove();
  } else {
    i = 0;
  }
}, 60);
.flex-container {
  display: -ms-flex;
  display: flex;
  flex-direction: column;
  -ms-flex-direction: column;
}

.flex-fixed {
  flex: 0 0 auto;
  -ms-flex: 0 0 auto;
}

.flex-var {
  flex: 0 1 auto;
  -ms-flex: 0 1 auto;
}

#panel {
  position: absolute;
  top: 8px;
  left: 8px;
  width: 200px;
  color: white;
  overflow: hidden;
}

#panel > header {
  background: red;
}

#panel > .content {
  background: blue;
}

#panel > footer {
  background: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div id="panel" class="flex-container">
  <header class="flex-fixed">This is the header</header>
  <div class="flex-container flex-var content">
    <header class="flex-fixed">Content</header>
    <ul id="list" class="flex-var" style="overflow-y:scroll; padding: 0; margin:0">
    </ul>
  </div>
  <footer class="flex-fixed">This is the footer</footer>
</div>

谁能看到我可以在不过多改变布局的情况下支持 IE 的方法?谢谢。

【问题讨论】:

  • 我在 IE 上遇到了与嵌套 flexbox 相同的问题。这似乎是不可避免的
  • Modernizr 3.0 seems to offer a good solution here. 它检查不同级别的 flexbox 支持并向&lt;html&gt; 元素添加不同的类。 IE9 标记为no-flexbox no-flexboxtweener,IE10 标记为no-flexbox flexboxtweener,IE11 标记为flexbox flexboxtweener
  • 我在 IE11 中没有看到这个(你在 IE11 中看到的是哪个版本),IE11 不是 @Blazemonger 建议的 tweener 版本。也就是说,在此 repo 中有许多解决所有浏览器中互操作问题的建议:github.com/philipwalton/flexbugs 可能会有所帮助。

标签: html css internet-explorer internet-explorer-10 flexbox


【解决方案1】:

IE 前缀值应该是-ms-flexbox,而不是-ms-flex

在您的两个示例中,您都使用了不正确的浏览器前缀值:-ms-flex

正确的flexbox prefix value for IE-ms-flexbox

另外,您不需要使用 JavaScript 在窗口调整大小时设置容器的高度;您可以通过在 CSS 中设置 #panelheight 来实现相同的结果:

#panel {
    …
    height: calc(100% - 16px);
    …
}

或者,因为#panel 是绝对定位的,通过设置bottom 位置:

#panel {
    …
    bottom: 8px;
    …
}

var i = 0;

setInterval(function() {
  var ul = $("#list");
  ++i;
  if (i <= 20) {
    var li = $("<li>").text("Item " + i);
    ul.append(li);
  } else if (i <= 40) {
    ul.children().last().remove();
  } else {
    i = 0;
  }
}, 60);
.flex-container {
  display: -ms-flexbox;
  display: flex;
  flex-direction: column;
  -ms-flex-direction: column;
}

.flex-fixed {
  flex: 0 1 auto;
  -ms-flex: 0 1 auto;
}

.flex-var {
  flex: 0 1 auto;
  -ms-flex: 0 1 auto;
}

#panel {
  position: absolute;
  top: 8px;
  left: 8px;
  bottom: 8px;
  width: 200px;
  color: white;
  overflow: hidden;
}

#panel > header {
  background: red;
}

#panel > .content {
  background: blue;
  overflow-y: auto;
}

#panel > footer {
  background: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div id="panel" class="flex-container">
  <header class="flex-fixed">This is the header</header>
  <div class="flex-container flex-var content">
    <header class="flex-fixed">Content</header>
    <ul id="list" class="flex-var" style="overflow-y:scroll; padding: 0; margin:0">
    </ul>
  </div>
  <footer class="flex-fixed">This is the footer</footer>
</div>

【讨论】:

  • 他们更容易把 Modernizr 扔进去,而不是正确阅读文档:P
猜你喜欢
  • 2021-04-28
  • 2017-02-11
  • 1970-01-01
  • 2021-02-23
  • 1970-01-01
  • 2017-11-23
  • 2017-11-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多