您好,登錄后才能下訂單哦!
這篇文章主要介紹CSS3如何模擬IOS實(shí)現(xiàn)滑動(dòng)開(kāi)關(guān)效果,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
前言
H5站點(diǎn)需要IOS滑動(dòng)按鈕的效果,想了想似乎CSS3能搞起,就折騰出來(lái)了...挺簡(jiǎn)單的..請(qǐng)看注釋
效果
代碼
XML/HTML Code復(fù)制內(nèi)容到剪貼板
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>CSS3模擬IOS開(kāi)關(guān)</title> <style type="text/css" media="screen"> /* ========================================================================== 設(shè)置根元素字體大小 ========================================================================== */ html { font-size: 100px; } /* ========================================================================== 設(shè)置模擬元素的包裹層,裝飾...毫無(wú)卵用 ========================================================================== */ .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標(biāo)簽?zāi)M按鈕 ========================================================================== */ .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; } /* ========================================================================== 設(shè)置偽類,來(lái)實(shí)現(xiàn)模擬滑塊滑動(dòng),過(guò)渡用了transition來(lái)實(shí)現(xiàn) , translateZ來(lái)強(qiáng)制啟用硬件渲染 ========================================================================== */ .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; } /* ========================================================================== 設(shè)置raw-checkbox,視覺(jué)直觀比較 ========================================================================== */ .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>
以上是“CSS3如何模擬IOS實(shí)現(xiàn)滑動(dòng)開(kāi)關(guān)效果”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。