简体   繁体   English

如何在不丢失动画的情况下将线性渐变应用于图像?

[英]How to apply linear-gradient to image without loosing animation?

This is my code:这是我的代码:

HTML: HTML:

<div class="hidden">
      <img src="img/homebg/bg2.jpg" />
      <img src="img/homebg/bg3.jpg" />
      <img src="img/homebg/bg4.jpg" />
      <img src="img/homebg/bg5.jpg" />
    </div>

    <div class="slider">
      <div class="container">
        <h1>Мышонок</h1>
      </div>
    </div>

CSS: CSS:

/* Slider */
.slider {
  height: 100vh;

  background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)),
    url(../img/homebg/bg1.jpg);
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;

  animation: slide 18s infinite;
  transition: 100ms ease-in-out;
}

.slider h1 {
  color: #fff;
  font-size: 50px;
  padding-top: 40px;
  text-transform: uppercase;
}

.hidden {
  display: none;
}

@keyframes slide {
  0% {
    background-image: url(../img/homebg/bg1.jpg);
  }

  20% {
    background-image: url(../img/homebg/bg2.jpg);
  }
  40% {
    background-image: url(../img/homebg/bg3.jpg);
  }
  60% {
    background-image: url(../img/homebg/bg4.jpg);
  }
  80% {
    background-image: url(../img/homebg/bg5.jpg);
  }
  100% {
    background-image: url(../img/homebg/bg1.jpg);
  }
}

I need every image in keyframe to have also linear-gradient like in .slider image-background, but if I apply it like this :我需要关键帧中的每个图像也像 .slider 图像背景一样具有线性渐变,但如果我像这样应用它:

20% {
    background-image: linear-gradient(
      rgba(0, 0, 0, 0.5),
      rgba(0, 0, 0, 0.5)
    ), url(../img/homebg/bg2.jpg);
  }

Then I lose the ease-in-out transition.然后我失去了缓入缓出过渡。 The image just appears instantly at 20%, it does have the gradient, but it does not transition ease-in-out.图像仅以 20% 的比例立即出现,它确实具有渐变,但不会过渡缓入缓出。 How to fix this?如何解决这个问题?

Solution:解决方案:

I just added a div with overlay separately instead of using the linear-gradient property to background-image:我只是单独添加了一个带有叠加层的 div,而不是将线性渐变属性用于背景图像:

HTML: HTML:

<div class="slider">
      <div class="overlay">
        <div class="container">
          <h1>Мышонок</h1>
        </div>
      </div>
    </div>

CSS: CSS:

.overlay {
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 9999;
}

.slider h1 {
  font-family: "Bangers", cursive;
  font-size: 50px;
  color: #fff;
  padding-top: 40px;
  text-transform: uppercase;
  z-index: 1;
}

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

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