There is switch simulation for IOS, and of course there is also switch simulation for MIUI
Seeing the switch style in the settings options, I decided to try it out on a whim
The final effect is as shown in the picture:
Implementation process
1. Option box checkbox
Of course, the analog switch requires an option box, and the checkbox is used here
2. Understand the process of switching
Click the switch button to turn it on or off. The native checkbox cannot achieve the effect of the icon, so additional elements are needed to represent the switch in the picture
And we need to use the click effect of the checkbox and the effect of whether it is checked after clicking, so the checkbox cannot be hidden, but it can be overwritten
In order to reduce the use of redundant tags, you can use pseudo elements: before and :after. The tag structure is
<span style="color: #0000ff;"> <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 style="color: #800000;">span</span><span style="color: #0000ff;">><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 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 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></span></span></span></span></span>
3. Implementation of switch
Use the :before pseudo-element as the switch background layer, and the :after pseudo-element as the switch item (that is, the small circle)
<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>; }
The initial small circle is on the left. When the switch state is on, move it to the right and update the background color of the on state
<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>; }
The above is the key code, the following is the complete style


<span style="color: #008080;"> 1</span> <span style="color: #800000;"><style> </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;"> </span>
4. Switch test
Finally, you can use JS to detect the status change of the switch
<span style="color: #0000ff;"><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 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></span></span>

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

HTMLisaspecifictypeofcodefocusedonstructuringwebcontent,while"code"broadlyincludeslanguageslikeJavaScriptandPythonforfunctionality.1)HTMLdefineswebpagestructureusingtags.2)"Code"encompassesawiderrangeoflanguagesforlogicandinteract

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment