css+js实现checkbox胶粘开关切换效果代码

代码语言:html

所属分类:布局界面

代码描述:css+js实现checkbox胶粘开关切换效果代码

代码标签: css js heckbox 胶粘 开关 切换

下面为部分代码预览,完整代码请点击下载或在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">
  
  
  
<style>
* {
	border: 0;
	box-sizing: border-box;
	margin: 0;
	padding: 0;
}
:root {
	--hue: 223;
	--bg: hsl(var(--hue),90%,90%);
	--fg: hsl(var(--hue),90%,10%);
	--primary: hsl(var(--hue),90%,50%);
	--trans-dur: 0.3s;
	--trans-timing: cubic-bezier(0.65,0,0.35,1);
	font-size: calc(40px + (80 - 40) * (100vw - 320px) / (2560 - 320));
}
body,
input {
	font: 1em/1.5 sans-serif;
}
body {
	background-color: var(--bg);
	color: var(--fg);
	display: grid;
	place-items: center;
	height: 100vh;
	transition:
		background-color var(--trans-dur),
		color var(--trans-dur);
}
.switch,
.switch__input {
	-webkit-tap-highlight-color: transparent;
}
.switch {
	width: 3em;
	height: 1.5em;
}
.switch__input {
	cursor: pointer;
	filter: url(#goo);
	outline: transparent;
	position: relative;
	width: inherit;
	height: inherit;
	-webkit-appearance: none;
	appearance: none;
}
.switch__input:before,
.switch__input:after {
	background-color: hsl(var(--hue),10%,70%);
	content: "";
	display: block;
	position: absolute;
	transition:
		background-color var(--trans-dur) var(--trans-timing),
		transform var(--trans-dur) var(--trans-timing);
}
.switch__input:before {
	border-radius: 0.125em;
	top: calc(50% - 0.125em);
	width: 100%;
	height: 0.25em;
}
.switch__input:after {
	animation: switch-off var(--trans-dur);
	border-radius: 50%;
	top: 0;
	left: 0;
	width: 1.5em;
	height: 1.5em;
	transform-origin: 0 50%;
}
.switch__input:active:after {
	transform: translateX(20%) scale(1.2,0.8);
}
.switch__input:focus-visible:before,
.switch__input:focus-visible:after {
	background-color: hsl(var(--hue),10%,60%);
}

/* `:checked` state */
.switch__input:checked:before,
.switch__input:checked:after {
	background-color: var(--primary);
}
.switch__input:checked:after {
	animation: switch-on var(--trans-dur);
	transform: translateX(100%);
	transform-origin: 100% 50%;
}
.switch__input:checked:active:after {
	transform: translateX(80%) scale(1.2,0.8);
}
.switch__input:checked:focus-visible:before,
.switch__input:checked:focus-visible:after {
	background-color: hsl(var(--hue),90%,40%);
}

/* Other */
.switch__sr {
	overflow: hidden;
	position: absolute;
	width: 1px;
	height: 1px;
}
.switch.........完整代码请登录后点击上方下载按钮下载查看

网友评论0