css+div实现亮色暗色按钮开关效果代码
代码语言:html
所属分类:布局界面
代码描述:css+div实现亮色暗色按钮开关效果代码
下面为部分代码预览,完整代码请点击下载或在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; --fg: hsl(var(--hue),10%,10%); --trans-dur: 0.3s; --trans-timing: cubic-bezier(0.83, 0, 0.17, 1); font-size: calc(30px + (60 - 30) * (100vw - 320px) / (2560 - 320)); } body, input { color: var(--fg); font: 1em/1.5 sans-serif; } body { display: flex; height: 100vh; } main, label, .col { display: flex; } main { flex-direction: column; width: 100%; } label { align-items: center; margin: auto; -webkit-tap-highlight-color: transparent; } .col { background-color: hsl(var(--hue),10%,90%); background-image: linear-gradient(hsla(0,0%,0%,0),hsla(0,0%,0%,0.4)); padding: 1.5em; width: 100%; height: 100%; } .sr { overflow: hidden; position: absolute; width: 1px; height: 1px; } /* Light theme */ .switch, .switch__input { display: block; } .switch { border-radius: 1.5em; box-shadow: 0 0.125em 0.25em hsla(0,0%,0%,0.4); position: relative; width: 4.5em; height: 3em; perspective: 10em; } .switch span { display: block; } .switch > span { border-radius: inherit; z-index: 1; } .switch__surface-glare, .switch__inner, .switch__inner-glare, .switch__rocker-sides, .switch__rocker-sides-glare, .switch__rocker, .switch__rocker-glare, .switch__light { transition: transform var(--trans-dur) var(--trans-timing); } .switch__surface { background-color: hsl(var(--hue),10%,83%); overflow: hidden; width: 100%; height: 100%; } .switch > .switch__surface { z-index: 0; } .switch__surface-glare { background-image: radial-gradient(50% 50% at center,hsla(var(--hue),10%,90%,1),hsla(var(--hue),10%,90%,0)); width: 3em; height: 3em; transform: translate(0,0.125em); } .switch__input { background-color: hsl(var(--hue),10%,80%); border-radius: 1.5em; box-shadow: 0 0 0 0.125em hsla(var(--hue),90%,50%,0); outline: transparent; position: absolute; top: 0.0625em; left: 0.0625em; width: calc(100% - 0.125em); height: calc(100% - 0.125em); transition: box-shadow 0.15s linear; z-index: 1; -webkit-appearance: none; appearance: none; -webkit-tap-highlight-color: transparent; } .switch__input:focus-visible { box-shadow: 0 0 0 0.125em hsla(var(--hue),90%,50%,1); } .switch__inner { background-color: hsl(var(--hue),10%,83%); overflow: hidden; position: absolute; inset: 0.5em; transform: translate(0.125em,0); } .switch__inner-glare { background-image: radial-gradient(50% 50% at center,hsla(var(--hue),10%,90%,1),hsla(var(--hue),10%,90%,0)); position: absolute; width: 2em; height: 2em; transform: translate(100%,-0.5em); } .switch__inner-shadow { box-shadow: 0 0.125em 0.25em hsla(0,0%,0%,0.4); position: absolute; inset: 0.5em; } .switch__rocker-shadow { box-shadow: 0 0.125em 0.25em hsla(0,0%,0%,0.15); position: absolute; top: 0.5625em; right: 0.5625em; bottom: 0.5625em; left: 0.75em; } .switch__rocker-sides { background-color: hsl(var(--hue),10%,80%); overflow: hidden; position: absolute; inset: 0.5em; transform: rotateY(-20deg) translateZ(0.5em); transform-style: preserve-3d; } .switch__rocker-sides-glare { background-image: linear-gradient(90deg,hsla(var(--hue),10%,85%,0),hsla(var(--hue),10%,85%,1),hsla(var(--hue),10%,85%,0)); position: absolute; width: 200%; height: 100%; } .switch__rocker { background-color: hsl(var(--hue),10%,80%); overflow: hidden; position: absolute; inset: 0.5625em; transform: rotateY(-20deg) translateZ(0.5em); transform-style: preserve-3d; } .switch__rocker-glare { background-image: linear-gradient(120deg,hsla(var(--hue),10%,85%,0) 25%,hsla(var(--hue),10%,85%,1) 50%,hsla(var(--hue),10%,85%,0) 75%); position: absolute; width: 100%; height: 100%; transform: translateX(-33%); } .switch__light { background-image: linear-gradient(-45deg,hsl(var(--hue),10%,60%) 30%,hsl(var(--hue),10%,80%)); box-shadow: 0 0 max(1px,0.05em) hsla(var(--hue),10%,10%,0.3) inset; position: absolute; top: 50%; left: 50%; width: 0.5em; height: 0.5em; transform: translate(-50%,-50%) rotateY(-20deg) translateX(1.125em) translateZ(0.51em); } .switch__light-inner { background-color: hsl(133,90%,50%); box-shadow: 0 0 max(1px,0.05em) hsla(var(--hue),10%,10%,0.3) inset, 0 0 0.25em hsl(133,90%,50%); border-radius: 50%; opacity: 0; width: 100%; height: 100%; transition: opacity var(--trans-dur) var(--trans-timing); } /* Dark theme */ .col--dark { background-c.........完整代码请登录后点击上方下载按钮下载查看
网友评论0