简体   繁体   English

底层图像加载后平滑设置背景颜色不透明度的动画

[英]Smoothly animate background color opacity after underlying image loads

I have a div with a black background. 我有一个黑色背景的div。 When my page loads, I make a call for an image and then load that image into a div behind the main div . 当我的页面加载,我拨打电话一个image ,然后是加载image到一个div的主要背后div Then I want to smoothly fade the overlaying div to have an opacity so that the image underneath is displayed, but without impacting the opacity of content in the overlaying div . 然后,我要平滑淡入淡出叠加div以使其具有不透明度,以便显示下面的图像,但又不影响叠加div中内容的不透明度。

What I have isn't really working at all: https://jsfiddle.net/n7t2xmha/3/ 我所拥有的根本无法工作: https//jsfiddle.net/n7t2xmha/3/

  • The animation is not smooth 动画不流畅
  • The opacity is not accurate 不透明度不准确
  • The text does not stay solid 文字不固定

Code: 码:

<div class="outerdiv">
    <div class="innerdiv">
    </div>
    <p>
        content - should remain solid white
    </p>
</div>

.outerdiv {
    background-color: black;
    position: relative;
    display: block;
    height: 500px;
    width: 500px;
    color: white;
    -moz-transition: all 1s linear;
    -o-transition: all 1s linear;
    -webkit-transition: all 1s linear;
    transition: all 1s linear;
}

.outerdiv-opaque {
    opacity: 0.9 !important;
}

.innerdiv {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index=-1;
}

JS JS

var innerDiv = $('.innerdiv');
setTimeout(function() {
    innerDiv.css('background-image', 'url(http://i.stack.imgur.com/MxR09.png)');
    var outerdiv = $('.outerdiv');
    setTimeout(function() {
        outerdiv.addClass('outerdiv-opaque');
    }, 500);

}, 1000)

Separate the timeouts functions. 单独设置超时功能。 modify the .outerdiv-opaque class 修改.outerdiv-opaque类

   .outerdiv-opaque {
      background-color: white;
    }

your timeOut functions after separating would look like this 分离后的timeOut函数如下所示

    var innerDiv = $('.innerdiv');
setTimeout(function() {
  innerDiv.css('background-image', 'url(http://i.stack.imgur.com/MxR09.png)');
}, 1000)

 var outerdiv = $('.outerdiv');
  setTimeout(function() {
    outerdiv.addClass('outerdiv-opaque');
  }, 500);

I would use a pseudo, like this, which will keep your markup as is and as the opacity is on the pseudo it won't effect any other element. 我将使用一个伪像这样,它将使您的标记保持不变,并且由于伪像的opacity不会影响任何其他元素。

Instead of a script, I used an extra step on the animation, where I told it to keep its opacity at 1 up until 60% of the animation time before it should start to fade. 我在动画上使用了一个额外的步骤,而不是脚本,在该步骤中,我告诉它将opacity保持为1,直到动画开始褪色之前的动画时间的60%。

 .outerdiv { position: relative; height: 500px; width: 500px; color: white; background: url(http://i.stack.imgur.com/MxR09.png); } .outerdiv::before { content: ''; background-color: black; position: absolute; top: 0; right: 0; bottom: 0; left: 0; opacity: 0.5; animation: fade 2s linear; } .innerdiv { position: absolute; top: 0; right: 0; bottom: 0; left: 0; } p { position: relative; } @keyframes fade { 0% { opacity:1 } 60% { opacity:1 } 100% { opacity:0.5 } } 
 <div class="outerdiv"> <div class="innerdiv"> </div> <p> content - should remain solid white </p> </div> 

There are literally a dozen ways to do this. 实际上有十二种方法可以做到这一点。 Here are four basic examples which work smoothly. 这是四个可以顺利运行的基本示例。

Using CSS Transitions 使用CSS过渡

HTML: HTML:

<div class="container">
  <div class="outerdiv">
  </div>
  <div class="innerdiv">
  </div>
  <p>
    content - should remain solid white
  </p>
</div>

CSS: CSS:

.container,.outerdiv {
  background-color: black;
  position: relative;
  display: block;
  height: 500px;
  width: 500px;
  color: white;
}

.outerdiv,.innerdiv {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
}

.outerdiv{
  z-index:1;
  transition: .5s opacity linear;
}

.innerdiv{
  background-image: url(http://i.stack.imgur.com/MxR09.png);
}

.outerdiv.fadeout{
  opacity:0
}

.container p{
  position:relative;
  z-index:3;
}

JS: JS:

// wait 1 second, add the fadeout class, let the CSS do the rest
setTimeout(function(){
  document.querySelector('.outerdiv').classList.add('fadeout')
},1000);

See it in action: https://jsfiddle.net/kmm8e0x7/8/ 实际观看: https : //jsfiddle.net/kmm8e0x7/8/


Using CSS Animation 使用CSS动画

HTML: same as above HTML:与上述相同

CSS: CSS:

.container,.outerdiv {
  background-color: black;
  position: relative;
  display: block;
  height: 500px;
  width: 500px;
  color: white;
}

.outerdiv,.innerdiv {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
}

.outerdiv{
  z-index:1;
}

.innerdiv{
  background-image: url(http://i.stack.imgur.com/MxR09.png);
}

.outerdiv{
  animation: fadeout .5s linear forwards 1s;      
  /* 
    Which is shorthand for:
      animation-name: fadeout 
      animation-duration: .5s;
      animation-timing-function: linear
      animation-fill-mode:forwards;
      animation-delay: 1s 
  */
}

.container p{
  position:relative;
  z-index:3;
}

@keyframes fadeout{
  from{opacity:1}
  to{opacity:0}
}

JS: none ( animation-delay property removes the need for setTimeout ) JS:无( animation-delay属性消除了对setTimeout的需求)

See it in action: https://jsfiddle.net/kmm8e0x7/7/ 实际观看: https : //jsfiddle.net/kmm8e0x7/7/


Using JavaScript 使用JavaScript

HTML: as above HTML:如上所述

CSS: CSS:

.container,.outerdiv {
  background-color: black;
  position: relative;
  display: block;
  height: 500px;
  width: 500px;
  color: white;
}

.outerdiv,.innerdiv {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
}

.outerdiv{
  z-index:1;
  transition: .5s opacity linear;
}

.innerdiv{
  background-image: url(http://i.stack.imgur.com/MxR09.png);
}

.container p{
  position:relative;
  z-index:3;
}

JS: JS:

var el = document.querySelector('.outerdiv');

function fadeout(){
  el.style.opacity -= 0.01;

  if(el.style.opacity !== 0){
      requestAnimationframe(fadeout);
      // this could just as easily be setTimeout(fadeout,t) where t = an increment of time after which to call the next frame
  }
}

// just use setTimeout to wait for 1 second before starting the fadeout
setTimeout(fadeout,1000);

See it in action: https://jsfiddle.net/kmm8e0x7/6/ 实际观看: https : //jsfiddle.net/kmm8e0x7/6/


Using jQuery 使用jQuery

HTML: same as above HTML:与上述相同

CSS: same as above CSS:同上

JS: JS:

$('.outerdiv').animate({
  'opacity': '0'
}, 500);

See it in action: https://jsfiddle.net/kmm8e0x7/5/ 实际观看: https : //jsfiddle.net/kmm8e0x7/5/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM