1

Как сделать такой переключатель на CSS3?

введите сюда описание изображения

2 ответа 2

4

Вот тут есть код.

body {
  color: #151B25;
  background-color: #2C4159;
}

.form {
  position: absolute;
  top: 50%;
  left: 50%;
  height: 100px;
  padding: 10px 0;
  width: 260px;
  transform: translate3d(-50%, -50%, 0);
}

.form:before {
  content: '';
  position: absolute;
  top: 0;
  left: -5px;
  height: 100%;
  width: 120px;
  background-color: #243548;
  border-radius: 50%;
  box-shadow: 75px 0 0 #243548, 150px 0 0 #243548;
}

.form:after {
  content: '';
  position: absolute;
  top: -94px;
  left: 70px;
  height: 100%;
  width: 120px;
  background-color: #2C4159;
  border-radius: 50%;
  box-shadow: 0 188px 0 #2C4159;
}

.form input {
  position: absolute;
  left: -9999px;
}

.bubble {
  position: absolute;
  z-index: 2;
  top: 50%;
  left: 50%;
  height: 100px;
  width: 100px;
  transform: translate3d(-75%, -50%, 0);
  margin-left: -50px;
  background: #BBBBBB;
  border-radius: 50% 50% 50% 50% / 50% 50% 50% 50%;
  border-right: 0px solid #BBBBBB;
  border-left: 0px solid #BBBBBB;
  animation: toggle-reverse 2s 1;
}

#bubble:checked+.bubble {
  animation: toggle 2s 1;
  transform: translate3d(75%, -50%, 0);
  background: #3CCC97;
}

.bubble:after {
  content: '';
  position: absolute;
  top: 50%;
  margin-top: -15px;
  margin-left: -2px;
  height: 30px;
  width: 4px;
  background: #fff;
  left: 50%;
  transform: rotate(45deg);
  border-radius: 2px;
  transition: 500ms ease all 1.25s;
}

.bubble:before {
  content: '';
  position: absolute;
  top: 50%;
  margin-top: -15px;
  margin-left: -2px;
  height: 30px;
  width: 4px;
  background: #fff;
  left: 50%;
  transform: rotate(-45deg);
  border-radius: 2px;
  transition: 500ms ease all 1.25s;
}

#bubble:checked+.bubble:after {
  content: '';
  position: absolute;
  top: 50%;
  margin-top: -15px;
  margin-left: 5px;
  height: 30px;
  width: 4px;
  background: #fff;
  left: 50%;
  transform: rotate(225deg);
  border-radius: 2px;
}

#bubble:checked+.bubble:before {
  content: '';
  position: absolute;
  top: 50%;
  margin-top: -7px;
  margin-left: -10px;
  height: 20px;
  width: 4px;
  background: #fff;
  left: 50%;
  transform: rotate(-215deg);
  border-radius: 2px;
}

@keyframes toggle {
  0% {
    transform: translate3d(-75%, -50%, 0);
    border-right: 0 solid #BBBBBB;
    border-left: 0 solid #BBBBBB;
    background: #BBBBBB;
    border-radius: 50% 50% 50% 50% / 50% 50% 50% 50%;
    height: 100px;
  }
  20% {
    border-right: 0 solid #BBBBBB;
    border-left: 0 solid #BBBBBB;
    border-radius: 50% 50% 50% 50% / 50% 50% 50% 50%;
    transform: translate3d(-75%, -50%, 0);
    height: 100px;
  }
  40% {
    border-left: 0 solid #BBBBBB;
    border-radius: 35% 65% 65% 35% / 50% 50% 50% 50%;
    height: 90px;
  }
  50% {
    transform: translate3d(0%, -50%, 0);
    border-right: 25px solid #BBBBBB;
    border-left: 0 solid #BBBBBB;
    background: #BBBBBB;
    border-radius: 50% 50% 50% 50% / 50% 50% 50% 50%;
    height: 90px;
  }
  75% {
    border-left: 25px solid #3CCC97;
    border-color: #3CCC97;
    background: #3CCC97;
    border-radius: 65% 35% 35% 65% / 50% 50% 50% 50%;
    height: 90px;
  }
  100% {
    border-right: 0 solid #3CCC97;
    border-left: 0 solid #3CCC97;
    border-radius: 50% 50% 50% 50% / 50% 50% 50% 50%;
    transform: translate3d(75%, -50%, 0);
    height: 100px;
    width: 100px
  }
}

