我有一个带有线性渐变背景,橙色边框和一些文本的按钮。当我将鼠标悬停在按钮上时,我希望背景变得透明而不改变按钮的其他属性。
我尝试将不透明度转换为0,但是显然,这将隐藏边框和文本。我也尝试过转换背景,但是它不起作用,因为我没有可以转换的端点,因为它需要透明。
body {
background-color: darkblue;
}
.button {
background-image: linear-gradient(red,yellow);
border: solid orange 2px;
border-radius: 10px;
color: white;
padding: 15px 25px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 24px;
}
<button class="button">Submit</button>
答案 0 :(得分:5)
使用伪元素作为背景,您可以轻松地做到这一点:
body {
background-color: darkblue;
}
.button {
border: solid orange 2px;
border-radius: 10px;
color: white;
padding: 15px 25px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 24px;
position: relative;
overflow: hidden;
z-index:0;
background:transparent;
}
.button::before {
content: "";
position: absolute;
z-index:-1;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-image: linear-gradient(red, yellow);
transition:1s;
}
.button:hover::before {
opacity:0;
}
<button class="button">Submit</button>
这是不使用伪元素的另一种想法,您可以在其中依靠更改background-color
和background-size
。诀窍是使一种渐变颜色保持透明,这样我们就可以看到background-color
(您可以将其中一种过渡到透明)。然后,您增加background-size
以隐藏底色,我们只会看到透明色。
body {
background-color: darkblue;
}
.button {
border: solid orange 2px;
background-image: linear-gradient(transparent, yellow);
background-size:100% 100%;
background-color:red;
border-radius: 10px;
color: white;
padding: 15px 25px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 24px;
transition:1s;
}
.button:hover {
background-color:transparent;
background-size:100% 500%;
}
<button class="button">Submit</button>
或者考虑调整background-size
以进行另一种过渡:
body {
background-color: darkblue;
}
.button {
border: solid orange 2px;
background:
linear-gradient(red, yellow),
transparent;
background-size:100% 100%;
background-position:left; /*change this to change the way the transtion happen*/
background-repeat:no-repeat;
border-radius: 10px;
color: white;
padding: 15px 25px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 24px;
transition:1s;
}
.button:hover {
background-size:0% 100%;
}
<button class="button">Submit</button>
答案 1 :(得分:1)
在启用了实验性网络平台功能(see)的Chrome中,我们可以使用:
CSS.registerProperty({
name: '--alpha',
syntax: '<number>',
initialValue: 1,
inherits: true,
})
body {
background-color: darkblue;
}
.button {
--alpha: 1;
background: linear-gradient(rgba(255,0,0,var(--alpha)), rgba(255,255,0,var(--alpha))) transparent;
border: solid orange 2px;
border-radius: 10px;
color: white;
padding: 15px 25px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 24px;
transition: --alpha 1s linear;
}
.button:hover {
--alpha: 0;
}
<button class="button">Submit</button>
答案 2 :(得分:0)
这可能会帮助:
body {
background-color: darkblue;
}
.button {
background-image: linear-gradient(red,yellow);
border: solid orange 2px;
border-radius: 10px;
color: white;
padding: 15px 25px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 24px;
transition: 2s;
z-index: 1000;
}
button:hover{
background:linear-gradient(transparent, transparent);
}
<button class="button">Submit</button>
答案 3 :(得分:0)
我使用javascript和CSS variables做。
const b = document.querySelector(".button");
b.addEventListener("mouseover",function(){
let i = 1;
setInterval(function(){
if(i>0){
i=i - 0.009;
b.style.setProperty("--a", i);
b.style.setProperty("--b", i);
}
},5);
});
b.addEventListener("mouseout",function(){
let i = 0;
setInterval(function(){
if(i<1){
i=i + 0.009;
b.style.setProperty("--a", i);
b.style.setProperty("--b", i);
}
},5);
});
body {
background-color: darkblue;
}
.button {
--a:1;
--b:1;
background-image: linear-gradient(rgba(255,0,0,var(--a)),rgba(255,255,0,var(--b)));
background-color:transparent;
border: solid orange 2px;
border-radius: 10px;
color: white;
padding: 15px 25px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 24px;
}
<button class="button">Submit</button>