背景图像使用Flexbox重新加载时闪烁(使背景显示为瞬间)

时间:2017-04-17 15:22:25

标签: javascript html css flexbox background-image

因此,当我刷新时,我的背景图像会出现闪烁问题。这只适用于Chrome / Edge,但在Firefox中运行良好...

CSS:

.site {
  display: flex;
  display: -webkit-flex;
  display: -moz-flex;
  display: -ms-flexbox;
  flex-direction: column;
  -webkit-flex-direction: column;
  -moz-flex-direction: column;
  -ms-flex-direction: column;
  -moz-box-direction: column;
  height: 100vh;
  /*width: 100vw;*/
  margin: 0;
}

.site-content {
  position: relative;
  flex: 1;
  -moz-box-flex: 1;
  -webkit-flex: 1;
  -moz-flex: 1;
  -ms-flex: 1;
}

.background-image {
  position: absolute;
  height: 100%;
  width: 100%;
  filter: blur(5px);
  background: #4c6c87 url("background.png");
  background-repeat: no-repeat;
  background-size: cover;
  top: 50%;
  transform: translateY(-50%);
}

.center {
  display: flex;
  align-items: center;
  justify-content: center;
}

.grid-top {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
}

.grid-bottom {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
}

.container {
  width: 960px;
  margin: 0 auto;
}

HTML:

<body class="site">
  <header class="top">
    <div class="container grid-top">
      <h1>PlaceHolder</h1>
    </div>
  </header>
  <main class="center site-content">
    <div class="background-image scrollable"></div>
    <h3 class="leftSlide">PlaceHolder</h3>
  </main>
  <footer class="bottom">
    <div class="container grid-bottom">
      <h1>PlaceHolder</h1>
    </div>
  </footer>
</body>

我尝试用一​​些JS预加载背景..然而这并没有起作用..

JS:

var a = new Image();

var img = "background.png";

a.onload = function() {

    document.querySelector('.background-image').style.backgroundImage = 'url(' + img + ')';
}

a.src = img;

我很欣赏任何见解。 感谢~~

0 个答案:

没有答案