CSS3類比IOS滑動開關

來源:互聯網
上載者:User

標籤:head   alt   attribute   net   doc   attr   flexbox   body   one   

前言
H5網站需要IOS滑動按鈕的效果,想了想似乎CSS3能搞起,就折騰出來了...挺簡單的..請看注釋
效果

代碼
<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>CSS3類比IOS開關</title>    <style type="text/css" media="screen">       /* ==========================================================================                        設定根項目字型大小       ========================================================================== */        html {            font-size: 100px;        }        /* ==========================================================================                        設定類比元素的包裹層,裝飾...毫無卵用       ========================================================================== */       .ios-checkbox{            height:4rem;            width:4rem;            position:absolute;            left:50%;            top:50%;            -webkit-transform:translate(-50%, -50%);                    transform:translate(-50%, -50%);            border:.05rem dashed #3DB7A9;            display:-webkit-box;            display:-webkit-flex;            display:-ms-flexbox;            display:flex;            -webkit-box-orient:horizontal;            -webkit-box-direction:normal;            -webkit-flex-direction:column;                -ms-flex-direction:column;                    flex-direction:column;            -webkit-flex-wrap:nowrap;                -ms-flex-wrap:nowrap;                    flex-wrap:nowrap;            -webkit-justify-content:space-around;                -ms-flex-pack:distribute;                    justify-content:space-around;            -webkit-box-align:center;            -webkit-align-items:center;                -ms-flex-align:center;                    align-items:center;       }        /* ==========================================================================                    label標籤類比按鈕                ========================================================================== */        .emulate-ios-button {            display: block;            width: 2rem;            height: 1rem;            background: #ccc;            border-radius: 5rem;            cursor: pointer;            position: relative;            -webkit-transition: all .3s ease;            transition: all .3s ease;        }        /* ==========================================================================                    設定偽類,來實現類比滑塊滑動,過渡用了transition來實現 ,                    translateZ來強制啟用硬體渲染          ========================================================================== */        .emulate-ios-button:after {            content: ‘‘;            display: block;            width: .9rem;            height: .9rem;            border-radius: 100%;            background: #fff;            box-shadow: 0 1px 1px rgba(0, 0, 0, .1);            position: absolute;            left: .05rem;            top: .05rem;            -webkit-transform:translateZ(0);                transform:translateZ(0);            -webkit-transition: all .3s ease;              transition: all .3s ease;        }        .emulate-ios-button:active:after {            width: 1.1rem;        }      /* ==========================================================================                    設定raw-checkbox,視覺直觀比較        ========================================================================== */        .raw-checkbox{            height:2rem;            width:2rem;        }        .raw-checkbox:checked+label {            background: #34bf49;        }        /* 這裡是虛擬元素位移,初始是0.9+0.05 ,所以這裡1.05rem */        .raw-checkbox:checked+label:after {            left: 1.05rem;        }        .raw-checkbox:checked+label:active:after {            left: .5rem;        }        .raw-checkbox:disabled+label {            background: #d5d5d5;            pointer-events: none;        }        .raw-checkbox:disabled+label:after {            background: #bcbdbc;        }    </style></head><body>    <div class="ios-checkbox">        <input type="checkbox" id="ios-checkbox" name="emulate-ios-button" class="raw-checkbox">        <label for="ios-checkbox" class="emulate-ios-button"></label>    </div></body></html>

 

總結

為了更直觀的比較,raw checkbox我就沒有隱藏了..~~~~

CSS3類比IOS滑動開關

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.