一键换肤按钮的创意和实现

2023-12-27 14:52:30

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站

来看一个有意思的一键换肤,黑背景是白按钮,白背景是黑按钮:
在这里插入图片描述
它的源码如下:

<div id="textLight" class="text">Light</div>
<label class="switch">
  <input id="toggle" type="checkbox">
  <span class="slider round"></span>
</label>
<div id="textDark" class="textDark">Dark</div>
* {
	margin: 0;
	font-family: monospace;
	font-size: 20px;
}

body {
	height: 100vh;
	justify-content: center;
	align-items: center;
	display: flex;
	background-color: #0d1117;
	color: #fff;
}

.light {
	background-color: #fff !important;
	color: #000;
}

.dark {
	background-color: #0d1117 !important;
	color: #fff;
}

.switch {
	position: relative;
	display: inline-block;
	width: 60px;
	height: 34px;
}

.switch input {
	opacity: 0;
	width: 0;
	height: 0;
}

.slider {
	position: absolute;
	cursor: pointer;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	background-color: #ccc;
	-webkit-transition: 0.4s;
	transition: 0.4s;
}

.slider:before {
	position: absolute;
	content: "";
	height: 26px;
	width: 26px;
	left: 4px;
	bottom: 4px;
	background-color: white;
	-webkit-transition: 0.4s;
	transition: 0.4s;
}

input:checked + .slider {
	background-color: #0d1117;
}

input:focus + .slider {
	box-shadow: 0 0 1px #2196f3;
}

input:checked + .slider:before {
	-webkit-transform: translateX(26px);
	-ms-transform: translateX(26px);
	transform: translateX(26px);
}

/* Rounded sliders */
.slider.round {
	border-radius: 34px;
}

.slider.round:before {
	border-radius: 50%;
}

.text, .textDark {
	margin: 10px;
	border-radius: 10px;
	padding: 10px;
}

const toggle = document.getElementById("toggle");
const body = document.querySelector("body");
const text = document.getElementById("textLight");
const textDark = document.getElementById("textDark");

toggle.addEventListener("click", function () {
	if (toggle.checked) {
		body.classList.add("light");
		body.classList.remove("dark");
		textDark.classList.add("light");
		textDark.classList.remove("dark");
	} else {
		body.classList.add("dark");
		body.classList.remove("light");
		text.classList.add("dark");
		text.classList.remove("light");
		text.classList.remove("light");
	}
});

文章来源:https://blog.csdn.net/qq_35241329/article/details/135238694
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。