字体颜色纯粹使用CSS过渡动画

时间:2015-08-09 06:09:52

标签: html css css3 css-transitions css-animations

我正在玩一些CSS过渡,并试图解决这个问题。 我希望文字颜色从左到右改变INSTEAD的背景。

以下是代码:( codepen here:http://codepen.io/xkurohatox/pen/zGboMz

HTML:

     <a href="#" class="added_to_cart wc-forward" title="View Cart">View Cart</a>

CSS:

 a.added_to_cart.wc-forward {
 font-size:100px;
color:black;
}  
a.added_to_cart.wc-forward {
display: inline-block;
vertical-align: middle;
-webkit-transform: translateZ(0);
transform: translateZ(0);
box-shadow: 0 0 1px rgba(0, 0, 0, 0);
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
-moz-osx-font-smoothing: grayscale;
position: relative;
-webkit-transition-property: color;
transition-property: color;
-webkit-transition-duration: 0.5s;
transition-duration: 0.5s;
 }
a.added_to_cart.wc-forward:before {
content: "";
position: absolute;
z-index: -1;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: red;
-webkit-transform: scaleX(0);
transform: scaleX(0);
-webkit-transform-origin: 0 50%;
transform-origin: 0 50%;
-webkit-transition-property: transform;
transition-property: transform;
-webkit-transition-duration: 0.5s;
transition-duration: 0.5s;
-webkit-transition-timing-function: ease-out;
transition-timing-function: ease-out;
 }
a.added_to_cart.wc-forward:hover, a.added_to_cart.wc-forward:focus,  a.added_to_cart.wc-forward:active {
 color: white;
 }
 a.added_to_cart.wc-forward:hover:before, a.added_to_cart.wc-forward:focus:before, a.added_to_cart.wc-forward:active:before {
 -webkit-transform: scaleX(1);
 transform: scaleX(1);
 -webkit-transition-timing-function: cubic-bezier(0.52, 1.64, 0.37, 0.66);
 transition-timing-function: cubic-bezier(0.52, 1.64, 0.37, 0.66);
  }

已经尝试了显而易见的事情,例如将背景颜色更改为颜色并使用translateZ进行游戏。我不认为我之前见过css字体颜色过渡所以我很乐意看看这里是否有人有足够的才能想出一个。

从这里获得基本代码:https://github.com/IanLunn/Hover

提前谢谢你xx:)

2 个答案:

答案 0 :(得分:2)

绝对可以使用一点点诡计来完成。这是一个使用遮罩来显示彩色文本的工作示例。

Codepen:http://codepen.io/maxlaumeister/pen/eNXBaW

现场演示:

&#13;
&#13;
a.added_to_cart.wc-forward {
  font-size:100px;
  color:black;
}  
a.added_to_cart.wc-forward {
  display: inline-block;
  vertical-align: middle;
  -webkit-transform: translateZ(0);
  transform: translateZ(0);
  box-shadow: 0 0 1px rgba(0, 0, 0, 0);
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  -moz-osx-font-smoothing: grayscale;
  position: relative;
  -webkit-transition-property: color;
  transition-property: color;
  -webkit-transition-duration: 0.5s;
  transition-duration: 0.5s;
}
.mask {
  display: inline-block;
  position: absolute;
  top: 0;
  left: 0;
  width: 0px;
  overflow: hidden;
  -webkit-transform-origin: 0 50%;
  transform-origin: 0 50%;
  -webkit-transition-property: width;
  transition-property: width;
  -webkit-transition-duration: 0.5s;
  transition-duration: 0.5s;
  -webkit-transition-timing-function: ease-out;
  transition-timing-function: ease-out;
  text-shadow:
   -1px -1px 0 red,  
    1px -1px 0 red,
    -1px 1px 0 red,
     1px 1px 0 red;
}

.mask a.added_to_cart.wc-forward {
  color: red;
}

#container:hover .mask {
  width: 500px;
  -webkit-transition-timing-function: cubic-bezier(0.52, 1.64, 0.37, 0.66);
  transition-timing-function: cubic-bezier(0.52, 1.64, 0.37, 0.66);
}

body {
  margin: 0;
}

a.added_to_cart.wc-forward.under {
  position: absolute;
  left: 0;
  top: 0;
}

#inner {
  width: 500px;
}
&#13;
<div id="container">
  <a href="#" class="added_to_cart wc-forward under" title="View Cart">View Cart</a>
  <div class="mask">
    <div id="inner">
      <a href="#" class="added_to_cart wc-forward" title="View Cart">View Cart</a>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

我从a.added_to_cart.wc-forward:before

中删除了背景颜色

<强> CSS

a.added_to_cart.wc-forward {
font-size:100px;
  color:black;
}  
a.added_to_cart.wc-forward {
  display: inline-block;
  vertical-align: middle;
  -webkit-transform: translateZ(0);
  transform: translateZ(0);
  box-shadow: 0 0 1px rgba(0, 0, 0, 0);
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  -moz-osx-font-smoothing: grayscale;
  position: relative;
  -webkit-transition-property: color;
  transition-property: color;
  -webkit-transition-duration: 0.5s;
  transition-duration: 0.5s;
}
a.added_to_cart.wc-forward:before {
  content: "";
  position: absolute;
  z-index: -1;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;

  -webkit-transform: scaleX(0);
  transform: scaleX(0);
  -webkit-transform-origin: 0 50%;
  transform-origin: 0 50%;
  -webkit-transition-property: transform;
  transition-property: transform;
  -webkit-transition-duration: 0.5s;
  transition-duration: 0.5s;
  -webkit-transition-timing-function: ease-out;
  transition-timing-function: ease-out;
}
a.added_to_cart.wc-forward:hover, a.added_to_cart.wc-forward:focus, a.added_to_cart.wc-forward:active {
  color: red;
}
a.added_to_cart.wc-forward:hover:before, a.added_to_cart.wc-forward:focus:before, a.added_to_cart.wc-forward:active:before {
  -webkit-transform: scaleX(1);
  transform: scaleX(1);
  -webkit-transition-timing-function: cubic-bezier(0.52, 1.64, 0.37, 0.66);
  transition-timing-function: cubic-bezier(0.52, 1.64, 0.37, 0.66);
}

<强> HTML

<a href="#" class="added_to_cart wc-forward" title="View Cart">View Cart</a>

Demo here