Toggleswitch (1)

반응형



[javascript] on,off 스위치 만들기 (토글버튼: Togglebutton)


더웠던 휴가 다녀오고 바쁜 업무를 처리하다보니 시간만 흘러 어느새 8월말이 다다르고 있네요. 

휴가를 너무 푹 쉬고 왔는지 피곤함만 더 늘어가는 요즘입니다.

피곤함을 조금 더 빨리 떨치기 위해서 소홀했던 블로그도 챙길 겸 ON,OFF 스위치를 만드는 법을 가져왔습니다.


on,off 스위치 즉 토글버튼(Togglebutton)은 상태값을 on,off할때 유용한 버튼이죠. 

만들기 앞서서 먼저 <head> 안에 해당 소스를 넣어주세요.



<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>



다음 <html> 안에 해당 내용을 넣어주세요.



<label class="switch">
  <input type="checkbox">
  <span class="slider round"></span>
</label>
<p>OFF</p><p style="display:none;">ON</p>



<head> 안에 <style></style> 사이로 css를 넣어주세요.



/* The switch - the box around the slider */
.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
  vertical-align:middle;
}

/* Hide default HTML checkbox */
.switch input {display:none;}

/* The slider */
.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}

input:checked + .slider {
  background-color: #2196F3;
}

input:focus + .slider {
  box-shadow: 0 0 1px #2196F3;
}

input:checked + .slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}

/* Rounded sliders */
.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}

p {
	margin:0px;
	display:inline-block;
	font-size:15px;
	font-weight:bold;
}



마지막으로 </body> 안의 <script></script> 사이로 해당 구문을 넣어주면 완성입니다.



var check = $("input[type='checkbox']");
check.click(function(){
	$("p").toggle();
});




참조 :  https://www.w3schools.com/howto/howto_css_switch.asp

반응형