自定義checkbox樣式

1542160134(1).jpg

css部分:

  .toggle {
            display: inline-block;
            vertical-align: middle;
            position: relative;
            box-sizing: border-box;
            align-self: center;
            width: 52px;
            border-radius: 16px;
            height: 32px;
        }
        .toggle-icon {
            z-index: 0;
            margin: 0;
            padding: 0;
            -webkit-appearance: none;
            -moz-appearance: none;
            appearance: none;
            border: none;
            position: relative;
            -webkit-transition: .3s;
            transition: .3s;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            display: block;
            cursor: pointer;
        }

        .toggle-icon {
            width: 52px;
            border-radius: 16px;
            height: 32px;
            background: #e5e5e5;
        }

        .toggle input[type=checkbox]:checked+.toggle-icon {
            background: #007aff;
        }

        .toggle-icon:after,
        .toggle-icon:before {
            content: '';
        }

        .toggle-icon:before {
            position: absolute;
            left: 2px;
            top: 2px;
            width: 48px;
            height: 28px;
            border-radius: 16px;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            z-index: 1;
            -webkit-transition-duration: .3s;
            transition-duration: .3s;
            -webkit-transform: scale(1);
            transform: scale(1);
        }

        .toggle-icon:after {
            background: #fff;
            position: absolute;
            z-index: 2;
            -webkit-transform: translateX(0);
            transform: translateX(0);
            -webkit-transition-duration: .3s;
            transition-duration: .3s;
        }

        .toggle-icon:after {
            height: 28px;
            width: 28px;
            top: 2px;
            left: 2px;
            -webkit-box-shadow: 0 2px 4px rgba(0, 0, 0, .3);
            box-shadow: 0 2px 4px rgba(0, 0, 0, .3);
            border-radius: 14px;
        }

        .toggle input[type=checkbox]:checked+.toggle-icon:after {
            -webkit-transform: translateX(20px);
            transform: translateX(20px);
        }

        .toggle input[type=checkbox] {
            display: none;
        }

html部分:

<label class="toggle">
       <input type="checkbox" >
       <span class="toggle-icon"></span>
   </label>

代碼都已經(jīng)貼上,有問題歡迎提問。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容