js+svg实现radio中性胶囊开关效果代码
代码语言:html
所属分类:表单美化
代码描述:js+svg实现radio中性胶囊开关效果代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <style> @import url("https://fonts.googleapis.com/css2?family=Comfortaa:wght@600&display=swap"); :root { --highlight-left: 0; --pagination-width: 0; } html { height: 100%; } body { font-family: sans-serif; height: 100%; margin: 0; font-family: "Comfortaa", cursive; background-color: #eef3f7; user-select: none; display: flex; align-items: center; justify-content: center; } .radio-checked { position: relative; font-size: 34px; letter-spacing: 1px; box-shadow: -6px -5px 14px 3px #fff, 7px 7px 12px 2px #d0d8e3; border-radius: 70px; } .radio-checked_input { display: none; } .radio-checked_input:checked + .radio-checked_label--on { color: #47cf73; text-shadow: 0 0 7px rgba(71, 207, 115, 0.6); } .radio-checked_input:checked + .radio-checked_label--off { color: #ff3c41; text-shadow: 0 0 7px rgba(255, 60, 65, 0.6); } .radio-checked_input:checked + .radio-checked_label:before { display: none; } .radio-checked_label { cursor: pointer; display: inline-block; padding: 20px 25px 15px; line-height: 1; border-radius: 3rem; color: #acb2c0; transition: all 150ms ease-in-out; } .radio-checked_label:before { content: ""; position: absolute; left: 0; top: 0; right: 0; bottom: 0; display: block; } .radio-checked_container { position: relative; } .radio-checked_highlight { position: absolute; left: 0; bottom: 0; width: var(--pagination-width); height: 100%; border-radius: 70px; box-shadow: inset -5px -5px 10px 4px #fff, inset -2px 6px 8px 6px #d0d8e3; background: #eef3f7; transition: all 0.6s ease; transform: translateX(var(--highlight-left)); } .svg { position: absolute; top: -50%; bottom: -50%; pointer-events: none; } .svg_icon { width: auto; height: 100%; } .svg--right { left: 100%; } .svg--left { right: 100%; } </style> </head> <body > <div class="radio-checked"> <div class="radio-checked_highlight"></div> <div class="radio-checked_container"> <input checked="checked" class="radio-checked_input" id="on" name="status" type="radio" value="on" /><label class="radio-checked_label radio-checked_label--on" for="on"> On</label><input class="radio-checked_input" id="off" name="status" type="radio" value="off" /><label class="radio-checked_label radio-checked_label--off" for="off">Off</label> </div> </div> <script > //#cpc-on-off #codepenchallenge let root = document.documentElement; let color = ["#47cf73", "#7f71fe", "#1A3AF8", "#47cf73"]; var animateDur = "1.4s"; window.onload = function () { let activeChecked = document.querySelector( ".radio-checked_input:checked + .radio-checked_label"); let left = activeChecked.offsetLeft; let width = activeChecked.offsetWidth; animation(left, width); }; document. querySelectorAll(".radio-checked_label"). forEach(t => t.addEventListener("click", onChecked)); function onChecked(e) { let left = e.target.offsetLeft; let width = e.target.offsetWidth; animation(left, width); } var bool = true; function animation(left, width) { root.style.setProperty("--pagination-width", width + "px"); root.style.setProperty("--highlight-left", left + "px"); (bool = !bool) ? svgCreate("right") : svgCreate("left"); } function svgCreate(position) { let svgLeft = `<svg class="svg_icon" fill="none" viewBox="0 0 132 140" xmlns="http://www.w3.org/2000/svg"> <path d="M110 50.4209L68.7302 25.4209" id="Line1"/> <path d="M110 59.498L45.9597 54.498" id="Line2"/> <path d="M110 70.4824L46.1323 87.4824" id="Line3"/> <path d="M110 79.3584L73.3484 113.358" id="Line4"/> <path d="M110 73.4473L34.2236 110.447" id="Line5"/> <path d="M110 64.499L24.0356 70.499" id="Line6"/> <path d="M110 54.4736L39.8398 31.4736" id="Line7"/> <path d="M110 56.4893L23.8953 38.4893" id="Line8"/> <path d="M110 67.4893L21.1013 85.4893" id="Line9"/> <path d="M110 85.2861L85.4102 118.286" id="Line10"/> <path d="M110 76.4111L50.2839 116.411" id="Line11"/> <path d="M110 61.4995L18.9907 59.4307" id="Line12"/> <g> <g> <g class="svg_obj"> <path d="M14.6324 5.25L20.9832 16.25C21.5606 17.25 20.8389 18.5 19.6842 18.5H6.98249C5.82778 18.5 5.10609 17.25 5.68344 16.25L12.0343 5.25C12.6116 4.25 1.........完整代码请登录后点击上方下载按钮下载查看
网友评论0