有没有办法让渐变保留其所应用元素的初始宽度?

我知道渐变只是与它们所应用的元素的尺寸相匹配。尽管如此,有没有办法在视觉上使渐变静态并屏蔽掉不应该可见的部分?

我的目的是让倒计时器在接近其演变的终点时变得更暗。目前,我的渐变保留了左侧和右侧的颜色,同时简单地减少了中间的颜色:

(function() {

  function resetCountdown() {

    window.requestAnimationFrame(function() {

      document.getElementById("countdown-evolution").classList.remove("countdown-reset");

      window.requestAnimationFrame(function() {

        document.getElementById("countdown-evolution").classList.add("countdown-reset");

      });

    });

  }

  resetCountdown();

  document.getElementById("countdown-evolution").addEventListener("transitionend", resetCountdown);

})();

/* Background */


#countdown-background {

  height: 50px;

  width: 100%;

  box-sizing: border-box;

  border: 1px solid #ebebeb;

  background-color: #ffffff;

}



/* Fill */


#countdown-evolution {

  height: 100%;

  width: 100%;

  transform-origin: left;

  background: linear-gradient(to right, #6419cd, #3273fa);

}



/* Reset */


.countdown-reset {

  transition: transform 15s linear;

  transform: scaleX(0);

}



/* Reference */


.fixed-background {

  height: 50px;

  width: 100%;

  box-sizing: border-box;

  border: 1px solid #ebebeb;

  background: linear-gradient(to right, #6419cd, #3273fa);

}

<!DOCTYPE html>

<html>


<head>

  <meta charset="UTF-8">

  <title>Countdown</title>

</head>


<body>

  <div id="countdown-background">

    <div id="countdown-evolution"></div>

  </div>

  <div class="fixed-background"></div>

</body>


</html>

我已经尝试过制作countdown-background渐变和countdown-evolution纯色,这基本上就是我所追求的。然而,这带来的问题比它解决的问题还要多。因为现在我必须修复我的倒计时器,但我似乎无法让它看起来和以前一样:



白衣非少年
浏览 71回答 2
2回答

米脂

使用另一个元素作为窗帘并与 css 关键帧一起进行绝对定位:document.querySelector("#countdown-evolution-curtain").addEventListener('animationend', () => {&nbsp; console.log('Animation ended');});/* Background */#countdown-background {&nbsp; height: 50px;&nbsp; width: 100%;&nbsp; box-sizing: border-box;&nbsp; border: 1px solid #ebebeb;&nbsp; background-color: #ffffff;&nbsp; position: relative;}#countdown-background div {&nbsp; position: absolute;&nbsp; right: 0;&nbsp; top: 0;}/* Fill */#countdown-evolution-curtain {&nbsp; background: #fff;&nbsp; height: 100%;&nbsp; width: 0%;&nbsp; animation: reveal 10s linear;}#countdown-evolution {&nbsp; height: 100%;&nbsp; width: 100%;&nbsp; background: linear-gradient(to right, #6419cd, #3273fa);}@keyframes reveal {&nbsp; 0% {&nbsp; &nbsp; width: 0%;&nbsp; }&nbsp; 100% {&nbsp; &nbsp; width: 100%;&nbsp; }}<div id="countdown-background">&nbsp; <div id="countdown-evolution"></div>&nbsp; <div id="countdown-evolution-curtain"></div></div>

慕运维8079593

有多种方法可以仅用一个元素来实现这一目标:在上面使用额外的白色层和另一个渐变对渐变颜色停止点使用固定值用于background-clip通过填充动画来剪辑内容区域中的背景使用mask图层使用伪元素作为额外层/* Reference */.reference {&nbsp; height: 50px;&nbsp; border: 1px solid #ebebeb;&nbsp; background: linear-gradient(to right, #6419cd, #3273fa);}/* (1) */.first {&nbsp; background:&nbsp;&nbsp; &nbsp; linear-gradient(#fff,#fff) right no-repeat,&nbsp; &nbsp; linear-gradient(to right, #6419cd, #3273fa);&nbsp; animation:first 5s linear forwards;}&nbsp;@keyframes first{&nbsp; from {&nbsp; &nbsp; background-size:0% 100%,auto;&nbsp; }&nbsp; to {&nbsp; &nbsp; background-size:100% 100%,auto;&nbsp; }}/* (2) */.second {&nbsp; background:linear-gradient(to right, #6419cd 0, #3273fa 100vw) left no-repeat;&nbsp; animation:second 5s linear forwards;}&nbsp;@keyframes second{&nbsp; from {&nbsp; &nbsp; background-size:100% 100%;&nbsp; }&nbsp; to {&nbsp; &nbsp; background-size:0% 100%;&nbsp; }}/* (3) */.third {&nbsp; background-clip:content-box;&nbsp; animation:third 5s linear forwards;}&nbsp;@keyframes third{&nbsp; from {&nbsp; &nbsp; padding-right:0%;&nbsp; }&nbsp; to {&nbsp; &nbsp; padding-right:100%;&nbsp; }}/* (4) */.fourth {&nbsp; -webkit-mask:linear-gradient(#fff,#fff) left no-repeat;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mask:linear-gradient(#fff,#fff) left no-repeat;&nbsp; animation:fourth 5s linear forwards;}&nbsp;@keyframes fourth{&nbsp; from {&nbsp; &nbsp; -webkit-mask-size:100% 100%;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mask-size:100% 100%;&nbsp; }&nbsp; to {&nbsp; &nbsp; -webkit-mask-size:0% 100%;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mask-size:0% 100%;&nbsp; }}/* (5) */.fifth{&nbsp; position:relative;}&nbsp;.fifth::before {&nbsp; content:"";&nbsp; position:absolute;&nbsp; background:#fff;&nbsp; top:0;&nbsp; right:0;&nbsp; bottom:0;&nbsp; animation:fifth 5s linear forwards;}@keyframes fifth{&nbsp; from {&nbsp; &nbsp; left:100%;&nbsp; }&nbsp; to {&nbsp; &nbsp; left:0%;&nbsp; }}<div class="first reference"></div><div class="second reference"></div><div class="third reference"></div><div class="fourth reference"></div><div class="fifth reference"></div><div class="reference"></div>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5