时间:2023-07-24 14:57:01 | 来源:网站运营
时间:2023-07-24 14:57:01 来源:网站运营
web前端入门到实战:CSS实现开关按钮三例:我们将使用纯CSS打造一些切换开关并使其拥有类似于checkbox的用户体验。<div class="switch"> <input id="cmn-toggle-1" class="cmn-toggle cmn-toggle-round" type="checkbox"> <label for="cmn-toggle-1"></label></div><div class="switch"> <input id="cmn-toggle-4" class="cmn-toggle cmn-toggle-round-flat" type="checkbox"> <label for="cmn-toggle-4"></label></div><div class="switch"> <input id="cmn-toggle-7" class="cmn-toggle cmn-toggle-yes-no" type="checkbox"> <label for="cmn-toggle-7" data-on="Yes" data-off="No"></label></div>
这里没什么特别的。对于CSS,我们希望真实的checkbox被隐藏在屏幕和视线之外。基本上所有的样式都被加在label上。这样做很方便,因为点击label实际上会勾选/取消勾选checkbox。我们将用下面的CSS来实现切换开关:.cmn-toggle { position: absolute; margin-left: -9999px; visibility: hidden;}.cmn-toggle + label { display: block; position: relative; cursor: pointer; outline: none; user-select: none;}
input.cmn-toggle-round + label { padding: 2px; width: 120px; height: 60px; background-color: #dddddd; border-radius: 60px;}input.cmn-toggle-round + label:before,input.cmn-toggle-round + label:after { display: block; position: absolute; top: 1px; left: 1px; bottom: 1px; content: "";}input.cmn-toggle-round + label:before { right: 1px; background-color: #f1f1f1; border-radius: 60px; transition: background 0.4s;}input.cmn-toggle-round + label:after { width: 58px; background-color: #fff; border-radius: 100%; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3); transition: margin 0.4s;}input.cmn-toggle-round:checked + label:before { background-color: #8ce196;}input.cmn-toggle-round:checked + label:after { margin-left: 60px;}
input.cmn-toggle-round-flat + label { padding: 2px; width: 120px; height: 60px; background-color: #dddddd; border-radius: 60px; transition: background 0.4s;}input.cmn-toggle-round-flat + label:before,input.cmn-toggle-round-flat + label:after { display: block; position: absolute; content: "";}input.cmn-toggle-round-flat + label:before { top: 2px; left: 2px; bottom: 2px; right: 2px; background-color: #fff; border-radius: 60px; transition: background 0.4s;}input.cmn-toggle-round-flat + label:after { top: 4px; left: 4px; bottom: 4px; width: 52px; background-color: #dddddd; border-radius: 52px; transition: margin 0.4s, background 0.4s;}input.cmn-toggle-round-flat:checked + label { background-color: #8ce196;}input.cmn-toggle-round-flat:checked + label:after { margin-left: 60px; background-color: #8ce196;}
input.cmn-toggle-yes-no + label { padding: 2px; width: 120px; height: 60px;}input.cmn-toggle-yes-no + label:before,input.cmn-toggle-yes-no + label:after { display: block; position: absolute; top: 0; left: 0; bottom: 0; right: 0; color: #fff; font-family: "Roboto Slab", serif; font-size: 20px; text-align: center; line-height: 60px;}input.cmn-toggle-yes-no + label:before { background-color: #dddddd; content: attr(data-off); transition: transform 0.5s; backface-visibility: hidden;}input.cmn-toggle-yes-no + label:after { background-color: #8ce196; content: attr(data-on); transition: transform 0.5s; transform: rotateY(180deg); backface-visibility: hidden;}input.cmn-toggle-yes-no:checked + label:before { transform: rotateY(180deg);}input.cmn-toggle-yes-no:checked + label:after { transform: rotateY(0);}
关键词:实现,入门,实战