带有线性渐变的 CSS 过渡

发布于 2024-12-03 15:56:58 字数 469 浏览 0 评论 0原文

我正在尝试向一个按钮添加一个过渡,该按钮的背景是用 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。

这有什么问题吗?有什么想法吗?

I'm trying to add a transition to a button I have that's background is made with css linear-gradient but it's not working.

This is the css for my button.

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

If you're wondering about @green and @greenhover, I'm using .less to make my css.

Anything wrong with this? Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

书间行客 2024-12-10 15:56:59

遗憾的是,您现在确实无法过渡渐变。

因此,唯一可行的解​​决方法是使用具有所需渐变和过渡的额外元素(其不透明度):

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>

Sadly, you really can't transition gradients for now.

So, the only workable workaround is using an extra element with needed gradient and transition it's opacity:

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>

梦一生花开无言 2024-12-10 15:56:59

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

好吧..我找到了解决方案。它基本上是通过令人惊叹的 :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 sudo 类,只是为了顶部图像占位符的小目的。

有一个非常棒的样式;)

it's tricky.. and of course tricky is cool ;)

okay.. i got a solution. and it's basically done via amazing :before selector

#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>


NOTE : i just added the :after sudo class just for the small on top image placeholder purpose..

have a nice awesome styling ;)

香草可樂 2024-12-10 15:56:59

我知道这很老了,但我还找不到任何好的解决方案。所以这是我的解决方案

首先在 ":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>

I know this pretty old but I could not find any good solution yet. So here is my solution

First Make gradient on ":before and hide it with opacity then transition opacity 1 on hover.

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>

烧了回忆取暖 2024-12-10 15:56:59

您可以使用投影来伪造渐变过渡!
例如,在我的一页中:

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 的蒙版,从而在底层元素上产生渐变效果。悬停时,效果会反转。

You can fake gradient transitions using drop shadows!
For instance, from one of my pages:

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

Followed by:

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

Here, you are essentially using an inset shadow as a Photoshop-like mask, causing a gradient effect on the underlying element. On hover, you invert the effect.

巷雨优美回忆 2024-12-10 15:56:59

如果您在悬停按钮时进行轻微突出显示,则有一个更简单的解决方案。您可以将渐变稍微向下微调,并使背景颜色与渐变的顶部颜色相同: http:// /cdpn.io/oaByI

我知道它非常有限,但如果适用于该用例。

If you're doing the slight highlight when hovering the button there is a much simpler solution. You can just nudge the gradient down a bit and have the background-color be the same as the top color of your gradient: http://cdpn.io/oaByI

It's pretty limited I know, but if works well for that use case.

内心荒芜 2024-12-10 15:56:59

我知道这个问题已经很老了,但我找到了一种很好的方法来动画基本渐变,该方法在某些情况下有效。

此方法可让您对渐变颜色的变化进行动画处理,但不能对颜色停止位置的变化进行动画处理。

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

I know this question is pretty old, but I found a good way to animate basic gradients that will work in some cases.

This method will let you animate a change in color of the gradient but not a change in the position of the color stops.

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;
}
瑕疵 2024-12-10 15:56:59

9 年了,但这一次,希望我的 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 的建议。

9 years, but this time, hope my styled-components can help someone:

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

And start using your new designed component with extra effect! Thanks to @kizu for the suggestion.

楠木可依 2024-12-10 15:56:59

您必须在源中具有相同的样式并在目标中更改样式。

喜欢

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

you must have same style in source and changed style in target.

like

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文