css 触发复选框时背景渐变之间的过渡
css transition between background gradient when checkbox is triggered
我一直在尝试通过css制作一个可切换的按钮,我一直想知道是否有一种方法可以在单击复选框时平滑地在这两种状态之间转换,我已经设法做到了不用动画就可以做到,但如果可以将过渡动画化就更好了
toggle-button {
position: relative;
display: inline-block;
width: 120px;
height: 40px;
margin-left: 100px;
}
.toggle-button input{
opacity: 0;
height: 0;
width: 0;
}
.options-wrapper {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: linear-gradient(to right, white 50%, gray 50%);
border-radius:3px;
}
.button-options {
display: grid;
grid-template-columns: 1fr 1fr;
justify-items: center;
align-items: center;
height: 100%;
width: 100%;
background: linear-gradient(to right, black 50%, white 50%);
-webkit-background-clip: text;
-webkit-transition: .4s;
transition: .4s;
cursor: pointer;
}
.toggle-button input:checked + .options-wrapper {
background: linear-gradient(to right, gray 50%, white 50%);
}
.toggle-button input:checked + .options-wrapper > .button-options {
background: linear-gradient(to right, white 50%, black 50%);
-webkit-background-clip: text;
}
@keyframes shiftOpacity {
from { opacity: 0; }
to { opacity: 1; }
}
.button-options > p {
color: transparent
}
<label class="toggle-button">
<input type="checkbox">
<div class="options-wrapper">
<div class="button-options">
<p>Daily</p>
<p>Hourly</p>
</div>
</div>
</label>
这似乎行得通,我创建了一个背景 div,当检查输入时它会来回移动,我还更改了文本改变颜色的方式。
toggle-button {
position: relative;
display: inline-block;
width: 120px;
height: 40px;
margin-left: 100px;
}
.toggle-button input{
opacity: 0;
height: 0;
width: 0;
}
.options-wrapper {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
border-radius:3px;
}
.button-options {
display: grid;
grid-template-columns: 1fr 1fr;
justify-items: center;
align-items: center;
height: 100%;
width: 100%;
color: gray;
cursor: pointer;
position: relative;
}
.button-options .bg {
position: absolute;
height: 100%;
width: 50%;
left: 0;
background: gray;
transition: .5s ease-in-out;
}
.toggle-button input:checked + .options-wrapper .bg {
left: 50%;
}
.toggle-button input + .options-wrapper p:first-of-type {
color: white;
}
.toggle-button input:checked + .options-wrapper p:first-of-type {
color: gray;
}
.toggle-button input:checked + .options-wrapper p:last-of-type {
color: white;
}
.button-options p {
position: relative;
transition: .5s ease-in-out;
}
<label class="toggle-button">
<input type="checkbox">
<div class="options-wrapper">
<div class="button-options bg-left">
<div class="bg"></div>
<p>Daily</p>
<p>Hourly</p>
</div>
</div>
</label>
我一直在尝试通过css制作一个可切换的按钮,我一直想知道是否有一种方法可以在单击复选框时平滑地在这两种状态之间转换,我已经设法做到了不用动画就可以做到,但如果可以将过渡动画化就更好了
toggle-button {
position: relative;
display: inline-block;
width: 120px;
height: 40px;
margin-left: 100px;
}
.toggle-button input{
opacity: 0;
height: 0;
width: 0;
}
.options-wrapper {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: linear-gradient(to right, white 50%, gray 50%);
border-radius:3px;
}
.button-options {
display: grid;
grid-template-columns: 1fr 1fr;
justify-items: center;
align-items: center;
height: 100%;
width: 100%;
background: linear-gradient(to right, black 50%, white 50%);
-webkit-background-clip: text;
-webkit-transition: .4s;
transition: .4s;
cursor: pointer;
}
.toggle-button input:checked + .options-wrapper {
background: linear-gradient(to right, gray 50%, white 50%);
}
.toggle-button input:checked + .options-wrapper > .button-options {
background: linear-gradient(to right, white 50%, black 50%);
-webkit-background-clip: text;
}
@keyframes shiftOpacity {
from { opacity: 0; }
to { opacity: 1; }
}
.button-options > p {
color: transparent
}
<label class="toggle-button">
<input type="checkbox">
<div class="options-wrapper">
<div class="button-options">
<p>Daily</p>
<p>Hourly</p>
</div>
</div>
</label>
这似乎行得通,我创建了一个背景 div,当检查输入时它会来回移动,我还更改了文本改变颜色的方式。
toggle-button {
position: relative;
display: inline-block;
width: 120px;
height: 40px;
margin-left: 100px;
}
.toggle-button input{
opacity: 0;
height: 0;
width: 0;
}
.options-wrapper {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
border-radius:3px;
}
.button-options {
display: grid;
grid-template-columns: 1fr 1fr;
justify-items: center;
align-items: center;
height: 100%;
width: 100%;
color: gray;
cursor: pointer;
position: relative;
}
.button-options .bg {
position: absolute;
height: 100%;
width: 50%;
left: 0;
background: gray;
transition: .5s ease-in-out;
}
.toggle-button input:checked + .options-wrapper .bg {
left: 50%;
}
.toggle-button input + .options-wrapper p:first-of-type {
color: white;
}
.toggle-button input:checked + .options-wrapper p:first-of-type {
color: gray;
}
.toggle-button input:checked + .options-wrapper p:last-of-type {
color: white;
}
.button-options p {
position: relative;
transition: .5s ease-in-out;
}
<label class="toggle-button">
<input type="checkbox">
<div class="options-wrapper">
<div class="button-options bg-left">
<div class="bg"></div>
<p>Daily</p>
<p>Hourly</p>
</div>
</div>
</label>