【问题标题】:Displaying a flexbox in old browsers在旧浏览器中显示弹性框
【发布时间】:2016-11-19 01:27:35
【问题描述】:

首先我将解释我到目前为止所拥有的; 一个始终位于页面中间的动态画布,它的宽度由 javascript 计算计算,因此它可能会有所不同,之后将一些 flexbox div 添加到它的两侧并且它们平等地填充页面的其余部分(它的工作方式相同即使使用 CTRL 调整大小):

https://jsfiddle.net/62g1mqw0/2/

<div class="wrapper">
  <div class="divA"></div>
  <div class="divB">
    <canvas id="canvas"></canvas>
  </div>
  <div class="divC"></div>
</div>
<style type="text/css">
  * {
  box-sizing: border-box;
}
body {
  height: 100vh;
  margin: 0;
}
.wrapper {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  width: 100%;
  height: 100%;
}
.wrapper div {
  -webkit-box-flex: 1;
      -ms-flex: 1;
          flex: 1;
  border: 1px solid;
}
.wrapper .divB {
  border: none;
}
#canvas {
  vertical-align:top;
}
</style>

<script>
var canvas = document.getElementById("canvas");

var WIDTH = 500; // This could be anything! it comes from
//previous calculations which I did not include here because they are irelevant

document.getElementsByClassName('divB')[0].style.flex = '0 0 ' + WIDTH + 'px';

var width = document.getElementsByClassName('divB')[0].getBoundingClientRect().width;
var height = document.getElementsByClassName('divB')[0].getBoundingClientRect().height;

document.getElementById("canvas").style.width = width + 'px';
document.getElementById("canvas").style.height = height + 'px';
var ctx = canvas.getContext("2d");
ctx.fillStyle = "blue";
ctx.fillRect(0, 0, width, height);

// resize handler Ref: https://developer.mozilla.org/en-US/docs/Web/Events/resize
(function() {
  window.addEventListener("resize", resizeThrottler, false);
  var resizeTimeout;

  function resizeThrottler() {
    if (!resizeTimeout) {
      resizeTimeout = setTimeout(function() {
        resizeTimeout = null;
        actualResizeHandler();
      }, 66);
    }
  }

  function actualResizeHandler() {
    var width = document.getElementsByClassName('divB')[0].getBoundingClientRect().width;
    var height = document.getElementsByClassName('divB')[0].getBoundingClientRect().height;

    document.getElementById("canvas").style.width = width + 'px';
    document.getElementById("canvas").style.height = height + 'px';
    ctx = canvas.getContext("2d");
    ctx.fillStyle = "red";
    ctx.fillRect(0, 0, width, height);
  }
}());
</script>

正如您在 jsFiddle 上看到的,总的结果是页面的宽度和高度的 100% 被画布和 flexbox 填充。

现在,在现代浏览器中你不会看到问题,但在旧浏览器中你会看到奇怪的事情;一个 div 浮动到下一个,一些黑点等。对我来说,支持旧浏览器非常重要,因为我使用 cordova 编译它,而使用旧版本的 Android 用户仍然使用旧浏览器,他们可能会觉得我的应用程序很奇怪。我已经在 KitKat 和一些更旧的版本上对其进行了测试,但我不知道为什么它们不能正确支持 flexbox。我添加了 -webkit 行,但它仍然没有帮助。我什至不介意去另一个完全不同的不涉及 flexbox 的解决方案

【问题讨论】:

标签: javascript android html css flexbox


【解决方案1】:

您可能会通过使用 polyfill 为自己节省大量时间和挫败感 - 为了获得完整的浏览器支持,您将完全需要。

最受欢迎的似乎是Flexibility

如果您不想走 polyfill 路线,您可以关注几年前 this question 的一些答案。

【讨论】:

  • 我刚刚尝试了灵活性,但没有帮助。我认为问题出在我的画布与 div 交互的方式上……您提供的 stackoverflow 链接也无济于事,因为我的情况更复杂。编辑:我不确定除了包含它之外我应该如何使用灵活性
  • 来自 GitHub 上的自述文件:“如果您只针对 Internet Explorer 10 及更低版本,请在 CSS 中的任何 display: flex 声明之前添加 -js-display: flex 声明”和“如果您'针对其他浏览器,请使用 data-style 属性提醒这些浏览器注意您的更改。”我假设您已经尝试过这两种方法?
猜你喜欢
  • 2013-02-20
  • 2012-12-20
  • 1970-01-01
  • 2017-09-16
  • 2022-10-20
  • 2020-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多