div+css实现炫酷胶囊开关点击切换效果代码

代码语言:html

所属分类:其他

代码描述:div+css实现炫酷胶囊开关点击切换效果代码

代码标签: div css 炫酷 胶囊 开关 点击 切换

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

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

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

  
  
  
<style>
* {
  box-sizing: border-box;
}

:root {
  background-color: hsl(208.7, 100%, 9%);

  --on-color: hsl(208.7, 100%, 50%);
  --off-color: hsl(208.7, 100%, 90%);
  --toggle-bg: hsl(208.7, 40%, 20%);
  --toggle-gap: 10px;
  --toggle-width: 160px;
  --transition-color: var(--off-color);
}

body {
  margin: 0;
  display: flex;
  place-items: center;
  min-width: 320px;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.toggle {
  position: relative;
  cursor: pointer;
  background: var(--toggle-bg);
  width: var(--toggle-width);
  height: calc(var(--toggle-width) / 2);
  border-radius: 160px;
  overflow: hidden;
  display: flex;
  align-items: center;
  box-shadow: inset 0 0 2px #7c7e8855;
  -webkit-tap-highlight-color: transparent;
}

.toggle input {
  appearance: none;
  margin: 0;
  padding: 0;
}

.toggle input:checked ~ svg .outline {
  --transition-color: var(--on-color);
}

.off-knob,
.on-knob {
  position: absolute;
  height: calc(100% - var(--toggle-gap) * 2);
  aspect-ratio: 1;
  border-radius: 50%;
  box-shadow: 0px 1px 1px rgba(10, 19, 39, 0.1),
    0px 4px 12px rgba(10, 19, 39, 0.03), 0px 4px 20px rgba(10, 19, 39, 0.04);
  transform-origin: center center;
  will-change: transform;
}

.off-knob {
  left: var(--toggle-gap);
  background: var(--off-color);
}

.on-knob {
  right: var(--toggle-gap);
  background: var(--on-color);
  transform: scale(0.1) translateX(430px);
}

.toggle input:checked ~ .on-knob {
  animation-delay: 1.1s;
}

svg {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
}

.outline {
  width: 100%;
  height: 100%;
  stroke: var(--transition-color);
  stroke-dasharray: 2 125;
  stroke-dashoffset: 32.5;
  stroke-opacity: 0;
}

.outline--blur {
  filter: blur(0.8px);
}
</style>


  
</head>

<body >
  <label class="toggle">
  <input type="checkbox" />
  <div class="off-knob"></div>
  <div class="on-knob"></div>

  <svg viewBox="0 0 44 22" xmlns="http://www.w3.org/2000/svg">
    <rect
      class="outline"
      fill="none"
      rx="11"
      stroke-linejoin="round"
      stroke-linecap="round"
      stroke-width="1"
    />
    <rect
      class="outline outline--blur"
      fill="none"
      rx="11"
      stroke-linejoin="round"
      stroke-linecap="round"
      stroke-width="1"
    />
  </svg>
</label>

  
      <script >
const DURATION = 500;

Array.from(document.querySelectorAll('.toggle')).forEach((toggle) =>
initToggle(toggle));


function initToggle(toggle) {
  const input = toggle.querySelector('input');
  const offKnob = toggle.querySelector('.off-knob');
  const onKnob = toggle.querySelector('.on-knob');
  const outline = toggle.querySelector('.outline');
  const outlineBlur = toggle.querySelector('.outline--blur');

  input.addEventListener('click', e => {
    const animating = toggle.
    getAnimations({ subtree: true }).
    some(animation => animation.playState !== 'finished');

    if (animating) {
      e.preventDefault();
      return;
    }

    if (input.checked) {
      animateOffKnobCheck();
      animateOnKnobCheck();
      animateOutlineCheck();
      return;
    }

    animateOffKnobUncheck();
    animateOnKnobUncheck();
    animateOutlineUncheck();
  });

  function animateOffKnobCheck() {
    offKnob.animate(
    [
    .........完整代码请登录后点击上方下载按钮下载查看

网友评论0