search
HomeWeb Front-endCSS TutorialSample code sharing on how to use CSS3+JS to draw various buttons

I think the drawing of buttons is divided into the following three steps

  • The first step is to draw the outline of the button

    • Choose the appropriate one html tag, set the outline CSS

    ##
    /* html代码 */
    <a href="#" class="button off"></a>
    body{
        background-color: #E6C9B6;
    }
    
    /* CSS样式 */
    /* 按钮轮廓 */
    .button{
        display: block;
        margin:100px auto;
        position: relative;
        width:100px;
        height:40px;
        border-radius: 50px;
        border:1px solid #fff;
        background-color: #E9E4E0;
    }
Rendering


Imitate IOS-1.jpg

  • The second step is to draw the default state of the button

    • This step is very important because we will not add any more to the html file Other tags, so we need to use the :after pseudo-class to render the button with CSS

      /* 接在上面继续写 */
      .button:after{
         display: block;
         position: absolute;    //相对按钮的轮廓进行决定定位
         top:2px;
         bottom: 2px;        //即设置top,又设置bottom使元素自动拉伸到最大
         left:2px;           //实际上,按钮的宽度即为容器的高度-(top+bottom)
         width:36px;           //按钮的宽度
         line-height: 36px;    //按钮文字的高度,如果不需要文字,可移除
         text-align: center;
         text-transform: uppercase;
         font-size: 16px;
         background-color: #fff;      //这里的背景颜色是按钮的背景颜色
         border:2px solid;
         transition: all 500ms;        //按钮的动画时长
      }

      In fact, after doing this step, you will find that the effect on the browser has not changed at all, it is still the same as before , but don’t worry, it will be obvious if we add a little something

    • Add a small button within the outline

      .off:after {
         content: &#39;off&#39;;
         border-radius:30px;
         color: #999;
      }

      The default is off


imitate IOS-2.jpg

- 再接着绘制要切换的状态
.on:after {
          content: &#39;ON&#39;;
          border-radius:30px;
          transform: translate(56px, 0);   
          color:transparent;
          background-color:#4BD429;
        }


##imitate IOS-3. jpg

    The last step is to write JS code to switch
  • In fact, among CSS switches, toggleClass is the most convenient.

    but! ! !

    This switching method cannot switch the JS event you want to trigger.
    After all, we draw buttons to complete certain functions,
    So I adopted this method, but it may not be the best

    var zn=0;
    $(&#39;.button&#39;).click(function(e){
      if(zn==1){
          $(this).removeClass("on").addClass("off");
          //此处可填要触发的事件
          zn=0;
      }else{
          $(this).removeClass("off").addClass("on");
           //此处可填要触发的事件
          zn=1;
      }
    });

    At this point, a complete switch button has been drawn

    Thank you for spending 3 to 5 minutes reading my unprofessional tutorial


  • PS: Yesterday I was going to draw a button to control the light bulb switch (actually switching the background image), and then I looked around and saw a bull switch on the wall. Since it controls the light, I wanted to play with the simulated switch. I endured the drowsiness at noon and reluctantly drew it
  绘制过程并不复杂,我也就不细说了,贴下效果图和代码,感兴趣的可以自行看一下

##Simulation switch.jpg

Simulation-2.jpg

PS:我引用了一个初始化的CSS文件,可能需要
box-sizing:border-box;

