svg+css+js实现树状checkbox开关效果代码

代码语言:html

所属分类:其他

代码描述:svg+css+js实现树状checkbox开关效果代码

代码标签: svg css js 树状 checkbox 开关

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

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

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

  
  
<style>


*,
*:after,
*:before {
  box-sizing: border-box;
}

body {
  display: grid;
  place-items: center;
  min-height: 100vh;
  font-family: 'SF Pro Text', 'SF Pro Icons', 'AOS Icons', 'Helvetica Neue',
    Helvetica, Arial, sans-serif, system-ui;
}

html {
  color-scheme: light dark;
}

[data-theme='light'] {
  color-scheme: light only;
}

[data-theme='dark'] {
  color-scheme: dark only;
}

body::before {
  --size: 45px;
  --line: color-mix(in lch, canvasText, transparent 70%);
  content: '';
  height: 100vh;
  width: 100vw;
  position: fixed;
  background: linear-gradient(
        90deg,
        var(--line) 1px,
        transparent 1px var(--size)
      )
      50% 50% / var(--size) var(--size),
    linear-gradient(var(--line) 1px, transparent 1px var(--size)) 50% 50% /
      var(--size) var(--size);
  -webkit-mask: linear-gradient(-20deg, transparent 50%, white);
          mask: linear-gradient(-20deg, transparent 50%, white);
  top: 0;
  transform-style: flat;
  pointer-events: none;
  z-index: -1;
}


:where(.x-link):is(:hover, :focus-visible) {
  opacity: 1;
}


/* Utilities */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border-width: 0;
}

[for] {
  --active: 0;
  cursor: pointer;
  height: clamp(24px, var(--size) * 0.5px, 150px);
  width: clamp(48px, var(--size) * 1px, 300px);
  border-radius: 50%;
  position: relative;
  transition: background-color 0.25s;
}

[for]:is(:hover, :has(+ :focus-visible)) {
  background: color-mix(in lch, canvas, canvasText 15%);
}

.tree {
  overflow: visible !important;
  width: 60%;
  position: absolute;
  bottom: 50%;
  left: 50%;
  translate: -50% 10%;
}

:root:has(#tree:focus-visible) [for='tree'] {
  outline: 2px dashed green;
}

[for='tree'] {
  --ease: linear(
    0 0%,
    0.5007 7.21%,
    0.7803 12.29%,
    0.8883 14.93%,
    0.9724 17.63%,
    1.0343 20.44%,
    1.0754 23.44%,
    1.0898 25.22%,
    1.0984 27.11%,
    1.1014 29.15%,
    1.0989 31.4%,
    1.0854 35.23%,
    1.0196 48.86%,
    1.0043 54.06%,
    0.9956 59.6%,
    0.9925 68.11%,
    1 100%
  );
}

:root:has(#tree:checked) [for='tree'] {
  --active: 1;
  --ease: linear(
    0 0%,
    0.0075 31.89%,
    0.0044 40.4%,
    -0.0043 45.94%,
    -0.0196 51.14%,
    -0.0854 64.77%,
    -0.0989 68.6%,
    -0.1014 70.85%,
    -0.0984 72.89%,
    -0.0898 74.78%,
    -0.0754 76.56%,
    -0.0343 79.56%,
    0.0276 82.37%,
    0.1117 85.07%,
    0.2197 87.71%,
    0.4993 92.79%,
    1 100%
  );
}

.tree__layer,
.tree__hole {
  transform-box: fill-box;
  transform-origin: 50% 100%;
  transition: scale, translate;
  transition-duration: calc((var(--speed, 0.5) + (var(--i, 0) * 0.075)) * 1s);
  transition-timing-function: var(--ease);
}

.tree__hole {
  transform-origin: 50% 50%;
}

.tree__layer:nth-of-type(1) {
  --i: 0;
  scale: calc(1 - (var(--active) * 0));
  translate: 0 calc(var(--active) * 35%);
}
.tree__layer:nth-of-type(2) {
  --i: 1;
  scale: calc(1 + (var(--active) * 0.25));
  translate: 0 calc(var(--active) * 90%);
}
.tree__layer:nth-of-type(3) {
  --i: 2;
  scale: calc(1 + (var(--active) * 0.675));
  translate: 0 calc(var(--active) * 190%);
}
.tree__hole {
  scale: calc(0.65 + (var(--active) * 0.75));
}
</style>

  
  
</head>

<body translate="no">
  <label for="tree">
      <span class="sr-only">Toggle tree growth</span>
      <svg
        class="tree"
        viewBox="0 0 274 564"
        fill="none"
        xmlns="http://www.w3.org/2000/svg"
      >
        <ellipse
          class="tree__hole"
          cx="137.5"
          cy="497.5"
          rx="133.5"
          ry="66.5"
          fi.........完整代码请登录后点击上方下载按钮下载查看

网友评论0