gpt4 book ai didi

html - div 上线性渐变背景的不同悬停行为

转载 作者:可可西里 更新时间:2023-11-01 12:56:34 24 4
gpt4 key购买 nike

我有两个 div 用于两个不同的按钮。两个 div 之间唯一的变化是一个具有 background: #E82171; 而另一个具有渐变 background: linear-gradient(to right, #e82171 , #ef3e36);

但是,我想了解为什么它们具有不同的悬停行为,即使它们具有相同的样式?

body{
background-color: blue;
}

/** BUTTON 1 **/

.formLink {
text-align: center;
display: inline-block;
box-sizing: border-box;
background: linear-gradient(to right, #e82171 , #ef3e36);
padding: 24px 40px;
border-radius: 100px;
font-size: 18px;
color: #fff;
text-decoration: none;
cursor: pointer;
font-weight: 900;
margin-right: 0;
margin-left: 0;
transition: all 0.6s;
outline:none;
}

.formLink:hover {
box-shadow: 0 0 20px #ffffff;
background: #404262;
}


/** BUTTON 2 **/

.btn {
display: inline-block;
padding: 20px;
border-radius: 100px;
text-align: center;
border: 0;
cursor: pointer;
transition: all 0.6s;
color: #ffffff;
outline: none;
background: #E82171;
font-size: 90%;
}
.btn:hover {
box-shadow: 0 0 20px #ffffff;
background: #404262;
}
<div class="formLink">Button 1</div>

<div class="btn">Button 2</div>

如您所见,将鼠标悬停在按钮 1即时。我基本上希望 button 1 在悬停时有一个缓慢的过渡,就像在 button 2 中一样。

为了测试,我将 background: #E82171; 的线性渐变更改为 button 1,过渡效果完全符合我的要求。不确定为什么线性渐变会影响这个?

编辑:

在发现没有“直接”方法可以做到这一点后,我决定找到一个基于 this solution 的解决方法.

body{
background-color: blue;
}
.formLink {
text-align: center;
display: inline-block;
background: linear-gradient(to right,#e82171,#ef3e36);
padding: 24px 40px;
border-radius: 100px;
-webkit-border-radius: 100px;
-moz-border-radius: 100px;
font-size: 18px;
color: #fff;
text-decoration: none;
cursor: pointer;
font-weight: 900;
transition: all .6s;
background: -moz-linear-gradient(to right,#e82171,#ef3e36);
background: -webkit-linear-gradient(to right,#e82171,#ef3e36);
background: -o-linear-gradient(to right,#e82171,#ef3e36);
background: linear-gradient(to right, #e82171, #ef3e36);
background-repeat: repeat-x;
background-repeat: repeat-y;
background-size: 100%;
background-position: 0 100% no-repeat;
-webkit-transition: all 0.6s linear;
-moz-transition: all 0.6s linear;
-o-transition: all 0.6s linear;
transition: all 0.6s linear;
}

.formLink:hover {
box-shadow: 0 0 20px #fff;
background: #404262;
background-position: 0 0;
}


/*************/

/** BUTTON 2 **/

.btn {
display: inline-block;
padding: 20px;
border-radius: 100px;
text-align: center;
border: 0;
cursor: pointer;
transition: all 0.6s;
color: #ffffff;
outline: none;
background: #E82171;
font-size: 90%;
}
.btn:hover {
box-shadow: 0 0 20px #ffffff;
background: #404262;
}
<div class="formLink">Download</div>
<div class="btn">Button 2</div>

我认为我的下载 按钮与按钮 2 几乎相同?大家可以多多指教。但是,我不确定为什么我的下载 按钮悬停在它上面时会“闪烁”?背景消失一秒钟然后又出现?关于为什么的想法?我需要它的功能与按钮 2 完全一样。

最佳答案

为避免闪烁行为,您需要在其后面使用按钮的副本 - 否则在不透明度动画期间背景将是透明的。您不必使用第二个 div,可以改用伪元素:

https://jsfiddle.net/qLmpxgd8/2/

body{
background-color: blue;
}

.formLink, .formLink:after {
position: relative;
text-align: center;
display: inline-block;
background: linear-gradient(to right,#e82171,#ef3e36);
padding: 24px 40px;
border-radius: 100px;
-webkit-border-radius: 100px;
-moz-border-radius: 100px;
font-size: 18px;
color: #fff;
text-decoration: none;
cursor: pointer;
font-weight: 900;
transition: all .6s;
background: -moz-linear-gradient(to right,#e82171,#ef3e36);
background: -webkit-linear-gradient(to right,#e82171,#ef3e36);
background: -o-linear-gradient(to right,#e82171,#ef3e36);
background: linear-gradient(to bottom, #e82171, #ef3e36);
background-repeat: repeat-x;
background-repeat: repeat-y;
background-size: 100%;
background-position: 0 100% no-repeat;
-webkit-transition: all 0.6s linear;
-moz-transition: all 0.6s linear;
-o-transition: all 0.6s linear;
transition: all 0.6s linear;
}

.formLink:after {
content: "Download";
position: absolute;
left: 0;
top: 0;
}

.formLink:hover:after {
box-shadow: 0 0 20px #fff;
background: #404262;
background-position: 0 0;
}


/*************/

/** BUTTON 2 **/

.btn {
display: inline-block;
padding: 20px;
border-radius: 100px;
text-align: center;
border: 0;
cursor: pointer;
transition: all 0.6s;
color: #ffffff;
outline: none;
background: #E82171;
font-size: 90%;
}
.btn:hover {
box-shadow: 0 0 20px #ffffff;
background: #404262;
}
<div class="formLink">Download</div>
<div class="btn">Button 2</div>

关于html - div 上线性渐变背景的不同悬停行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51157575/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com