CSS: инвертировать градиент кнопки при переходе при наведении - PullRequest
2 голосов
/ 25 апреля 2019

Я устанавливаю этот переход кнопок, и я обнаружил, что эффект, который я использую для других кнопок в моей сети, плохо работает с этой.При переходе цвета границы инвертируются.

Я должен использовать градиентный фон, поэтому изменить его нельзя.

#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>

Ответы [ 2 ]

3 голосов
/ 25 апреля 2019

Ваш градиент при наведении / фокусировке должен быть 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 .

1 голос
/ 25 апреля 2019

Вот идея перехода, где вы можете играть с background-size:

#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;
}
<button id="button"> BUTTON </button>

Это можно изменить, отрегулировав размер и положение:

#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;
}
<button id="button"> BUTTON </button>
...