【问题标题】:CSS: Button border gradient invert when transition on hoverCSS:悬停时过渡时按钮边框渐变反转
【发布时间】:2019-04-25 08:42:20
【问题描述】:

我正在设置此按钮转换,但我发现我用于网络中其他按钮的效果不适用于此按钮。 边框颜色在过渡时反转。

我必须使用渐变背景,所以不能改变它。

#button {
    background: linear-gradient(#FFF,#FFF) padding-box, linear-gradient(to right, blue 0%, green 100%) border-box;
    border: 3px solid transparent;
    color: #000;
    background-color: white;
    border-radius: 25px;
    margin-top: 60px;
    margin-bottom: 0px;
    font-weight: 600;
    font-size: 14px;
    padding: 7px 22px;
    transition: all .5s ease;
    margin-left: auto;
    margin-right: auto;
    display: block;
    }
    
#button:hover{
background: linear-gradient(to right, blue 0%, green 100%);
    border: 3px solid transparent;
    color: #FFF;
    transition: all .5s ease;
    transition-property: all;
    transition-duration: 0.5s;
    transition-timing-function: ease;
    transition-delay: 0s;
    }
<button id="button"> BUTTON </button>

【问题讨论】:

  • 能否请您提供您的 html 代码 - 您可以简单地创建一个 sn-p,以便您可以看到您之前的进度:)

标签: css border transition gradient


【解决方案1】:

你的悬停/焦点渐变应该是border-box:

background: linear-gradient(to right, blue 0%, green 100%) border-box;

.myb {
    display: inline-block;
    cursor: pointer;
    background: linear-gradient(#FFF, #FFF) padding-box, linear-gradient(to right, blue 0%, green 100%) border-box;
    border: 3px solid transparent;
    color: #000;
    background-color: white;
    border-radius: 25px;
    margin-top: 60px;
    margin-bottom: 0px;
    font-weight: 600;
    font-size: 14px;
    padding: 7px 22px;
    transition: all .5s ease;
    margin-left: auto;
    margin-right: auto;
}

.myb:hover,
.myb:focus {
    background: linear-gradient(to right, blue 0%, green 100%) border-box;
    border: 3px solid transparent;
    color: #FFF;
    transition: all .5s ease;
    transition-property: all;
    transition-duration: 0.5s;
    transition-timing-function: ease;
    transition-delay: 0s;
}
<div class="myb">
    Button
</div>

同样在JSFiddle

【讨论】:

    【解决方案2】:

    这是一个过渡的想法,您可以在其中使用背景大小:

    #button {
        background: 
          linear-gradient(#FFF,#FFF) padding-box center, 
          linear-gradient(to right, blue 0%, green 100%) border-box;
        background-size:100% 100%,auto;
        background-repeat:no-repeat;
        border: 3px solid transparent;
        color: #000;
        border-radius: 25px;
        margin: 60px auto 0;
        font-weight: 600;
        font-size: 14px;
        padding: 7px 22px;
        transition: all .5s ease;
        display: block;
        }
        
    #button:hover{
        background-size:0 0,auto;
        color: #FFF;
    }
    &lt;button id="button"&gt; BUTTON &lt;/button&gt;

    你可以通过调整大小和位置来修改:

    #button {
        background: 
          linear-gradient(#FFF,#FFF) padding-box left, 
          linear-gradient(to right, blue 0%, green 100%) border-box;
        background-size:100% 100%,auto;
        background-repeat:no-repeat;
        border: 3px solid transparent;
        color: #000;
        border-radius: 25px;
        margin: 60px auto 0;
        font-weight: 600;
        font-size: 14px;
        padding: 7px 22px;
        transition: all .5s ease;
        display: block;
        }
        
    #button:hover{
        background-size:0 100%,auto;
        color: #FFF;
    }
    &lt;button id="button"&gt; BUTTON &lt;/button&gt;

    【讨论】:

    • 这些动画真的很酷!谢谢!但是我有设计师的这个设计,他不太喜欢这些变化......哈哈,但我喜欢它;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 2014-12-10
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    • 2016-01-25
    相关资源
    最近更新 更多