点击查看html编辑器说明文档

Toggle动画edit icon

|
|
Fork(复制)
|
|
作者:
穿越者X57

👉 新版编辑器已上线,点击进行体验吧!

BUG反馈
嵌入
设置
下载
HTML
格式化
支持Emmet,输入 p 后按 Tab键试试吧!
<head> ...
展开
</head>
<body>
            
            <input type="checkbox" role="switch" class="toggle" />
<input type="checkbox" role="switch" class="toggle" checked />
<input type="checkbox" role="switch" class="toggle" disabled />
<input type="checkbox" role="switch" class="toggle" disabled checked />

        
</body>
CSS
格式化
            
            body {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  margin: 0;
  min-height: 100vh;
  font-size: 40px;
  gap: 0.25em;
}

@property --initial-angle {
  syntax: '<angle>';
  initial-value: 90deg;
  inherits: true;
}

@property --arc {
  syntax: '<angle>';
  initial-value: 360deg;
  inherits: true;
}

@property --initial-angle2 {
  syntax: '<angle>';
  initial-value: 90deg;
  inherits: true;
}

@property --arc2 {
  syntax: '<angle>';
  initial-value: 360deg;
  inherits: true;
}

@keyframes delCircle1 {
  0% { 
    --left: 0.1em;
    --arc: 360deg; 
  }
  100% { 
    --left: 0.1em;
    --arc: 0deg; 
  }
}

@keyframes showCircle2 {
  0% {
    --initial-angle2: 270deg;
    --arc2: 0deg;
  }
  50% {
    --initial-angle2: 270deg;
    --arc2: 180deg;
  }
  100% {
    --initial-angle2: 450deg;
    --arc2: 0deg;
  }
}

@keyframes showCircle3 {
  0% {
    --left: 1.6em;
    --initial-angle: 270deg;
    --arc: 0deg;
  }
  100% {
    --left: 1.6em;
    --initial-angle: -90deg;
    --arc: 360deg;
  }
}

@keyframes moveLeft {
  to { --left: 0.1em; }
}


.toggle {
  --arc-width: 6deg;
  --time: 0.25s;
  --arc: 360deg;
  --initial-angle: 90deg;
  --left: 0.1em;
  --initial-angle2: 270deg;
  --arc2: 0deg;
  appearance: none;
  position: relative;
  font-size: 1em;
  box-sizing: content-box;
  height: 1em;
  width: 2.5em;
  border: max(1px, 0.05em) solid;
  border-radius: 2em;
/*   background: 
    linear-gradient(#f001 0 0) 0.1em 0.1em / 0.8em 0.8em,
    linear-gradient(#f001 0 0) 0.85em 0.1em / 0.8em 0.8em,
    linear-gradient(#f001 0 0) 1.6em 0.1em / 0.8em 0.8em; */
  background-repeat: no-repeat;
  transition: left 0.5s;
  animation: none;
  
  &::before {
    content: "";
    position: absolute;
    width: 0.8em;
    height: 0.8em;
    top: 0.1em;
    left: var(--left);
    border: max(1px, 0.05em) solid;
    box-sizing: border-box;
    border-radius: 50%;
    mask: conic-gradient(from var(--initial-angle), #000 var(--arc), #0000 0);
    transition: left 0.3s;
  }
  
  &::after {
    content: "";
    position: absolute;
    width: 0.8em;
    height: 0.8em;
    top: 0.1em;
    left: 0.85em;
    border: 0.05em solid;
    box-sizing: border-box;
    border-radius: 50%;
    mask: conic-gradient(from var(--initial-angle2), #000 var(--arc2), #0000 0);
    transition: left 0.3s;
  }
  
  &:checked {
    animation:
      delCircle1  calc(var(--time) * 1.0) calc(var(--time) * 0.0) linear forwards,
      showCircle2 calc(var(--time) * 2.0) calc(var(--time) * 0.0) linear forwards,
      showCircle3 calc(var(--time) * 1.0) calc(var(--time) * 1.0) linear forwards
      ;
    
    &::before {
      transition: none;
    }
  }
  
  &[disabled] {
    opacity: 0.25;
  }
}

@media print {
  .toggle {
    &, &::before, &::after {
      -webkit-print-color-adjust: exact;
      print-color-adjust: exact;
    }
  }
}

@media (prefers-reduced-motion) {
  .toggle {
    &, &::before, &::after {
      transition: none !important;
      animation-duration: 0s !important;
      animation-delay: 0s !important;
    }
  }
}
        
JS
格式化
            
            
        
预览
控制台