【问题标题】:How do I gradually change the background color of a element from left to right [duplicate]如何从左到右逐渐改变元素的背景颜色[重复]
【发布时间】:2020-04-06 23:14:43
【问题描述】:

我正在尝试使用关键帧来更改 a 标签的背景颜色

我的代码:

.button-container {
  width: 200px;
  height: 40px;
  display: flex;
  margin: 0 auto;

}
.button-container a{
  width: inherit;
  height: inherit;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
  background-image: linear-gradient(to right, #000, #000);
}

.button-container:hover a{
  animation: colorswitch 5s linear;
}

@keyframes colorswitch {
  from { 
    background-image: linear-gradient(to right, #ddd , #ddd);
  }
  to { 
    background-image: linear-gradient(to right, #ddd , #ddd);
  }
}
 <div class="button-container">
   <a href="#">View here</a>
 </div>

如何将 a 标签的背景颜色从 #000 更改为 #ddd 当我将鼠标悬停在它上面时从左向右移动?

【问题讨论】:

  • 所以背景图片是一个不可动画的属性。但是,您可以为 background-position 设置动画以在元素背景上滑动渐变,这可能是可接受的效果。
  • @WillD 你可能有一个sn-p吗?

标签: css css-animations


【解决方案1】:

这是一个使用线性渐变、背景位置和 css 过渡的图案。

从右到左:

button {
  background-image: linear-gradient(to right, red 40%,  blue 60%);
    
 background-size: 300% 100%;
  transition: background-position .5s;
  background-position: 0;
  
  border: none;
  padding: 1em;
}


button:hover {
   background-position: 100%;
}
&lt;button&gt;A button&lt;/button&gt;

从左到右:

button {
  background-image: linear-gradient(to right, red 40%,  blue 60%);
    
 background-size: 300% 100%;
  transition: background-position .5s;
  background-position: 100%;
  
  border: none;
  padding: 1em;
}


button:hover {
   background-position: 0%;
}
&lt;button&gt; A button&lt;/button&gt;

【讨论】:

  • 我怎样才能让这个从左到右移动呢?我尝试在线性渐变中改为“左”,但没有成功。
  • 编辑为包括两个方向
  • 就是这样。感谢支持
猜你喜欢
  • 2012-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-09
  • 1970-01-01
  • 2012-03-11
相关资源
最近更新 更多