CSS 过渡按钮背景颜色

标签 css css-transitions

尝试在按钮上创建向右滑动的过渡。如果我删除 .container 背景颜色,则过渡有效。但我希望按钮从黑色开始并变为黄色,同时保持容器颜色为默认颜色。但输出似乎不同。如果按钮背景是透明的,它只会过渡到黄色。感谢任何帮助,我为此绞尽脑汁太久了。

.container {
	background: #F0F0F0;
	margin: 20px 20%
}
a.animated-button:link, a.animated-button:visited {
	position: relative;
	display: block;
	margin: 30px auto 0;
	padding: 14px 15px;
	color: #fff;
	font-size: 14px;
	text-align: center;
	text-decoration: none;
	text-transform: uppercase;
	overflow: hidden;
	-webkit-transition: all 1s ease;
	-moz-transition: all 1s ease;
	-o-transition: all 1s ease;
	transition: all 1s ease;
}
a.animated-button:link:after, a.animated-button:visited:after {
	content: "";
	position: absolute;
	height: 0%;
	left: 50%;
	top: 50%;
	width: 150%;
	z-index: -1;
	-webkit-transition: all 0.75s ease 0s;
	-moz-transition: all 0.75s ease 0s;
	-o-transition: all 0.75s ease 0s;
	transition: all 0.75s ease 0s;
}
a.animated-button:link:hover, a.animated-button:visited:hover {
	color: #FFF;
}
a.animated-button:link:hover:after, a.animated-button:visited:hover:after {
	height: 450%;
}
a.animated-button:link, a.animated-button:visited {
	position: relative;
	display: block;
	margin: 30px auto 0;
	padding: 14px 15px;
	color: #fff;
	font-size: 14px;
	border-radius: 0;
	font-weight: bold;
	text-align: center;
	text-decoration: none;
	text-transform: uppercase;
	overflow: hidden;
	letter-spacing: .08em;
	text-shadow: 0 0 1px rgba(0, 0, 0, 0.2), 0 1px 0 rgba(0, 0, 0, 0.2);
	-webkit-transition: all 1s ease;
	-moz-transition: all 1s ease;
	-o-transition: all 1s ease;
	transition: all 1s ease;
}
a.animated-button.thar-three {
	color: #fff;
	cursor: pointer;
	display: block;
	position: relative;
	border: 2px solid #F7CA18;
	transition: all 0.4s cubic-bezier(0.42, 0, 0.58, 1) 0s;
}
a.animated-button.thar-three:hover {
	color: #000 !important;
}
a.animated-button.thar-three:hover:before {
	left: 0%;
	right: auto;
	width: 100%;
}
a.animated-button.thar-three:before {
	display: block;
	position: absolute;
	top: 0px;
	right: 0px;
	height: 100%;
	width: 0px;
	z-index: -1;
	content: '';
	color: #000 !important;
	background: #F7CA18;
	transition: all 0.4s cubic-bezier(0.42, 0, 0.58, 1) 0s;
}
        <div class="container">
          <p><a href="#" class="animated-button thar-three">Register</a></p>
        </div>

最佳答案

我稍微调整了代码,现在即使设置了黑色背景,容器也能正常工作。它现在从黑色变为黄色。此外,我还删除了黄色边框以创建流畅的动画外观。

这是工作片段。

.container {
	background: #F0F0F0;
	margin: 20px 20%
}
a.animated-button:link, a.animated-button:visited {
	position: relative;
	display: block;
	margin: 30px auto 0;
	padding: 14px 15px;
	color: #fff;
	font-size: 14px;
	text-align: center;
	text-decoration: none;
	text-transform: uppercase;
	overflow: hidden;
	-webkit-transition: all 1s ease;
	-moz-transition: all 1s ease;
	-o-transition: all 1s ease;
	transition: all 1s ease;
}
a.animated-button:link:after, a.animated-button:visited:after {
	content: "";
	position: absolute;
	height: 0%;
	left: 50%;
	top: 50%;
	width: 150%;
	z-index: -1;
	-webkit-transition: all 0.75s ease 0s;
	-moz-transition: all 0.75s ease 0s;
	-o-transition: all 0.75s ease 0s;
	transition: all 0.75s ease 0s;
}
a.animated-button:link:hover, a.animated-button:visited:hover {
	color: #FFF;
}
a.animated-button:link:hover:after, a.animated-button:visited:hover:after {
	height: 450%;
}
a.animated-button:link, a.animated-button:visited {
	position: relative;
	display: block;
	margin: 30px auto 0;
	padding: 14px 15px;
	color: #fff;
	font-size: 14px;
	border-radius: 0;
	font-weight: bold;
	text-align: center;
	text-decoration: none;
	text-transform: uppercase;
	overflow: hidden;
	letter-spacing: .08em;
  z-index: 1;
	text-shadow: 0 0 1px rgba(0, 0, 0, 0.2), 0 1px 0 rgba(0, 0, 0, 0.2);
	-webkit-transition: all 1s ease;
	-moz-transition: all 1s ease;
	-o-transition: all 1s ease;
	transition: all 1s ease;
}
a.animated-button.thar-three {
	color: #fff;
	cursor: pointer;
	display: block;
	position: relative;
	
	transition: all 0.4s cubic-bezier(0.42, 0, 0.58, 1) 0s;
  background-color: black;
}
a.animated-button.thar-three:hover {
	color: #000 !important;
}
a.animated-button.thar-three:hover:before {
	left: 0%;
	right: auto;
	width: 100%;
}
a.animated-button.thar-three:before {
	display: block;
	position: absolute;
	top: 0px;
	right: 0px;
	height: 100%;
	width: 0px;
	z-index: -1;
	content: '';
	color: #000 !important;
	background: #F7CA18;
	transition: all 0.4s cubic-bezier(0.42, 0, 0.58, 1) 0s;
}
<div class="container">
          <p><a href="#" class="animated-button thar-three">Register</a></p>
        </div>

关于CSS 过渡按钮背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47323506/

相关文章:

html - 尝试使用 Bootstrap 单击图像区域

css3 slideup 向下滑动

css - 在不同的浏览器窗口中同步动画

javascript - 为什么 Material Icons 不在 phantom js 中呈现?

javascript - 在文档就绪时隐藏除 1 个 dif 之外的所有 dif,并在 "change"之后取消隐藏 div

jquery - CSS3/Javascript : Transitions IE support using jQuery/Modernizr

css - 将在过渡时更改 css 使用

jquery - 过渡动画 : all; Weird delay on margin-right

CSS inline-block li 元素没有水平对齐

javascript - 防止svg边框渐变颜色