缩放元素时,部分边框在Firefox上不可见

时间:2019-03-01 08:08:30

标签: html css reactjs firefox

我正在使用提出的解决方案here按比例缩放图像以填充所有可用空间。在Chrome上一切正常,但在Firefox上边框出现问题:

Chrome: enter image description here

Firefox: enter image description here

示例代码可用here (codesandbox)。我想了解Firefox上发生了什么以及如何解决。

var wrapper = document.querySelector('#scaler');
var iteration = 20
var direction = 1;
setInterval(function() {
  direction = iteration > 40 ? -1 : iteration < 20 ? 1 : direction;
  iteration+=direction;
  wrapper.style.transform = "scale(" + (iteration * .01) + ")"
}, 1000)
.App {
  font-family: sans-serif;
  text-align: center;
  margin-top: 50px;
}

.root {
  width: 100%;
  text-align: center;
  position: relative;
  -webkit-flex: 1 1 auto;
  -ms-flex: 1 1 auto;
  flex: 1 1 auto;
  max-height: 40vh;
  height: 100vh;
}

.image-wrapper {
  height: 100%;
  width: 100%;
  position: absolute;
  padding: 0 10px;
  box-sizing: border-box;
  will-change: opacity, transform;
}

.overlay-wrapper {
  position: absolute;
  left: 50%;
  top: 50%;
  -webkit-transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
  -webkit-transform-origin: center center;
  -ms-transform-origin: center center;
  transform-origin: center center;
  border: 1px magenta solid;
}

.overlay-wrapper img {
  display: block;
}
<div id="root">
  <div class="App">
    <div class="root">
      <div class="image-wrapper" id="scaler" style="transform: scale(.2);">
        <div class="overlay-wrapper"><img alt="" src="https://dummyimage.com/1420x802/ccc/333"></div>
      </div>
    </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:0)

该解决方案如何?

.container {
  width: auto;
  height: 100vh;
  max-width: 100vw;
  max-height: 100vh;
  margin: 0 auto;
  overflow: hidden;
  position: relative;
}
.container img {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: block;
  max-width: 100%;
  box-sizing: border-box;
  max-height: 100%;
  height: auto;
  width: auto;
  border: 1px solid grey;
  margin: auto;
}

body {
  margin: 0;
  padding: 0;
}

/* for demonstration purpose */
body {
  animation: mw 10s alternate infinite linear;
  background: gold;
  margin: 0 auto;
}


@keyframes mw {
  from {
    width: 400px;
  }
  to {
    width: 200px;
  }
}
<div class="container">
  <img src="https://dummyimage.com/1420x802/ccc/333" alt="">
</div>

有关无动画版本,请参见此codepen:https://codepen.io/HerrSerker/pen/WmrZZa?editors=1100

您还应该看到以下答案:https://stackoverflow.com/a/26967278/476951