Programming language/HTML & CSS

모바일 토글 스타일 버튼( switch button )

hello-world 2018. 11. 7. 14:16
728x90
반응형

HTML

<div class="switcher">
  <input type="checkbox" id="switcher-1" data-title1="on" data-title2="off" />
  <label for="switcher-1"></label>
</div>

 

CSS

.switcher {position:relative;width:192px;height:42px;border-radius:25px;}
.switcher input {
    -webkit-appearance: none;
    appearance: none;
    position: relative;
    width: 192px;
    height: 42px;
    border-radius: 25px;
    background-color: #0ab1a9;
    border:1px solid #fff;
}
.switcher input:before,
.switcher input:after {
    z-index: 2;
    position: absolute;
    top: 50%;
    color: #fff;
    -webkit-transform: translateY(-50%);
    -moz-transform: translateY(-50%);
    -ms-transform: translateY(-50%);
    -o-transform: translateY(-50%);
    transform: translateY(-50%);
}
/*토글 좌측 텍스트*/
.switcher input:before {
    content: attr(data-title1);
    left: 30px;
    color: #0ab1a9;
    font-size: 16px;
    -webkit-transition:  color 0.5s, left 0.5s;
    -moz-transition:  color 0.5s, left 0.5s;
    -ms-transition:  color 0.5s, left 0.5s;
    -o-transition:  color 0.5s, left 0.5s;
    transition:  color 0.5s, left 0.5s;
}
/*토글 우측 텍스트*/
.switcher input:after {
    content: attr(data-title2);
    right: 30px;
    color: #0ab1a9;
    font-size: 16px;
    -webkit-transition:  color 0.5s, right 0.5s;
    -moz-transition:  color 0.5s, right 0.5s;
    -ms-transition:  color 0.5s, right 0.5s;
    -o-transition:  color 0.5s, right 0.5s;
    transition:  color 0.5s, right 0.5s;
}
/*토클 바 (head)*/
.switcher label {z-index:1;position:absolute;top:1px;bottom:1px;border-radius:25px;background-color: #fff;}
/*토글 좌측 텍스트 활성화시*/
.switcher input:checked:before {
    color: #0ab1a9;
    left:38px;
    font-weight: 500;
    -webkit-transition: color 0.5s 0.2s, left 0.5s 0.2s;
    -moz-transition: color 0.5s 0.2s, left 0.5s 0.2s;
    -ms-transition: color 0.5s 0.2s, left 0.5s 0.2s;
    -o-transition: color 0.5s 0.2s, left 0.5s 0.2s;
    transition: color 0.5s 0.2s, left 0.5s 0.2s;
}
/*토글 우측 텍스트 비활성화시*/
.switcher input:checked:after {color: #fff;}
/*토글바가 좌측으로 이동시*/
.switcher input:checked + label {
    left: 0;
    right: 82px;
    -webkit-transition: left 0.5s, right 0.4s 0.2s;
    -moz-transition: left 0.5s, right 0.4s 0.2s;
    -ms-transition: left 0.5s, right 0.4s 0.2s;
    -o-transition: left 0.5s, right 0.4s 0.2s;
    transition: left 0.5s, right 0.4s 0.2s;
}
/*토글 좌측 텍스트 비활성화시*/
.switcher input:not(:checked):before {color: #fff;}
/*토글 우측 텍스트 활성화시*/
.switcher input:not(:checked):after {
    color: #0ab1a9;
    right:38px;
    font-weight: 500;
    -webkit-transition: color 0.5s 0.2s, right 0.5s 0.2s;
    -moz-transition: color 0.5s 0.2s, right 0.5s 0.2s;
    -ms-transition: color 0.5s 0.2s, right 0.5s 0.2s;
    -o-transition: color 0.5s 0.2s, right 0.5s 0.2s;
    transition: color 0.5s 0.2s, right 0.5s 0.2s;
}
/*토글바가 우측 이동시*/
.switcher input:not(:checked) + label {
    left: 82px;
    right: 0;
    -webkit-transition: left 0.4s 0.2s, right 0.5s;
    -moz-transition:left 0.4s 0.2s, right 0.5s;
    -ms-transition:left 0.4s 0.2s, right 0.5s;
    -o-transition:left 0.4s 0.2s, right 0.5s;
    transition:left 0.4s 0.2s, right 0.5s;
}
728x90
반응형