CSS淡入淡出过渡背景:线性渐变,不起作用
一个简单的 html 元素,带有 CSS 背景线性渐变。
目标:悬停时淡化背景。不起作用(带有过渡背景),请参见下面的示例 - 为什么?
ui-accordion-header {
border: 0 none;
font-size: 100%;
line-height: 1.3;
list-style: none outside none;
margin: 0;
outline: 0 none;
padding: 0;
text-decoration: none;
}
.mx-accordion h1 {
background: linear-gradient(0deg, rgba(227,227,227,1) 20%, rgba(247,247,247,1) 100%);
border: 1px solid #e3e3e3;
border-width: 0 0 1px;
color: #000;
padding: 7px 10px;
margin: 0px;
transition: background .2s ease-in-out;
}
.mx-accordion h1:not(.ui-state-active):hover {
background: linear-gradient(0deg, #e1ebff 20%, #e1ebff 100%);
cursor: pointer;
color: #027BFF;
}
<div id="accordion" class="mx-accordion ui-accordion ui-widget ui-helper-reset" role="tablist">
<h1 class="ui-accordion-header ui-corner-top ui-accordion-header-collapsed ui-corner-all ui-state-default ui-accordion-icons" role="tab" id="ui-id-4" aria-controls="my-filters" aria-selected="false" aria-expanded="false" tabindex="-1"><span class="ui-accordion-header-icon ui-icon ui-icon-triangle-1-e"></span>
<a href="#">My Text</a>
</h1>
</div>
A simple html element with CSS background linear-gradient.
Goal: Fade background on hover. Does not work (with transition background), see example below - Why?
ui-accordion-header {
border: 0 none;
font-size: 100%;
line-height: 1.3;
list-style: none outside none;
margin: 0;
outline: 0 none;
padding: 0;
text-decoration: none;
}
.mx-accordion h1 {
background: linear-gradient(0deg, rgba(227,227,227,1) 20%, rgba(247,247,247,1) 100%);
border: 1px solid #e3e3e3;
border-width: 0 0 1px;
color: #000;
padding: 7px 10px;
margin: 0px;
transition: background .2s ease-in-out;
}
.mx-accordion h1:not(.ui-state-active):hover {
background: linear-gradient(0deg, #e1ebff 20%, #e1ebff 100%);
cursor: pointer;
color: #027BFF;
}
<div id="accordion" class="mx-accordion ui-accordion ui-widget ui-helper-reset" role="tablist">
<h1 class="ui-accordion-header ui-corner-top ui-accordion-header-collapsed ui-corner-all ui-state-default ui-accordion-icons" role="tab" id="ui-id-4" aria-controls="my-filters" aria-selected="false" aria-expanded="false" tabindex="-1"><span class="ui-accordion-header-icon ui-icon ui-icon-triangle-1-e"></span>
<a href="#">My Text</a>
</h1>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不支持过渡渐变,如此处所述。
解决方法可能是
过渡
背景位置
并缩放背景。这样我们就可以实现非常相似的目标。Transitioning gradients is not supported, as explained here.
A workaround could be, to
transition
thebackground-position
and scaling the background. This way we can achieve something very similar.虽然渐变图像的过渡方式与背景颜色不同,但您可以过渡不透明度,当您提到淡入淡出时,这似乎就是您想要的。
当然,您不想改变整个元素的不透明度,因此此代码片段在元素之前和之后以伪方式添加两个背景,并转换这些元素的不透明度。
While gradient images do not transition in the same way as say background-color, you can transition opacity and that would seem to be what you want as you mention fade.
Of course, you dont want to alter the opacity of the whole element, so this snippet adds the two backgrounds in pseudo before and after elements and transitions the opacities of those.