css实现拟物化checkbox开关效果代码

代码语言:html

所属分类:布局界面

代码描述:css实现拟物化checkbox开关效果代码

代码标签: css 拟物化 checkbox 开关

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">

  <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
  
  <link rel='stylesheet' href='https://fonts.googleapis.com/css2?family=DM+Sans:wght@400;700&amp;display=swap'>
  
<style>
* {
	border: 0;
	box-sizing: border-box;
	margin: 0;
	padding: 0;
}
:root {
	--hue: 223;
	--hue-success: 103;
	--bg: hsl(var(--hue),10%,80%);
	--fg: hsl(var(--hue),10%,10%);
	--primary: hsl(var(--hue),90%,50%);
	--primary-t: hsla(var(--hue),90%,50%,0);
	--trans-dur: 0.3s;
	--trans-timing: cubic-bezier(0.65,0,0.35,1);
	font-size: calc(16px + (32 - 16) * (100vw - 320px) / (2560 - 320));
}
body, 
input {
	font: 1em/1.5 "DM Sans", sans-serif;
}
body {
	background-color: var(--bg);
	color: var(--fg);
	display: flex;
	height: 100vh;
}
form {
	background-color: hsl(var(--hue),10%,90%);
	border-radius: 0.25em;
	box-shadow: 0 0.25em 0.25em hsla(var(--hue),10%,10%,0.4);
	margin: auto;
	width: calc(100% - 3em);
	max-width: 20em;
}
.setting {
	box-shadow:
		0 0.125em 0 hsl(var(--hue),10%,100%) inset,
		0 -0.125em 0 hsl(var(--hue),10%,75%) inset;
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding: 0.75em 1em;
}
.setting:only-child {
	border-radius: 0.25em;
}
.setting:first-child {
	border-radius: 0.25em 0.25em 0 0;
}
.setting:last-child {
	border-radius: 0 0 0.25em 0.25em;
}
.setting__label {
	margin-right: 1em;
}
.setting,
.switch__input {
	-webkit-tap-highlight-color: transparent;
}
.switch,
.switch:before,
.switch:after {
	display: block;
}
.switch {
	background-image: linear-gradient(hsl(var(--hue),10%,60%),hsl(var(--hue),10%,95%));
	box-shadow: 0 0 0.125em 0.125em hsl(var(--hue),10%,90%) inset;
	border-radius: 1em;
	flex-shrink: 0;
	position: relative;
	width: 5em;
	height: 2em;
}
.switch:before,
.switch:after {
	border-radius: 0.75em;
	content: "";
	position: absolute;
	top: 0.25em;
	left: 0.25em;
	width: 4.5em;
	height: 1.5em;
}
.switch:before {
	background-color: hsl(var(--hue),10%,60%);
}
.switch::after {
	box-shadow: 0 0 0.5em hsl(var(--hue),10%,20%) inset;
	z-index: 1;
}
.switch__fill {
	border-radius: 0.75em;
	overflow: hidden;
	position: absolute;
	top: 0.25em;
	right: 0;
	left: 0.25em;
	width: 4.5em;
	height: 1.5em;
	z-index: 1;
}
.switch__input {
	border-radius: 1em;
	box-shadow: 0 0 0 0.125em var(--primary-t);
	cursor: pointer;
	outline: transparent;
	position: relative;
	width: 100%;
	height: 100%;
	transition: box-shadow calc(var(--trans-dur) / 2) var(--trans-timing);
	z-index: 2;
	-webkit-appearance: none;
	appearance: none;
}
.switch__input:focus-visible {
	box-shadow: 0 0 0 0.125em var(--primary);
}
.switch__input:before,
.switch__input:after {
	border-radius: 50%;
	content: "";
	display: block;
	position: absolute;
	transition: transform var(--trans-dur) var(--trans-timing);
}
.switch__input:before {
	background-image: linear-gradient(hsl(0,0%,100%),hsl(var(--hue),10%,60%));
	box-shadow:
		0 0 0.125em 0.0625em hsl(var(--hue),10%,40%),
		0 0.25em 0.25em hsla(var(--hue),10%,10%,0.4);
	top: 0.125em;
	left: 0.125em;
	width: 1.75em;
	height: 1.75em;
}
.switch__input:after {
	background-image: linear-gradient(hsl(var(--hue),10%,90%),hsl(var(--hue),10%,80%));
	top: 0.25em;
	left: 0.25em;
	width: 1.5em;
	height: 1.5em;
}
.switch__text {
	background-color: hsl(var(--hue-success),90%,50%,0.5);
	color: hsl(var(--hue-success),90%,10%);
	display: block;
	font-size: 0.75em;
	font-weight: 700;
	line-height: 2;
	opacity: 0.6;
	padding: 0 0.75.........完整代码请登录后点击上方下载按钮下载查看

网友评论0