div+css实现逼真摇杆开关点击效果代码

代码语言:html

所属分类:布局界面

代码描述:div+css实现逼真摇杆开关点击效果代码,有三种形态,圆形、横向、竖向,颜色也有三种。模拟现实世界中的按钮按下效果。

代码标签: div css 逼真 摇杆 开关

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

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

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

  
  
<style>
* {
	border: 0;
	box-sizing: border-box;
	margin: 0;
	padding: 0;
}
:root {
	--hue: 223;
	--hue2: 28;
	--hue3: 13;
	--bg: hsl(var(--hue),10%,90%);
	--fg: hsl(var(--hue),10%,10%);
	--primary: hsl(var(--hue),90%,50%);
	--trans-dur: 0.3s;
	--trans-timing: cubic-bezier(0.65,0,0.35,1);
	font-size: calc(20px + (80 - 20) * (100vw - 320px) / (2560 - 320));
}
body,
input {
	font: 1em/1.5 sans-serif;
}
body {
	background-color: var(--bg);
	color: var(--fg);
	display: flex;
	height: 100vh;
	transition:
		background-color var(--trans-dur),
		color var(--trans-dur);
}
form {
	display: flex;
	gap: 1.5em;
	align-items: center;
	margin: auto;
}
.switch,
.switch__input {
	-webkit-tap-highlight-color: transparent;
}
.switch {
	display: block;
	position: relative;
	width: 3em;
	height: 1.5em;
}
.switch__border,
.switch__border:before,
.switch__flap-a,
.switch__flap-b,
.switch__flap-a:before,
.switch__flap-b:before,
.switch__inside {
	display: block;
	position: absolute;
}
.switch__border {
	background-color: hsl(var(--hue),10%,10%);
	background-image:
		linear-gradient(hsla(var(--hue),10%,10%,0),hsl(var(--hue),10%,10%)),
		linear-gradient(105deg,hsl(var(--hue),10%,35%) 44%,hsla(var(--hue),10%,35%,0) 45%);
	border-radius: 0.25em;
	box-shadow:
		0 0 0 0.0625em hsl(var(--hue),10%,10%) inset,
		0.0625em 0.0625em 0.25em hsla(var(--hue),10%,10%,0.3),
		0 0 0 0.0625em hsla(0,0%,100%,0.3);
	top: 0;
	left: 0;
	transition: box-shadow var(--trans-dur) var(--trans-timing);
	width: 100%;
	height: 100%;
}
.switch__flap-a,
.switch__flap-b {
	top: 0.3125em;
	width: 1.1875em;
	height: calc(100% - 0.625em);
	transition:
		background-color var(--trans-dur) var(--trans-timing),	
		transform var(--trans-dur) var(--trans-timing);
}
.switch__flap-a {
	background-color: hsl(var(--hue),90%,50%);
	background-image: linear-gradient(90deg,hsla(var(--hue),90%,45%,0),hsl(var(--hue),90%,45%));
	box-shadow:
		1px 0 0 hsla(0,0%,100%,0.1) inset,
		0 1px 0 hsla(0,0%,100%,0.1) inset,
		0 -1px 0 hsla(0,0%,100%,0.1) inset;
	border-radius: 0.125em 0 0 0.125em;
	right: 50%;
	transform-origin: 100% 50%;
}
.switch__flap-b {
	background-color: hsl(var(--hue),90%,60%);
	background-image: linear-gradient(90deg,hsl(var(--hue),90%,55%),hsla(var(--hue),90%,55%,0));
	border-radius: 0 0.125em 0.125em 0;
	box-shadow:
		-1px 0 0 hsla(0,0%,100%,0.1) inset,
		0 1px 0 hsla(0,0%,100%,0.1) inset,
		0 -1px 0 hsla(0,0%,100%,0.1) inset;
	left: 50%;
	transform: rotateY(40deg);
	transform-origin: 0 50%;
}
.switch__flap-a:before,
.switch__flap-b:before {
	content: "";
	top: 50%;
	left: 50%;
	transform: translate(-50%,-50%);
}
.switch__flap-a:before {
	border-radius: 50%;
	box-shadow: 0 0 0 0.0625em hsl(0,0%,100%) inset;
	transition: box-shadow var(--trans-dur) var(--trans-timing);
	width: 0.375em;
	height: 0.375em;
}
.switch__flap-b:before {
	background-color: hsl(var(--hue),10%,90%);
	border-radius: 0.0625em;
	transition: background-color var(--trans-dur) var(--trans-timing);
	width: 0.375em;
	height: 0.125em;
}
.switch__inside {
	background-image:
		linear-gradient(90deg,hsl(var(--hue),90%,50%),hsl(var(--hue),90%,70%) 20%,hsla(var(--hue),90%,70%,0) 20%),
		linear-gradient(hsl(var(--hue),90%,30%),hsl(var(--hue),90%,50%));
	border-radius: 0.125em;
	box-shadow: 0 0 0 0.0625em hsl(var(--hue),90%,10%) inset;
	top: 0.25em;
	left: 0.25em;
	width: calc(100% - 0.5em);
	height: calc(100% - 0.5em);
}
.switch__input {
	border-radius: 0.25em;
	box-shadow: 0 0 0 0.125em hsla(var(--hue),90%,50%,0);
	cursor: pointer;
	outline: transparent;
	position: relative;
	width: inherit;
	height: inherit;
	transition: box-shadow 0.15s var(--trans-timing);
	-webkit-appearance: none;
	appearance: none;
	z-index: 1;
}
.switch__input:focus-visible {
	box-shadow: 0 0 0 0.125em var(--primary);
}
.switch__input:checked ~ .switch__flap-a {
	background-color: hsl(var(--hue),90%,40%);
	transform: rotateY(40deg);
}
.switch__input:checked ~ .switch__flap-a:before {
	box-shadow: 0 0 0 0.0625em hsl(var(--hue),10%,90%) inset;
}
.switch__input:checked ~ .switch__flap-b {
	background-color: hsl(var(--hue),90%,55%);
	transform: rotateY(0);
}
.switch__input:checked ~ .switch__flap-b:before {
	background-color: hsl(0,0%,100%);
}
.switch--vert {
	width: 2.25em;
	height: 3em;
}
.switch--vert .switch__border {
	border-radius: 0.375em;
}
.switch--vert .switch__flap-a,
.switch--vert .switch__flap-b {
	left: 0.4375em;
	width: calc(100% - 0.875em);
	height: 1.0625em;
}
.switch--vert .switch__flap-a {
	background-color: hsl(var(--hue2),90%,55%);
	background-image: linear-gradient(90deg,hsl(var(--hue2),90%,45%),hsla(var(--hue2),90%,45%,0));
	border-radius: 0 0 0.25em 0.25em;
	box-shadow:
		0 -1px 0 hsla(0,0%,100%,0.1) inset,
		1px 0 0 hsla(0,0%,100%,0.1) inset,
		-1px 0 0 hsla(0,0%,100%,0.1) inset;
	top: 50%;
	transform: rotateX(0);
	transform-origin: 50% 0;
}
.switch--vert .switch__flap-b {
	background-color: hsl(var(--hue2),90%,50%);
	background-image: linear-gradient(135deg,hsla(var(--hue2),90%,60%,0),hsl(var(--hue2),90%,60%));
	border-radius: 0.25em 0.25em 0 0;
	box-shadow:
	.........完整代码请登录后点击上方下载按钮下载查看

网友评论0