Use CSS3 to write code similar to check boxes and buttons with switches in iOS

不言
Release: 2018-06-11 15:17:04
Original
1987 people have browsed it

This article mainly introduces the use of CSS3 to write check boxes and buttons with switches similar to those in iOS. Friends in need can refer to

checkbox multiple selection

Recently I wrote a checkbox suitable for mobile terminals, as shown in the picture:
2016411114913458.png (812×522)

ps: The checkbox in the middle is iconfont, iOS style.

Specific HTML:

Copy after login

CSS code (exported by SCSS, the layout is a bit strange):

.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; }
Copy after login

SCSS code:

@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; }
Copy after login

With switch switch
itself. The purpose of making this UI is to support mobile pages, and webkit also supports single-tagged input elements using pseudo-classes (:before or:after), so I didn’t make any changes. Much support and optimization, I just want to keep the html as clean as possible, so I don’t use other elements for simulation. If you want to use it on a desktop application or support other browsers, you can modify it slightly yourself. I haven't tested it anyway.

Today I continue to share an iOS-style switch button, which looks very common, as shown in the picture:
2016411115110047.png (818×350)

## Mainly uses ; To simulate the implementation, specific HTML:

     
Copy after login

In actual use, two transition effects were later added, and the mui-switch-animbg and mui-switch-anim classes were added respectively. Check the specific effects. The demo page below.

CSS code (exported by SCSS, the layout is a bit strange):

.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 */
Copy after login

SCSS code:

@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; } } } }
Copy after login
The above is the entire content of this article, I hope it will be helpful to everyone's study , please pay attention to the PHP Chinese website for more related content!

Related recommendations:

Analysis of scale scaling in transform of css3

About list-style modifying list attributes in CSS Problems with controlling the style of the li tag

About the code that uses ellipses to replace CSS text when it exceeds the div or span

The above is the detailed content of Use CSS3 to write code similar to check boxes and buttons with switches in iOS. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!