【问题标题】:Javascript animate a style change using CSS3 transitionsJavascript 使用 CSS3 过渡动画样式更改
【发布时间】:2017-10-09 00:36:40
【问题描述】:

我有一个具有背景渐变的 div,其值是使用 javascript 动态设置的。有时,我需要更改渐变值(可以是颜色或颜色停止位置)。我想在不使用 javascript 的情况下为这些更改设置动画(不要误会我的意思,我使用 javascript 设置了渐变值,但我想使用 CSS3 为 thise 设置动画)。我尝试像使用 CSS 一样设置 transition 属性。这是一个 MWE:

function getPercent() {
  return Math.random() * 100;
}
setInterval(() => {
  const per = getPercent();
  document.getElementById('foo').style.background =
    `linear-gradient(to right, #aebcbf 0%,#aebcbf ${per}%,#0a0e0a ${per}%,#0a0e0a 100%`
}, 1000)
.container {
  transition: all 1s ease;
  background: linear-gradient(to right, #aebcbf 0%,#6e7774 50%,#0a0e0a 51%,#0a0809 100%);
  width: 150px;
  height: 10px;
}
<div class="container" id="foo"></div>

【问题讨论】:

标签: javascript html css


【解决方案1】:

希望对你有用

background: linear-gradient(269deg, #ffffff, #ff0000);
background-size: 400% 400%;
-webkit-animation: AnimationName 23s ease infinite;
-moz-animation: AnimationName 23s ease infinite;
-o-animation: AnimationName 23s ease infinite;
animation: AnimationName 23s ease infinite;
@-webkit-keyframes AnimationName {
    0%{background-position:0% 50%}
    50%{background-position:100% 50%}
    100%{background-position:0% 50%}
}
@-moz-keyframes AnimationName {
    0%{background-position:0% 50%}
    50%{background-position:100% 50%}
    100%{background-position:0% 50%}
}
@-o-keyframes AnimationName {
    0%{background-position:0% 50%}
    50%{background-position:100% 50%}
    100%{background-position:0% 50%}
}
@keyframes AnimationName {
    0%{background-position:0% 50%}
    50%{background-position:100% 50%}
    100%{background-position:0% 50%}
}

有关 CSS 过渡的更多信息,请查看此

【讨论】:

  • 这应该是一个评论,因为你没有回答这个问题。
  • 这好多了,但它仍然没有回答问题。 OP 想使用 JS 为渐变设置动画。
【解决方案2】:

不幸的是,background-image 是不可动画的(更多 info about background-image)。因此,动画 CSS 渐变是一个问题。

要使用纯 CSS 解决此问题,您可以创建一个包含新渐变的 :before。当你想显示新的渐变时,给它opacity: 0 并将其更改为opacity: 1

最终我们可以做到以下几点:

.container{
  width: 150px;
  height: 10px;
  background: linear-gradient(to right, #aebcbf 0%, #6e7774 50%, #0a0e0a 51%, #0a0809 100%);
  position: relative;
  z-index: 100;
}
.container:before{
  background: linear-gradient(to right, #aebcbf 0%, #6e7774 10%, #0a0e0a 51%, #0a0809 100%);
  content: '';    
  display: block;
  height: 100%;
  position: absolute;
  top: 0; 
  left: 0;
  opacity: 0;
  width: 100%;
  z-index: -100;
  transition: opacity 0.45s;
}
.container:hover:before {
  opacity: 1;
}
<div class="container" id="foo"></div>

编辑:

如果您想使渐变动态化,请使用一个变量来跟踪您上次更改的元素,例如:

var swapElement = 0;
function getPercent() {
  return Math.random() * 100;
}
setInterval(() => {
  const per = getPercent();
  if(swapElement == 0){
     //change gradient of :before
     //set opacity to 1
     swapElement = 1;
  } else {
     document.getElementById('foo').style.background =
    `linear-gradient(to right, #aebcbf 0%,#aebcbf ${per}%,#0a0e0a ${per}%,#0a0e0a 100%`
}, 1000);
     //set opacity :before to 0
     swapElement = 0;
  }

【讨论】:

  • 您可以更改容器和之前元素的线性渐变。它们可以是完全动态的。
猜你喜欢
  • 2012-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-07
  • 1970-01-01
相关资源
最近更新 更多