@keyframes toggle-reverse {
  0% {
    transform: translate3d(75%, -50%, 0);
    background: #3CCC97;
    border-right: 0 solid #3CCC97;
    border-left: 0 solid #3CCC97;
    border-radius: 50% 50% 50% 50% / 50% 50% 50% 50%;
    height: 100px;
  }
  20% {
    border-right: 0 solid #3CCC97;
    border-left: 0 solid #3CCC97;
    border-radius: 50% 50% 50% 50% / 50% 50% 50% 50%;
    transform: translate3d(75%, -50%, 0);
    height: 100px;
  }
  40% {
    border-right: 0 solid #3CCC97;
    border-radius: 65% 35% 35% 65% / 50% 50% 50% 50%;
    height: 90px;
  }
  50% {
    transform: translate3d(0%, -50%, 0);
    border-left: 25px solid #3CCC97;
    border-right: 0 solid #3CCC97;
    background: #3CCC97;
    border-radius: 50% 50% 50% 50% / 50% 50% 50% 50%;
    height: 90px;
  }
  75% {
    border-right: 25px solid #BBBBBB;
    border-color: #BBBBBB;
    background: #BBBBBB;
    border-radius: 35% 65% 65% 35% / 50% 50% 50% 50%;
    height: 90px;
  }
  100% {
    border-right: 0 solid #BBBBBB;
    border-left: 0 solid #BBBBBB;
    border-radius: 50% 50% 50% 50% / 50% 50% 50% 50%;
    transform: translate3d(-75%, -50%, 0);
    height: 100px;
    width: 100px
  }
}
<div class="form">
  <input type="checkbox" id="bubble" />
  <label class="bubble" for="bubble"></label>
</div>

Анимация тоже есть, осталось прикрепить JS.

3
  • за что заминусовали парня? 27 мар 2020 в 2:45
  • @Август какого? 27 мар 2020 в 12:12
  • пока ты спал(ночью) , у твоего ответа было 2 минуса. вот и стало интересно чем эти минусы заслужены 27 мар 2020 в 12:18
1
body{  
    color: #151B25;
    background-color: #2C4159;
}
.form {
    position: absolute;      top: 50%;      left: 50%;      height: 100px;      
    padding: 10px 0;
    width: 260px;      
    transform: translate3d(-50%,-50%,0);
}
.form:before {
    content: '';
    position: absolute;
    top: 0;
    left: -5px;
    height: 100%;
    width: 120px;
    background-color: #243548;
    border-radius: 50%;
    box-shadow: 75px 0 0 #243548, 150px 0 0 #243548;
}
.form:after {
    content: '';
    position: absolute;
    top: -94px;
    left: 70px;
    height: 100%;
    width: 120px;
    background-color: #2C4159;
    border-radius: 50%;
    box-shadow: 0 188px 0 #2C4159;
}

.form input {
    position: absolute;
    left: -9999px;
}
.bubble{
  position:absolute;
  z-index:2;
  top:50%;
  left:50%;
  height:100px;
  width:100px;
  transform:translate3d(-75%,-50%,0);
  margin-left:-50px;
  background:#BBBBBB;
  border-radius: 50% 50% 50% 50% / 50% 50% 50% 50%;
  border-right: 0px solid #BBBBBB;
  border-left: 0px solid #BBBBBB;
  animation: toggle-reverse 2s 1;  
}
#bubble:checked + .bubble{
  animation: toggle 2s 1;  
  transform:translate3d(75%,-50%,0); 
 background: #3CCC97;   
}
.bubble:after {
    content: '';
    position: absolute;
    top: 50%;
    margin-top: -15px;
    margin-left: -2px;
    height: 30px;
    width: 4px;
    background: #fff;
    left: 50%;
    transform: rotate(45deg);
    border-radius: 2px;
    transition: 500ms ease all 1.25s;
}
.bubble:before {
    content: '';
    position: absolute;
    top: 50%;
    margin-top: -15px;
    margin-left: -2px;
    height: 30px;
    width: 4px;
    background: #fff;
    left: 50%;
    transform: rotate(-45deg);
    border-radius: 2px;
    transition: 500ms ease all 1.25s;
}

