CSS使用线性渐变过渡效果

65

我试图为一个背景由css线性渐变制作的按钮添加过渡动画,但是没有效果。

这是我的按钮的css样式。

a.button
{
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,@green), color-stop(100%,#a5c956));
-webkit-transition: background 5s linear;
}

a.button:hover
{
-webkit-gradient(linear, left top, left bottom, color-stop(0%,@greenhover), color-stop(100%,#89af37))
}

如果你对 @green 和 @greenhover 感到困惑,我在使用 .less 来制作我的 CSS。

这个有什么问题吗?有什么建议吗?


我只是想提示一下我的答案,因为你会发现所有的答案都涉及到透明度的改变,这并不令人满意。希望你们能同意并且发现它有所帮助。 - Biskrem Muhammad
9个回答

51

很遗憾,目前你无法过渡渐变。

因此,唯一可行的解决方法是使用具有所需渐变的额外元素,并转换其不透明度:

a.button {
  position: relative;
  z-index: 9;
  display: inline-block;
  padding: 0 10px;
  background: -webkit-gradient(linear, 0 0, 0 100%, from(green), to(#a5c956));
  background: -webkit-linear-gradient(top, green, #a5c956);
  background: -moz-linear-gradient(top, green, #a5c956);
  background: -o-linear-gradient(top, green, #a5c956);
  background: linear-gradient(top, green, #a5c956);
}

.button-helper {
  position: absolute;
  z-index: -1;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  background: -webkit-gradient(linear, 0 0, 0 100%, from(lime), to(#89af37));
  background: -webkit-linear-gradient(top, lime, #89af37);
  background: -moz-linear-gradient(top, lime, #89af37);
  background: -o-linear-gradient(top, lime, #89af37);
  background: linear-gradient(top, lime, #89af37);
  -webkit-transition: opacity 1s linear;
  -moz-transition: opacity 1s linear;
  -o-transition: opacity 1s linear;
  transition: opacity 1s linear;
}

a.button:hover .button-helper {
  opacity: 1;
}
<a href="#" class="button"><span class="button-helper"></span>button</a>


5
现在这已经过时了,伪元素可以很好地工作 - Dave Lunny
1
这并不总是提供相同的结果。您可以使用SVG来实现真正的过渡效果,而不是简单的过渡。 - Zach Saucier
甚至比我发现的SVG解决方案更令人满意 [请查看我的解决方案](https://dev59.com/-ms05IYBdhLWcg3wPfcR#48586471) - Biskrem Muhammad

40

这很棘手...当然,棘手也很酷 ;)

好的,我有一个解决方案。基本上是通过神奇的:before选择器完成的。

#cool-hover{
 width: 120px;
 height: 120px;
 display: block;
 cursor: pointer;
 border-radius: 10px;
 box-shadow: 0 0 15px rgba(0,0,0,0.3);
 margin: 0px auto 24px auto;
 transition: all 0.5s;
 position: relative;
 overflow: hidden;
}
#cool-hover:before{
 border-radius: inherit;
 display: block;
 width: 200%;
 height: 200%;
 content: '';
        position: absolute;
        top: 0; 
        left: 0;
 background: linear-gradient(135deg, #21d4fd 25%, #b721ff 75%); 
 transition: all 0.5s;
 transform: translate(-25%, -25%);
 z-index: 0;
}
#cool-hover:after{
 border-radius: 9px;
 display: block;
 width: 108px;
 height: 108px;
 margin: 6px;
 background: url('https://i.imgur.com/w0BjFgr.png');
 background-size: cover;
 content: '';
 position: absolute;
 top: 0; left: 0;
 z-index: 1;
}
#cool-hover:hover:before{
 transform: translate(-25%, -25%) rotate(-180deg);
}
#cool-hover:hover{
 box-shadow: 0 0 35px rgba(0,0,0,0.3);
}
<div id="cool-hover"></div>


注意:我只是添加了:after伪类,只是为了小图像占位符目的。

祝你有愉快的样式设置;)


7

我知道这篇文章有点老了,但我还是找不到一个好的解决方案。所以这是我的解决方案:

首先,在":before"上创建渐变,并使用不透明度隐藏它,然后在悬停时过渡为不透明度1。

https://jsfiddle.net/sumon380/osqLpboc/3/

.button {
    text-decoration: none;
    padding: 10px 25px;
    font-size: 20px;
    color: #333;
    display: inline-block;
    background: #d6e9eb;
    position: relative;
    z-index: 1;
    transition: color 0.3s ease-out;

}
.button:before {
    background: #91a5f4;
    background: linear-gradient(135deg, #91a5f4 0%, #b08cf9 86%);
    content: "";
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    position: absolute;
    z-index: -1;
    opacity: 0;
    transition: opacity 0.3s ease-out;
}
.button:hover:before {
    opacity: 1;
}
.button:hover {
    color: #fff;
}
<a class="button" href="#">Button</a>


最佳答案在这里! - allenski
除非我使用::before而不是:before,否则它对我无效。 - Octo Poulos
好的,这可能与特定浏览器有关。为什么不使用.button:before, .button::before呢? - user2462948

6
您可以使用阴影来模拟渐变过渡效果!例如,从我的一个页面中:

c { 
color: #FFF;
background: #000;
border-style:solid;
border-color:#CCC;
border-width: 0 0 0 1px;
box-shadow: 2px 2px 2px #555, inset 0 25px 20px -10px rgba(255, 255, 255, 0.3),
    inset 0 -15px 20px -10px rgba(0, 0, 0, 0.15);
-moz-box-shadow: 2px 2px 2px #555, inset 0 25px 20px -10px rgba(255, 255, 255, 0.3),
    inset 0 -15px 20px -10px rgba(0, 0, 0, 0.15);
-o-box-shadow: 2px 2px 2px #555, inset 0 25px 20px -10px rgba(255, 255, 255, 0.3),
    inset 0 -15px 20px -10px rgba(0, 0, 0, 0.15);
-webkit-box-shadow: 2px 2px 2px #555,
    inset 0 25px 20px -10px rgba(255, 255, 255, 0.3),
    inset 0 -15px 20px -10px rgba(0, 0, 0, 0.15);
-moz-transition: background-color .5s ease; 
-o-transition: background-color .5s ease; 
-webkit-transition: background-color .5s ease-in-out; 
transition: background-color .5s ease;
}

接下来是:

c:hover {
color:#FFF;
background: #505;
position:relative;
top:1px;
box-shadow: -1px -1px -1px #555,inset 0 20px 20px -10px rgba(0, 0, 0, 0.15), 
        inset 0 -15px 20px -10px rgba(255, 255, 255, 0.3);
-moz-box-shadow: -1px -1px -1px #555,inset 0 20px 20px -10px rgba(0, 0, 0, 0.15),
    inset 0 -15px 20px -10px rgba(255, 255, 255, 0.3);
-o-box-shadow: -1px -1px -1px #555, inset 0 20px 20px -10px rgba(0, 0, 0, 0.15), 
        inset 0 -15px 20px -10px rgba(255, 255, 255, 0.3);
-webkit-box-shadow: 1px -1px -1px #555, inset 0 20px 20px -10px rgba(0, 0, 0, 0.15), 
    inset 0 -15px 20px -10px rgba(255, 255, 255, 0.3);
}

在这里,你实质上使用一个内阴影作为类似于Photoshop的掩模,对下面的元素产生渐变效果。当鼠标悬停时,你会反转这个效果。


1
除非它是非常小的转换,否则这个性能相当糟糕。 - Zach Saucier

2
如果您只是想在按钮上悬停时进行轻微的高亮处理,那么有一个更简单的解决方案。您只需将渐变向下移动一点,并使背景颜色与渐变的顶部颜色相同:http://cdpn.io/oaByI
我知道这种方法相当受限制,但对于这种情况而言效果很好。

1

1
我知道这个问题很久了,但我找到了一种适用于某些情况的动画基本渐变的好方法。
这种方法可以让你动画改变渐变的颜色,但不能改变颜色停止位置的变化。

https://jsfiddle.net/62vzydeh/

HTML:
<div class="button">
    Click Me!
</div>

CSS(层叠样式表):
.button {

  width: 200px;
  height: 30px;

  margin: 50px;
  padding-top: 10px;

  color: #C0C0C0;
  background: linear-gradient(to left, #F8F8F8, transparent 30%);
  background-color: #808080;

  text-align: center;
  font-family: sans-serif;

  cursor: pointer;

  transition: background-color 500ms;
}

.button:hover {

  background-color: #A0A0A0;
}

0

九年了,但这一次,希望我的styled-components能够帮助到某些人:

import React, { ReactNode } from 'react'
import { Button as ButtonUI } from "@your-library-ui"
import styled from 'styled-components'

type Props = {
  onClick?: () => void;
  children?: ReactNode;
};

const Button = ({onClick, children}: Props) => (
  <StyledButton onClick={onClick} >
    {children}
    <ButtonHelper />
  </StyledButton>
)

export default Button

const ButtonHelper = styled.span`
  position: absolute;
  z-index: -1;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  border-radius: 5px;
  background: linear-gradient(to right, #5ab0f4, #1273ea)!important;
  transition: opacity 0.2s linear;
`;

const StyledButton = styled(ButtonUI)`
  position: relative;
  z-index: 1;
  background: linear-gradient(to right, #1273ea, #1c94f4)!important;
  color: #fff;
  &:hover ${ButtonHelper} {
    opacity: 1;
  }
`;

开始使用您设计的带有额外效果的新组件吧!感谢 @kizu 的建议。


-3

在程序编写中,源代码和修改后的代码样式必须相同。

例如:

a {
   background: transparent;
   background: linear-gradient(transparent,transparent);
   -moz-transition: all 0.3s ease;
   -o-transition: all 0.3s ease;
   -webkit-transition: all 0.3s ease;
   transition: all 0.3s ease;
}
a:hover
{
   background: #abc07c;
   background: linear-gradient(#c5db95,#88a743);
}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接