html switch效果,【Microanswer】-【CSS】css实现仿IOS的switch开关效果

重要声明:本文章仅仅代表了作者个人对此观点的理解和表述。读者请查阅时持自己的意见进行讨论。

一、先看效果

二、再看代码

1、html布局代码

2、css样式代码

/* 对开关整体样式设定 */

label.ios-switch {

position: relative;

display: inline-block;

cursor: pointer;

margin: 0;

padding: 0;

}

/* 首先隐藏input输入框 */

label.ios-switch input[type="checkbox"] {

display: none;

}

/* 确定switch开关的盒子模型的大小和边框效果,以及[关]的状态下背景颜色和边框颜色*/

label.ios-switch input[type="checkbox"]+i {

display: block;

width: 108px;

height: 50px;

box-shadow: 0 0 3px black inset;

border: 2px solid gray; /* 边框颜色 */

border-radius: 9999px; /* 将圆角设置为一个较大的值,可以实现两边呈现半圆形的效果。 */

background-color: #ffffff; /* 背景颜色 */

-webkit-transition-duration: 300ms; /* 动画过度时长 */

-moz-transition-duration: 300ms;

-ms-transition-duration: 300ms;

-o-transition-duration: 300ms;

transition-duration: 300ms;

}

/* 确定[开]的状态下背景颜色和边框颜色 */

label.ios-switch input[type="checkbox"]:checked+i {

background-color: rgba(0, 211, 0, 1); /* 背景颜色 */

}

/* 确定[关]状态下的指示圆块的样式,这些样式也会作用于开状态*/

label.ios-switch input[type="checkbox"]+i::before {

content: ' ';

position: absolute;

width: 55px; /* 圆块的宽度 */

height: 46px; /* 圆块的高度 */

background-color: #ededed; /* 关闭时背景色 */

box-shadow: 0 0 4px #000000;

border-radius: 9999px;

left: 2px;

-webkit-transition: inherit;

-moz-transition: inherit;

-ms-transition: inherit;

-o-transition: inherit;

transition: inherit;

}

/* 确定[开]状态下的指示圆块的样式 */

label.ios-switch input[type="checkbox"]:checked+i::before {

left: 50px; /* 开启时划过的距离 */

background-color: rgba(255, 255, 255, 1); /* 背景颜色 */

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值