字体颜色动画纯粹与CSS过渡



我正在玩一些CSS过渡,并试图弄清楚这一点。我希望文本颜色改变(从左到右)而不是背景。

代码如下:(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);
  }

已经尝试了一些明显的方法,例如将background-color更改为color,并使用translateZ。我想我以前从未见过css字体颜色过渡,所以我很想看看这里是否有人有足够的天赋来想出一个。

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

提前感谢xx:)

这绝对可以用一点小技巧来完成。下面是一个使用蒙版显示彩色文本的工作示例。

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

现场演示:

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;
}
<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>

我去掉了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);
}

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

相关内容

  • 没有找到相关文章

最新更新