#bubble:checked + .bubble:after {
    content: '';
    position: absolute;
    top: 50%;
    margin-top: -15px;
    margin-left: 5px;
    height: 30px;
    width: 4px;
    background: #fff;
    left: 50%;
    transform: rotate(225deg);
    border-radius: 2px;
}
#bubble:checked + .bubble:before {
    content: '';
    position: absolute;
    top: 50%;
    margin-top: -7px;
    margin-left: -10px;
    height: 20px;
    width: 4px;
    background: #fff;
    left: 50%;
    transform: rotate(-215deg);
    border-radius: 2px;
}
@keyframes toggle {
    0%   {
    transform:translate3d(-75%,-50%,0);
    border-right: 0 solid #BBBBBB;
    border-left: 0 solid #BBBBBB;
    background:#BBBBBB;
    border-radius: 50% 50% 50% 50% / 50% 50% 50% 50%;
    height: 100px;
    }
    20% {
    border-right: 0 solid #BBBBBB;
    border-left: 0 solid #BBBBBB;
    border-radius: 50% 50% 50% 50% / 50% 50% 50% 50%;
    transform:translate3d(-75%,-50%,0);
    height: 100px;
    } 
    40%  {
    border-left: 0 solid #BBBBBB;
    border-radius: 35% 65% 65% 35% / 50% 50% 50% 50%;
    height: 90px;
    }
    50%  {
    transform:translate3d(0%,-50%,0);
    border-right: 25px solid #BBBBBB;
    border-left: 0 solid #BBBBBB;
    background:#BBBBBB;
    border-radius: 50% 50% 50% 50% / 50% 50% 50% 50%;
    height: 90px;   
    }
    75%  {
    border-left: 25px solid #3CCC97;
    border-color:#3CCC97;
    background: #3CCC97;
    border-radius: 65% 35% 35% 65% / 50% 50% 50% 50%;
    height: 90px;
    }
    100% {
    border-right: 0 solid #3CCC97;
    border-left: 0 solid #3CCC97;
    border-radius: 50% 50% 50% 50% / 50% 50% 50% 50%;
    transform:translate3d(75%,-50%,0);
    height: 100px;
    width:100px
    }
}


@keyframes toggle-reverse {
    0%   {
    transform:translate3d(75%,-50%,0);
    background: #3CCC97;
    border-right: 0 solid #3CCC97;
    border-left: 0 solid #3CCC97;
    border-radius: 50% 50% 50% 50% / 50% 50% 50% 50%;
    height: 100px;
    }
    20% {
    border-right: 0 solid #3CCC97;
    border-left: 0 solid #3CCC97;
    border-radius: 50% 50% 50% 50% / 50% 50% 50% 50%;
    transform:translate3d(75%,-50%,0);
    height: 100px;
    } 
    40%  {
    border-right: 0 solid #3CCC97;
    border-radius: 65% 35% 35% 65% / 50% 50% 50% 50%;
    height: 90px;
    }
    50%  {
    transform:translate3d(0%,-50%,0);
    border-left: 25px solid #3CCC97;
    border-right: 0 solid #3CCC97;
    background: #3CCC97;
    border-radius: 50% 50% 50% 50% / 50% 50% 50% 50%;
    height: 90px;   
    }
    75%  {
    border-right: 25px solid #BBBBBB;
    border-color:#BBBBBB;
    background:#BBBBBB;
    border-radius: 35% 65% 65% 35% / 50% 50% 50% 50%;
    height: 90px;
    }
    100% {
    border-right: 0 solid #BBBBBB;
    border-left: 0 solid #BBBBBB;
    border-radius: 50% 50% 50% 50% / 50% 50% 50% 50%;
    transform:translate3d(-75%,-50%,0);
    height: 100px;
    width:100px
    }
}






<div class="form">
<input type="checkbox" id="bubble" />
<label class="bubble" for="bubble"></label>
</div>
2
  • 4
    Сообшения содержашие только ссылку ответом не являются. Добавте основное из ссылки прямо в тело ответа.
    – 0xdb
    27 мар 2020 в 0:24

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.