<style type="text/css">
    /*开关的轮廓*/
    .button{
        display: block;
        position: relative;
        width:160px;
        height:180px;
        border-radius: 5px;
        background-color: #f1f1f1;
    }
    .button2{
        display: block;
        position: relative;
        width:160px;
        height:180px;
        border-radius: 5px;
        background-color: #f1f1f1;
    }
    /*指示灯*/
    .indicate{
        display: block;
        position: absolute;
        margin:60px 0 0 70px;
        width: 20px;
        height: 4px;
        border-radius: 2px;
        background-color: #A8C1C2;
        z-index: 1;
        transition: all 200ms;
    }
    .indicate_yes{
        margin:69px 0 0 70px;
        background-color: #A3D7E7;
    }
    /*开关内部的小按钮*/
    .button:after{
        display: block;
        position: absolute;
        top:40px;
        bottom: 40px;
        left:20px;
        right:20px;
        line-height: 52px;
        border:1px solid #FFF;
        transition: all 200ms;
    }
    .button2:after{
        display: block;
        position: absolute;
        top:49px;
        bottom: 31px;
        left:20px;
        right:20px;
        line-height: 52px;
        border:1px solid #FFF;
        transition: all 200ms;
    }
    /*默认状态的小按钮*/
    .on:after {
      content: &#39;&#39;;
      border-radius: 5px;
      /* CSS3的颜色渐变凸显按钮的凸出感 */
      background: linear-gradient(#fff, #f2f2f2 80%,#fff);
      /* CSS3的影音的综合应用,绘制按钮的边缘,给予立体感 */
      box-shadow: 0 1px 0 0 #fff,
          0 3px 0.5px 0 #E7E9EA,
          0 5px 0.5px 0 #DEDFDF,
          0 6px 0.5px 0 #CCCCCD,
          0 7px 0.5px 0 #C5C7C7,
          0 8px 2px 0 #818283,
          0 9px 2px 0 #A7A8A8;
    }
    /*关闭后的小按钮*/
    .off:after {
      content: &#39;&#39;;
      border-radius: 5px;
      background: linear-gradient(#fff, #f2f2f2 80%,#fff);
      box-shadow: 0 -1px 0 0 #fff,
          0 -3px 0.5px 0 #E7E9EA,
          0 -5px 0.5px 0 #DEDFDF,
          0 -6px 0.5px 0 #CCCCCD,
          0 -7px 0.5px 0 #C5C7C7,
          0 -8px 2px 0 #818283,
          0 -9px 2px 0 #A7A8A8;
    }
    </style> 

/* JS代码 */
<script type="text/javascript">
$(&#39;.button&#39;).click(function(e) {
  var toggle = this;
  e.preventDefault();
  $(toggle).toggleClass(&#39;on&#39;)
         .toggleClass(&#39;off&#39;)
         .toggleClass("button2");
  //指示灯亮/灭
  $(this).children(".indicate")
    .toggleClass("indicate_yes");
});

//localStorage.clear();
</script>

The above is the detailed content of Sample code sharing on how to use CSS3+JS to draw various buttons. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Simulating Mouse MovementSimulating Mouse MovementApr 22, 2025 am 11:45 AM

If you've ever had to display an interactive animation during a live talk or a class, then you may know that it's not always easy to interact with your slides

Powering Search With Astro Actions and Fuse.jsPowering Search With Astro Actions and Fuse.jsApr 22, 2025 am 11:41 AM

With Astro, we can generate most of our site during our build, but have a small bit of server-side code that can handle search functionality using something like Fuse.js. In this demo, we’ll use Fuse to search through a set of personal “bookmarks” th

Undefined: The Third Boolean ValueUndefined: The Third Boolean ValueApr 22, 2025 am 11:38 AM

I wanted to implement a notification message in one of my projects, similar to what you’d see in Google Docs while a document is saving. In other words, a

In Defense of the Ternary StatementIn Defense of the Ternary StatementApr 22, 2025 am 11:25 AM

Some months ago I was on Hacker News (as one does) and I ran across a (now deleted) article about not using if statements. If you’re new to this idea (like I

Using the Web Speech API for Multilingual TranslationsUsing the Web Speech API for Multilingual TranslationsApr 22, 2025 am 11:23 AM

Since the early days of science fiction, we have fantasized about machines that talk to us. Today it is commonplace. Even so, the technology for making

Jetpack Gutenberg BlocksJetpack Gutenberg BlocksApr 22, 2025 am 11:20 AM

I remember when Gutenberg was released into core, because I was at WordCamp US that day. A number of months have gone by now, so I imagine more and more of us

Creating a Reusable Pagination Component in VueCreating a Reusable Pagination Component in VueApr 22, 2025 am 11:17 AM

The idea behind most of web applications is to fetch data from the database and present it to the user in the best possible way. When we deal with data there

Using 'box shadows' and clip-path togetherUsing 'box shadows' and clip-path togetherApr 22, 2025 am 11:13 AM

Let's do a little step-by-step of a situation where you can't quite do what seems to make sense, but you can still get it done with CSS trickery. In this

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

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.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version