使用CSS3編寫類似iOS中的復(fù)選框及帶開關(guān)的按鈕

checkbox多選
最近寫了一個適合移動端的checkbox,如圖:
ps:中間的勾勾是iconfont,iOS風(fēng)格的。
具體的HTML:
- <div class="mui-checkbox-con">
- <label>
- <input class="mui-checkbox" type="checkbox">默認未選中</label>
- </div>
- <div class="mui-checkbox-con">
- <label>
- <input class="mui-checkbox" type="checkbox" checked>默認選中</label>
- </div>
- <div class="mui-checkbox-con">
- <label>
- <input class="mui-checkbox checkbox-orange" type="checkbox" checked>橘黃色 checkbox-orange</label>
- </div>
- <div class="mui-checkbox-con">
- <label>
- <input class="mui-checkbox checkbox-green" type="checkbox" checked>綠色 checkbox-green</label>
- </div>
- <div class="mui-checkbox-con">
- <label>
- <input class="mui-checkbox" type="checkbox" disabled>禁用</label>
- </div>
CSS代碼(SCSS導(dǎo)出的,排版有些奇怪):
- .mui-checkbox {
- -webkit-appearance: none;
- position: relative;
- width: 25px;
- height: 25px;
- margin-right: 10px;
- background-color: #FFFFFF;
- border: solid 1px #d9d9d9;
- border-top-left-radius: 20px;
- border-top-rightright-radius: 20px;
- border-bottom-left-radius: 20px;
- border-bottom-rightright-radius: 20px;
- background-clip: padding-box;
- display: inline-block; }
- .mui-checkbox:focus {
- outline: 0 none;
- outline-offset: -2px; }
- .mui-checkbox:checked {
- background-color: #18b4ed;
- border: solid 1px #FFFFFF; }
- .mui-checkbox:checked:before {
- display: inline-block;
- margin-top: 1px;
- margin-left: 2px;
- font-family: iconfont;
- content: "\e667";
- color: #FFFFFF;
- font-size: 18px; }
- .mui-checkbox:disabled {
- background-color: #d9d9d9;
- border: solid 1px #d9d9d9; }
- .mui-checkbox:disabled:before {
- display: inline-block;
- margin-top: 1px;
- margin-left: 2px;
- font-family: iconfont;
- content: "\e667";
- color: #FFFFFF;
- font-size: 18px; }
- .mui-checkbox.checkbox-green:checked {
- background-color: #5cb85c; }
- .mui-checkbox.checkbox-orange:checked {
- background-color: #f0ad4e; }
- .mui-checkbox.checkbox-s {
- width: 19px;
- height: 19px; }
- .mui-checkbox.checkbox-s:before {
- display: inline-block;
- margin-top: 1px;
- margin-left: 2px;
- font-family: iconfont;
- content: "\e667";
- color: #FFFFFF;
- font-size: 13px; }
- .mui-checkbox-anim {
- -webkit-transition: background-color ease 0.2s;
- transition: background-color ease 0.2s; }
SCSS代碼:
- @mixin checkedCon($fs:18px) {
- &:before {
- display: inline-block;
- margin-top: 1px;
- margin-left: 2px;
- font-family: iconfont;
- content: "\e667";
- color: #FFFFFF;
- font-size: $fs;
- }
- }
- $duration: .4s;
- .mui-checkbox {
- -webkit-appearance: none;
- position: relative;
- width: 25px;
- height: 25px;
- margin-right: 10px;
- background-color: #FFFFFF;
- border: solid 1px #d9d9d9;
- border-top-left-radius: 20px;
- border-top-rightright-radius: 20px;
- border-bottom-left-radius: 20px;
- border-bottom-rightright-radius: 20px;
- background-clip: padding-box;
- display: inline-block;
- &:focus {
- outline: 0 none;
- outline-offset: -2px
- }
- &:checked {
- background-color: #18b4ed;
- border: solid 1px #FFFFFF;
- @include checkedCon();
- }
- &:disabled {
- background-color: #d9d9d9;
- border: solid 1px #d9d9d9;
- @include checkedCon();
- }
- &.checkbox-green:checked {
- background-color: #5cb85c;
- }
- &.checkbox-orange:checked {
- background-color: #f0ad4e;
- }
- &.checkbox-s {
- width: 19px;
- height: 19px;
- @include checkedCon(13px);
- }
- }
- .mui-checkbox-anim{
- //border等其他元素不做過渡效果,增加視覺差,更有動畫效果
- transition: background-color ease $duration/2;
- }
帶switch開關(guān)
本身我做這一個ui的目的是支持移動端的頁面,而webkit上也正好支持單標記的input元素是使用偽類(:before或:after),所以我沒做更多的支持和優(yōu)化,我只是想盡量的保持html干凈,所以沒用其他元素做模擬。如果你要使用在桌面應(yīng)用上,或支持其他瀏覽器,可以自己稍微修改一下,反正我是沒測試過。
今天繼續(xù)分享一個iOS風(fēng)格的switch開關(guān)按鈕,樣子也非常常見,如圖:
主要是使用了<input type="checkbox">來模擬實現(xiàn),具體的HTML:
- <label><input class="mui-switch" type="checkbox"> 默認未選中</label>
- <label><input class="mui-switch" type="checkbox" checked> 默認選中</label>
- <label><input class="mui-switch mui-switch-animbg" type="checkbox"> 默認未選中,簡單的背景過渡效果,加mui-switch-animbg類即可</label>
- <label><input class="mui-switch mui-switch-animbg" type="checkbox" checked> 默認選中</label>
- <label><input class="mui-switch mui-switch-anim" type="checkbox"> 默認未選中,過渡效果,加 mui-switch-anim
- 類即可</label>
- <label><input class="mui-switch mui-switch-anim" type="checkbox" checked> 默認選中</label>
在實際的使用中后來又增加了兩個過渡效果,分別加 mui-switch-animbg和mui-switch-anim 類即可,具體效果查看下面的demo頁面。
CSS代碼(SCSS導(dǎo)出的,排版有些奇怪):
- .mui-switch {
- width: 52px;
- height: 31px;
- position: relative;
- border: 1px solid #dfdfdf;
- background-color: #fdfdfd;
- box-shadow: #dfdfdf 0 0 0 0 inset;
- border-radius: 20px;
- border-top-left-radius: 20px;
- border-top-rightright-radius: 20px;
- border-bottom-left-radius: 20px;
- border-bottom-rightright-radius: 20px;
- background-clip: content-box;
- display: inline-block;
- -webkit-appearance: none;
- user-select: none;
- outline: none; }
- .mui-switch:before {
- content: '';
- width: 29px;
- height: 29px;
- position: absolute;
- top: 0px;
- left: 0;
- border-radius: 20px;
- border-top-left-radius: 20px;
- border-top-rightright-radius: 20px;
- border-bottom-left-radius: 20px;
- border-bottom-rightright-radius: 20px;
- background-color: #fff;
- box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4); }
- .mui-switch:checked {
- border-color: #64bd63;
- box-shadow: #64bd63 0 0 0 16px inset;
- background-color: #64bd63; }
- .mui-switch:checked:before {
- left: 21px; }
- .mui-switch.mui-switch-animbg {
- transition: background-color ease 0.4s; }
- .mui-switch.mui-switch-animbg:before {
- transition: left 0.3s; }
- .mui-switch.mui-switch-animbg:checked {
- box-shadow: #dfdfdf 0 0 0 0 inset;
- background-color: #64bd63;
- transition: border-color 0.4s, background-color ease 0.4s; }
- .mui-switch.mui-switch-animbg:checked:before {
- transition: left 0.3s; }
- .mui-switch.mui-switch-anim {
- transition: border cubic-bezier(0, 0, 0, 1) 0.4s, box-shadow cubic-bezier(0, 0, 0, 1) 0.4s; }
- .mui-switch.mui-switch-anim:before {
- transition: left 0.3s; }
- .mui-switch.mui-switch-anim:checked {
- box-shadow: #64bd63 0 0 0 16px inset;
- background-color: #64bd63;
- transition: border ease 0.4s, box-shadow ease 0.4s, background-color ease 1.2s; }
- .mui-switch.mui-switch-anim:checked:before {
- transition: left 0.3s; }
- /*# sourceMappingURL=mui-switch.css.map */
SCSS代碼:
- @mixin borderRadius($radius:20px) {
- border-radius: $radius;
- border-top-left-radius: $radius;
- border-top-rightright-radius: $radius;
- border-bottom-left-radius: $radius;
- border-bottom-rightright-radius: $radius;
- }
- $duration: .4s;
- $checkedColor: #64bd63;
- .mui-switch {
- width: 52px;
- height: 31px;
- position: relative;
- border: 1px solid #dfdfdf;
- background-color: #fdfdfd;
- box-shadow: #dfdfdf 0 0 0 0 inset;
- @include borderRadius();
- background-clip: content-box;
- display: inline-block;
- -webkit-appearance: none;
- user-select: none;
- outline: none;
- &:before {
- content: '';
- width: 29px;
- height: 29px;
- position: absolute;
- top: 0px;
- left: 0;
- @include borderRadius();
- background-color: #fff;
- box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
- }
- &:checked {
- border-color: $checkedColor;
- box-shadow: $checkedColor 0 0 0 16px inset;
- background-color: $checkedColor;
- &:before {
- left: 21px;
- }
- }
- &.mui-switch-animbg {
- transition: background-color ease $duration;
- &:before {
- transition: left 0.3s;
- }
- &:checked {
- box-shadow: #dfdfdf 0 0 0 0 inset;
- background-color: $checkedColor;
- transition: border-color $duration, background-color ease $duration;
- &:before {
- transition: left 0.3s;
- }
- }
- }
- &.mui-switch-anim {
- transition: border cubic-bezier(0, 0, 0, 1) $duration, box-shadow cubic-bezier(0, 0, 0, 1) $duration;
- &:before {
- transition: left 0.3s;
- }
- &:checked {
- box-shadow: $checkedColor 0 0 0 16px inset;
- background-color: $checkedColor;
- transition: border ease $duration, box-shadow ease $duration, background-color ease $duration*3;
- &:before {
- transition: left 0.3s;
- }
- }
- }
- }
相關(guān)文章
純CSS3代碼實現(xiàn)switch滑動開關(guān)按鈕效果
今天小編給大家?guī)硪粋€小demo,使用純css3代碼實現(xiàn)switch滑動開關(guān)按鈕效果,非常實用,感興趣的朋友可以參考下2016-08-30純css實現(xiàn)一款仿ios7的switches開關(guān)按鈕
這篇文章主要為大家介紹了純css實現(xiàn)的一款仿ios7的switches開關(guān)按鈕,無需引js代碼。在實現(xiàn)中給出了兩種顏色,三種不同大小的demo,需要的朋友可以參考下2014-10-22- 一款非常酷的CSS3 3D開關(guān)按鈕,點擊按鈕可以左右滑動2014-07-01
- 一款純CSS3實現(xiàn)的滑桿開關(guān)切換按鈕動畫,這個按鈕是一個搖桿,桿子推上推下來切換開關(guān)狀態(tài)2014-05-12
- 一款非??岬腃SS3開關(guān)切換按鈕,它不僅具有3D的效果,而且可以發(fā)光2014-04-23
- 這篇文章主要介紹了CSS開關(guān)按鈕樣式,本文給大家?guī)砣N樣式,通過實例代碼給大家介紹的非常下關(guān)系,具有一定的參考借鑒價值,需要的朋友可以參考下2019-06-04