日夜间切换开关
创作时间:
作者:
@小白创作中心
日夜间切换开关
引用
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;
}
以上就是实现日间/夜间模式切换开关的所有代码,希望对你有一些帮助或者启发。
热门推荐
商业计划书在融资路演中的作用:为投资者呈现最佳商业理念
职场女性必备:从校园到社会的职场技能提升指南
鱼需要多长时间才能长大到可以吃食物?
意外受傷指甲翻起怎麼辦?專家教您正確處理與注意事項
西藏酥油茶品味高原的温暖与文化
温彬:通胀又升了,如何理解美国经济非典型表现
后交叉韧带损伤怎么诊断
厚植文气,文化惠民空间更聚人气——浙江探索公共文化空间一体化运营模式
生态消费:如何实现可持续生活方式的转变
鄱阳湖蓼子花开候鸟来
“司空见惯”里的“司空”是谁?又见惯了什么?
国际中文教育新实践:科技赋能传统文化 打造沉浸式语言文化课堂
燕窝产品配料清单填写指南及关键成分标注
难治性抑郁症救星?碳酸锂治疗全揭秘!
中国古典舞的呼吸作用及方法
北海 vs 湛江:北部湾两座海滨城市的养老生活全方位对比
感冒声音嘶哑的原因及治疗方法
论持续审计和风险监控在信贷额度管理中的落地
那些华夏祖先绚烂夺目的奇迹发明
和谐号动车座位排列方式及购票座位查找指南
明朝官员工资真相:与唐宋比较及其生活困境
金线莲:中药界的明珠
七杀星的人性格特点及长相、命运特点
什么是战略定位理论?
与疾有关的成语有哪些?揭秘!与疾有关的成语,你了解多少?!
解密气体状态方程:从理想世界到现实应用
统计力学如何解释温度的本质?
2025 年引起轰动的 10 大传感器技术(附全名单)
业委会有权利动用维修基金吗
蒲公英根的功效与作用