简体   繁体   English

悬停时从渐变到非渐变背景的过渡

[英]Transition from a gradient to a non-gradient background on hover

I am using a gradient to set the background color on one element. 我使用渐变来设置一个元素的背景颜色。 The thing is, I am also having an " hover " background but not using the gradient. 问题是,我也有一个“ hover ”背景,但没有使用渐变。 At the minute, when I hover on an element having the class .tinted it flashes as it first display no background and then apply the rgba(0,0,0,0.65) Is there any way that the transition could directly go from background: gradient(left, rgba(0,0,0,0.65), rgba(0,0,0,0.30)) to rgba(0,0,0,0.65) ? 在那一刻,当我将鼠标悬停在具有类.tinted的元素上时,它会闪烁,因为它首先显示没有背景然后应用rgba(0,0,0,0.65)是否有任何方式可以直接从背景转换: gradient(left, rgba(0,0,0,0.65), rgba(0,0,0,0.30))rgba(0,0,0,0.65)

.tinted {
    transition: background-color 500ms linear;
    background: -webkit-linear-gradient(left, rgba(0,0,0,.65), rgba(0,0,0,.30));
    background: -o-linear-gradient(right, rgba(0,0,0,.65), rgba(0,0,0,.30));
    background: -moz-linear-gradient(right, rgba(0,0,0,.65), rgba(0,0,0,.30));
    background: linear-gradient(to right, rgba(0,0,0,.65), rgba(0,0,0,.30));
}

.tinted:hover {
    background: rgba(0, 0, 0, 0.65);
}

You need to define the gradients with background-image and the plain color with background-color : 您需要使用background-image定义gradients使用background-image plain color定义background-color

.tinted {
    transition: background-color 500ms linear;
    background-image: -webkit-linear-gradient(left, rgba(0,0,0,.65), rgba(0,0,0,.30));
    background-image: -o-linear-gradient(right, rgba(0,0,0,.65), rgba(0,0,0,.30));
    background-image: -moz-linear-gradient(right, rgba(0,0,0,.65), rgba(0,0,0,.30));
    background-image: linear-gradient(to right, rgba(0,0,0,.65), rgba(0,0,0,.30));
}

.tinted:hover {
    background-color: rgba(0, 0, 0, 0.65);
}

DEMO DEMO

This Is What you Can Use For This Approach: 这是您可以使用的方法:

 #box{ width: 200px; height: 300px; background-color: orange; background-image: -webkit-linear-gradient(top, crimson 0%, transparent 100%); transition: all 1s ease-in-out; } #box:hover{ background-color: crimson; } 
 <div id="box"></div> 

A posibility is to set a gradient that has 2 parts, one with the color changing, and the other with a constant color. 可能性是设置具有2个部分的梯度,一个具有颜色变化,另一个具有恒定颜色。

And change the part of the gradient that you are showing with background-image.position 并使用background-image.position更改要显示的渐变部分

 .test { width: 300px; height: 100px; background-image: linear-gradient(to right, rgba(0,0,0,0.65) 50%, rgba(0,0,0,0.3)); background-size: 200% 100%; background-position: 100% 0%; transition: background-position 1s; margin: 10px; } .test:hover { background-position: 0% 0%; } #test2 { background-image: linear-gradient(to right, blue 50%, red 100%); } 
 <div class="test"></div> <div class="test" id="test2"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM