日夜间切换开关
创作时间:
作者:
@小白创作中心
日夜间切换开关
引用
CSDN
1.
https://blog.csdn.net/justliu1989/article/details/143326117
本文将介绍如何使用CSS实现一个日间/夜间模式切换开关按钮。这个功能在现代网页设计中非常常见,能够提升用户的使用体验。
整体效果
实现这个功能主要用到了以下CSS知识点:
transition过渡属性:before和:after伪元素选择器content属性:checked选择器appearance自定义外观属性
核心思路是自定义多选框外观,通过过渡动画切换日夜模式图标,并改变提示文字。这种效果可以增强用户的交互体验。
核心代码
HTML 代码
<label class="label78">
<input type="checkbox" class="checkbox78" />
<div class="status78"></div>
</label>
按钮开关主体代码结构。
CSS 部分代码
.label78{
position: relative;
cursor: pointer;
overflow: hidden;
}
.checkbox78{
width:90px;
height:40px;
background-color:#000000;
border-radius:20px;
margin:0;
cursor: pointer;
box-sizing: border-box;
transition: all 0.4s linear;
appearance: none;
-webkit-appearance: none;
}
.status78{
width:16px;
height:16px;
background-color: transparent;
box-shadow: inset -6px -4px 0 yellow;
border-radius:50%;
position: absolute;
right:17px;
top:12px;
transition: all 0.4s linear;
display: flex;
align-items: center;
justify-content: center;
}
.status78:after{
content:'夜间';
font-size:14px;
font-weight: bold;
color:#ffffff;
letter-spacing:3px;
line-height:1;
position: absolute;
left:-38px;
top:1px;
}
.checkbox78:checked{
background-color:#ebebeb;
}
.checkbox78:checked+.status78{
width:16px;
height:16px;
box-shadow: inset -16px -16px 0 red;
right:17px;
}
.checkbox78:checked+.status78:after{
content:'日间';
color:#000000;
}
- 定义
label标签以及input标签的基本样式,定义input标签的type属性为type="checkbox"。 - 通过
appearance自定义外观属性,定义多选框外观效果,并且给多选框添加过渡属性transition: all 0.4s linear;,设置多选框所有属性可进行过渡动画进行。 - 定义
.status78基本样式,通过box-shadow阴影效果来实现小月亮图标,同样给status78添加过渡属性transition: all 0.4s linear;,设置所有属性可进行过渡动画进行。 - 基于
.status78创建:after伪元素以及基本样式,配合content属性给按钮开关添加文字元素。 - 利用
:checked选择器,当开关按钮被选中时,按钮背景颜色过渡,并且改变.status78月亮样式,通过改变box-shadow阴影属性值以及相关基本样式,让月亮小图标过渡变化成小太阳图标,以及变化.status78:after伪元素中的content属性,变化文字提示内容以及文字颜色。
特别说明: appearance 属性的浏览器兼容性不是很高,注意补充 -webkit-appearance 浏览器兼容效果。
完整代码
HTML 页面
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css">
<title>日夜间切换开关</title>
</head>
<body>
<div class="app">
<label class="label78">
<input type="checkbox" class="checkbox78" />
<div class="status78"></div>
</label>
</div>
</body>
</html>
CSS 样式
.app{
width:100%;
height:100vh;
background-color:#ffffff;
position: relative;
display: flex;
justify-content: center;
align-items: center;
}
.label78{
position: relative;
cursor: pointer;
overflow: hidden;
}
.checkbox78{
width:90px;
height:40px;
background-color:#000000;
border-radius:20px;
margin:0;
cursor: pointer;
box-sizing: border-box;
transition: all 0.4s linear;
appearance: none;
-webkit-appearance: none;
}
.status78{
width:16px;
height:16px;
background-color: transparent;
box-shadow: inset -6px -4px 0 yellow;
border-radius:50%;
position: absolute;
right:17px;
top:12px;
transition: all 0.4s linear;
display: flex;
align-items: center;
justify-content: center;
}
.status78:after{
content:'夜间';
font-size:14px;
font-weight: bold;
color:#ffffff;
letter-spacing:3px;
line-height:1;
position: absolute;
left:-38px;
top:1px;
}
.checkbox78:checked{
background-color:#ebebeb;
}
.checkbox78:checked+.status78{
width:16px;
height:16px;
box-shadow: inset -16px -16px 0 red;
right:17px;
}
.checkbox78:checked+.status78:after{
content:'日间';
color:#000000;
}
以上就是实现日间/夜间模式切换开关的所有代码,希望对你有一些帮助或者启发。
热门推荐
英国NHS指的是什么
阿里原初引力波探测计划:揭秘宇宙诞生之谜
找到自己的价值:从外在评价到内在追求
寿比山治什么
《道德经》:现代生活的智慧灯塔
剥开层层迷雾:掌握链式法则,轻松求解复合函数导数
手机屏幕闪屏怎么办?5个原因及解决方案全解析
人体会产生的消毒液——次氯酸最全科普
每天背诵多少字经典?经典背诵每日字数指南:找到你的最佳节奏!
如何计算实际汇率以优化外汇交易?实际汇率计算对跨境交易有何影响?
长三角低空经济“起飞”,谁在扶摇直上?
城镇老旧小区改造中的适老化需求及对策思考
揭秘受虐心理:症状、成因及疗愈之道
房产档案簿怎么查
遗嘱可以改变继承权吗
北京出发张家界自驾游全攻略:路线、景点与住宿推荐
百草益寿:香蕉蒸着吃,秒变三副药,生吃效果达不到!
人工智能的八大主要应用领域
AI换脸技术的原理是什么?
第二顺位继承人有哪些
清蒸鳕鱼,至简鲜味
客战火力凶猛大巴黎!利物浦并非毫无胜机
刘病已的非典型皇权之路:从市井到龙椅
感受中国国歌的庄严与神圣:一起歌唱祖国!
椰子水是寒性的吗? 如何科学判断食物的寒热属性
连云港的美食有哪些?
大盘指数说明什么?大盘指数的变化如何反映市场行情?
公积金的利息高还是余额宝收益高?多重维度全方位对比
影响光伏板发电的因素及预防措施
孕囊前的疑云:液性暗区的真相与应对