Javascript

【Javascript】iOSスタイルのチェックボックスiOs style Checkbox on CSS3

May 7, 2017
iOs-style-Checkbox-on-CSS3

iOs style Checkbox on CSS3はウェブサイト向けのCSS3のiOSスタイルのチェックボックス。

CSS

  <style>
    @import 'https://fonts.googleapis.com/css?family=Roboto:100,300';
    *{
      box-sizing: border-box;
      outline: none;
      cursor: default;
      -webkit-user-select: none;
       -khtml-user-select: none;
         -moz-user-select: none;
          -ms-user-select: none;
              user-select: none;
    }
    body{
      font-family: 'Roboto', Arial, Helvetica, sans-serif;
      font-size: 18px;
      font-weight: 300;
      background: #f4f4f4;
      text-align: center;
    }
    h1{
      margin: 45px 0 15px;
      font-weight: 100;
      font-size: 3em;
      color: #444;
    }
    p{
      font-size: 1.5em;
      margin: 0 0 45px;
      font-weight: 100;
      color: #aaa;
    }
    a{
      border-bottom: 1px solid transparent;
      color: #aaa;
      text-decoration: none;
      cursor: pointer;
    }
    a:hover{
      color: #888;
      border-color: #888;
    }
    footer{
      font-size: .75em;
      width: 100%;
      font-weight: 300;
      color: #aaa;
    }
    .ios-checkbox-list{
      display: inline-block;
      margin: 0 auto;
      text-align: left;
      margin-bottom: 45px;
    }
    .ios-checkbox-wrapper{
      margin-bottom: 15px;
    	white-space: nowrap;
    }
    .ios-checkbox-wrapper label{
      vertical-align: top;
      line-height: 32px;
      color: #444;
    }
    /* Style checkbox */
    .ios-checkbox-item{
      display: inline-block;
      position: relative;
      width: 58px;
      height: 32px;
      margin-right: 15px;
      border: 1px solid #aaa;
      border-radius: 20px;
      background: #aaa;
      cursor: pointer;
      -webkit-transition: all .3s linear 0s;
         -moz-transition: all .3s linear 0s;
          -ms-transition: all .3s linear 0s;
           -o-transition: all .3s linear 0s;
              transition: all .3s linear 0s;
    }
    .ios-checkbox-item:before{
      content: '';
      position: absolute;
      right: 100%;
      z-index: 1;
      width: 30px;
      height: 30px;
      border-radius: 16px;
      background: #fff;
      box-shadow: 1px 1px 2px 1px rgba(0, 0, 0, .3);
      -webkit-transform: translateX(100%);
         -moz-transform: translateX(100%);
          -ms-transform: translateX(100%);
           -o-transform: translateX(100%);
              transform: translateX(100%);
      -webkit-transition: right .3s ease,
                  transform .2s ease .1s,
                  box-shadow .2s linear;
         -moz-transition: right .3s ease,
                  transform .2s ease .1s,
                  box-shadow .2s linear;
          -ms-transition: right .3s ease,
                  transform .2s ease .1s,
                  box-shadow .2s linear;
           -o-transition: right .3s ease,
                  transform .2s ease .1s,
                  box-shadow .2s linear;
              transition: right .3s ease,
                  transform .2s ease .1s,
                  box-shadow .2s linear;
    }
    .ios-checkbox-item:after{
      content: '';
      position: absolute;
      left: 15%;
      top: 0;
      z-index: 0;
      width: 85%;
      height: 100%;
      border-radius: 16px;
      background: #fff;
      -webkit-transition: all .2s linear;
         -moz-transition: all .2s linear;
          -ms-transition: all .2s linear;
           -o-transition: all .2s linear;
              transition: all .2s linear;
    }
    /* Contrast checkbox */
    .ios-checkbox-item.ios-contrast{
      border: 1px solid #D81316;
      background: #D81316;
    }
    .ios-checkbox-item.ios-contrast:after{
      display: none;
    }
    /* Active checkbox */
    .ios-checkbox-item.ios-active{
      border-color: #0ea518;
      background: #0ea518;
      -webkit-transition: all .2s linear .1s;
         -moz-transition: all .2s linear .1s;
          -ms-transition: all .2s linear .1s;
           -o-transition: all .2s linear .1s;
              transition: all .2s linear .1s;
    }
    .ios-checkbox-item.ios-active:before{
      right: 0;
      box-shadow: -1px 0px 2px 1px rgba(0, 0, 0, .3);
      -webkit-transform: translateX(0);
         -moz-transform: translateX(0);
          -ms-transform: translateX(0);
           -o-transform: translateX(0);
              transform: translateX(0);
    }
    .ios-checkbox-item:active:before{
      width: 40px;
      -webkit-transition: all .2s linear;
         -moz-transition: all .2s linear;
          -ms-transition: all .2s linear;
           -o-transition: all .2s linear;
              transition: all .2s linear;
    }
    .ios-checkbox-item.ios-active:after,
    .ios-checkbox-item:active:after{
      left: 60%;
      top: 50%;
      width: 0;
      height: 0;
    }
  </style>

JS

  <script>
    "use strict";
    document.addEventListener("DOMContentLoaded", function(){
      document.onclick = function(event){
        var target = event.target;
        if(target.classList.contains("ios-checkbox-item")){
          target.classList.toggle('ios-active');
        }
      }
    });
  </script>

HTML

  <div class="ios-checkbox-list">
    <div class="ios-checkbox-wrapper">
      <i class="ios-checkbox-item"></i>
    </div>

    <div class="ios-checkbox-wrapper">
      <i class="ios-checkbox-item"></i><label>With label</label>
    </div>

    <div class="ios-checkbox-wrapper">
      <i class="ios-checkbox-item ios-active"></i><label>Active</label>
    </div>

    <div class="ios-checkbox-wrapper">
      <i class="ios-checkbox-item ios-contrast"></i><label>Contrast</label>
    </div>
  </div>

MIT