前言
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: