IOS용 스위치 시뮬레이션도 있고, 물론 MIUI용 스위치 시뮬레이션도 있습니다
설정 옵션에서 스위치 스타일을 보다가 갑자기 해보고 싶어졌습니다
최종 효과는 아래와 같습니다.
물론 아날로그 스위치에는 옵션박스가 필요하며 여기서는 체크박스 체크박스를 사용합니다
스위치 버튼을 클릭하여 켜거나 끌 수 있습니다. 기본 체크박스는 아이콘의 효과를 얻을 수 없으므로 그림에서 스위치를 표현하려면 추가 요소가 필요합니다
그리고 체크박스의 클릭 효과와 클릭 후 체크 여부의 효과를 활용해야 체크박스를 숨길 수는 없지만 덮어쓸 수는 있습니다
중복 태그 사용을 줄이기 위해 의사 요소(before 및 :after)를 사용할 수 있으며 태그 구조는
<span style="color: #0000ff;"> <</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="switch-wrap"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">span</span><span style="color: #0000ff;">><</span><span style="color: #800000;">span </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="switch-action"</span><span style="color: #0000ff;">></span>开启<span style="color: #0000ff;"></</span><span style="color: #800000;">span</span><span style="color: #0000ff;">></span>WLAN<span style="color: #0000ff;"></</span><span style="color: #800000;">span</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">label </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="switch"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="checkbox"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="switch"</span><span style="color: #ff0000;"> id</span><span style="color: #0000ff;">="switch"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">label</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
:before 의사 요소를 스위치 배경 레이어로 사용하고 :after 의사 요소를 스위치 항목(예: 작은 원)으로 사용합니다.
<span style="color: #800000;"> .switch input:before </span>{<span style="color: #ff0000;"> content</span>:<span style="color: #0000ff;"> ''</span>;<span style="color: #ff0000;"> display</span>:<span style="color: #0000ff;"> inline-block</span>;<span style="color: #ff0000;"> position</span>:<span style="color: #0000ff;"> relative</span>;<span style="color: #ff0000;"> border-radius</span>:<span style="color: #0000ff;"> 20px</span>;<span style="color: #ff0000;"> border</span>:<span style="color: #0000ff;"> 1px solid #ccccc6</span>;<span style="color: #ff0000;"> box-shadow</span>:<span style="color: #0000ff;"> 0 0 1px 1px #ececf3</span>;<span style="color: #ff0000;"> background-color</span>:<span style="color: #0000ff;"> #fff</span>;<span style="color: #ff0000;"> cursor</span>:<span style="color: #0000ff;"> pointer</span>; }
<span style="color: #800000;"> .switch input:after </span>{<span style="color: #ff0000;"> content</span>:<span style="color: #0000ff;"> ''</span>;<span style="color: #ff0000;"> position</span>:<span style="color: #0000ff;"> absolute</span>;<span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 12px</span>;<span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 12px</span>;<span style="color: #ff0000;"> top</span>:<span style="color: #0000ff;"> 2px</span>;<span style="color: #ff0000;"> left</span>:<span style="color: #0000ff;"> 3px</span>;<span style="color: #ff0000;"> border-radius</span>:<span style="color: #0000ff;"> 50%</span>;<span style="color: #ff0000;"> background-color</span>:<span style="color: #0000ff;"> #ccccc6</span>;<span style="color: #ff0000;"> transition</span>:<span style="color: #0000ff;"> .2s left, .2s background-color</span>; }
첫 번째 작은 원이 왼쪽에 있습니다. 스위치 상태가 켜져 있으면 오른쪽으로 이동하여 켜진 상태의 배경색을 업데이트하세요.
<span style="color: #800000;"> .switch input:checked:after </span>{<span style="color: #ff0000;"> left</span>:<span style="color: #0000ff;"> 15px</span>;<span style="color: #ff0000;"> background-color</span>:<span style="color: #0000ff;"> #36a6fa</span>;<span style="color: #ff0000;"> transition</span>:<span style="color: #0000ff;"> .2s left, .2s background-color</span>; }
위가 키코드이고, 다음이 전체 스타일입니다
<span style="color: #008080;"> 1</span> <span style="color: #800000;"><style> </span><span style="color: #008080;"> 2</span> <span style="color: #800000;"> .switch-wrap </span>{ <span style="color: #008080;"> 3</span> <span style="color: #ff0000;"> position</span>:<span style="color: #0000ff;"> relative</span>; <span style="color: #008080;"> 4</span> <span style="color: #ff0000;"> margin</span>:<span style="color: #0000ff;"> 50px auto</span>; <span style="color: #008080;"> 5</span> <span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 120px</span>; <span style="color: #008080;"> 6</span> <span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 40px</span>; <span style="color: #008080;"> 7</span> <span style="color: #ff0000;"> font</span>:<span style="color: #0000ff;"> 14px/1.5 Arial, Sans-Serif</span>; <span style="color: #008080;"> 8</span> } <span style="color: #008080;"> 9</span> <span style="color: #008080;">10</span> <span style="color: #800000;"> .switch, </span><span style="color: #008080;">11</span> <span style="color: #800000;"> .switch input, </span><span style="color: #008080;">12</span> <span style="color: #800000;"> .switch input:before </span>{ <span style="color: #008080;">13</span> <span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 30px</span>; <span style="color: #008080;">14</span> <span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 14px</span>; <span style="color: #008080;">15</span> } <span style="color: #008080;">16</span> <span style="color: #008080;">17</span> <span style="color: #800000;"> .switch input </span>{ <span style="color: #008080;">18</span> <span style="color: #ff0000;"> position</span>:<span style="color: #0000ff;"> absolute</span>; <span style="color: #008080;">19</span> <span style="color: #ff0000;"> right</span>:<span style="color: #0000ff;"> 0</span>; <span style="color: #008080;">20</span> } <span style="color: #008080;">21</span> <span style="color: #008080;">22</span> <span style="color: #800000;"> .switch input:before </span>{ <span style="color: #008080;">23</span> <span style="color: #ff0000;"> content</span>:<span style="color: #0000ff;"> ''</span>; <span style="color: #008080;">24</span> <span style="color: #ff0000;"> display</span>:<span style="color: #0000ff;"> inline-block</span>; <span style="color: #008080;">25</span> <span style="color: #ff0000;"> position</span>:<span style="color: #0000ff;"> relative</span>; <span style="color: #008080;">26</span> <span style="color: #ff0000;"> border-radius</span>:<span style="color: #0000ff;"> 20px</span>; <span style="color: #008080;">27</span> <span style="color: #ff0000;"> border</span>:<span style="color: #0000ff;"> 1px solid #ccccc6</span>; <span style="color: #008080;">28</span> <span style="color: #ff0000;"> box-shadow</span>:<span style="color: #0000ff;"> 0 0 1px 1px #ececf3</span>; <span style="color: #008080;">29</span> <span style="color: #ff0000;"> background-color</span>:<span style="color: #0000ff;"> #fff</span>; <span style="color: #008080;">30</span> <span style="color: #ff0000;"> cursor</span>:<span style="color: #0000ff;"> pointer</span>; <span style="color: #008080;">31</span> } <span style="color: #008080;">32</span> <span style="color: #008080;">33</span> <span style="color: #800000;"> .switch input:after </span>{ <span style="color: #008080;">34</span> <span style="color: #ff0000;"> content</span>:<span style="color: #0000ff;"> ''</span>; <span style="color: #008080;">35</span> <span style="color: #ff0000;"> position</span>:<span style="color: #0000ff;"> absolute</span>; <span style="color: #008080;">36</span> <span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 12px</span>; <span style="color: #008080;">37</span> <span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 12px</span>; <span style="color: #008080;">38</span> <span style="color: #ff0000;"> top</span>:<span style="color: #0000ff;"> 2px</span>; <span style="color: #008080;">39</span> <span style="color: #ff0000;"> left</span>:<span style="color: #0000ff;"> 3px</span>; <span style="color: #008080;">40</span> <span style="color: #ff0000;"> border-radius</span>:<span style="color: #0000ff;"> 50%</span>; <span style="color: #008080;">41</span> <span style="color: #ff0000;"> background-color</span>:<span style="color: #0000ff;"> #ccccc6</span>; <span style="color: #008080;">42</span> <span style="color: #ff0000;"> transition</span>:<span style="color: #0000ff;"> .2s left, .2s background-color</span>; <span style="color: #008080;">43</span> } <span style="color: #008080;">44</span> <span style="color: #008080;">45</span> <span style="color: #800000;"> .switch input:checked:after </span>{ <span style="color: #008080;">46</span> <span style="color: #ff0000;"> left</span>:<span style="color: #0000ff;"> 15px</span>; <span style="color: #008080;">47</span> <span style="color: #ff0000;"> background-color</span>:<span style="color: #0000ff;"> #36a6fa</span>; <span style="color: #008080;">48</span> <span style="color: #ff0000;"> transition</span>:<span style="color: #0000ff;"> .2s left, .2s background-color</span>; <span style="color: #008080;">49</span> } <span style="color: #008080;">50</span> <span style="color: #008080;">51</span> <span style="color: #008080;">52</span> <span style="color: #800000;"> </style></span>
마지막으로 JS를 사용하여 스위치의 상태 변경을 감지할 수 있습니다
<span style="color: #0000ff;"><</span><span style="color: #800000;">script </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="jquery.js"</span><span style="color: #0000ff;">></</span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">script </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="text/javascript"</span><span style="color: #0000ff;">></span><span style="background-color: #f5f5f5; color: #000000;"> $(</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">#switch</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">).change(</span><span style="background-color: #f5f5f5; color: #0000ff;">function</span><span style="background-color: #f5f5f5; color: #000000;">() { $(</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">.switch-action</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">).text(</span><span style="background-color: #f5f5f5; color: #0000ff;">this</span><span style="background-color: #f5f5f5; color: #000000;">.checked </span><span style="background-color: #f5f5f5; color: #000000;">?</span> <span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">关闭</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;"> : </span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">开启</span><span style="background-color: #f5f5f5; color: #000000;">'</span><span style="background-color: #f5f5f5; color: #000000;">); }); </span><span style="color: #0000ff;"></</span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span>