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

时间:2018-07-03 14:54:18

标签: html css

我有两个用于两个不同按钮的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>

如您所见,悬停在button 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 几乎相同?你们可以给我更好的建议。但是,我不确定为什么将我的 download 按钮悬停在其上时会“闪烁”?背景消失一秒钟然后重新出现?关于为什么的想法?我需要它的功能与按钮2 完全一样。

1 个答案:

答案 0 :(得分:3)

为避免闪烁行为,您需要使用其后面的按钮的副本-否则在不透明度动画期间背景将是透明的。您不必使用第二个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>