SlidingSwitch.vue 2.12 KB
<script lang="ts" setup>
const props = defineProps<{
  value?: boolean
}>()

const emit = defineEmits(['update:value', 'change'])

const handleChange = (event: Event) => {
  const _value = (event.target as HTMLInputElement).checked
  emit('update:value', _value)
  emit('change', _value)
}
</script>

<template>
  <label class="sliding-switch">
    <input :value="props.value" type="checkbox" :checked="props.value" @change="handleChange">
    <span class="slider" />
    <span class="on">ON</span>
    <span class="off">OFF</span>
  </label>
</template>

<style scoped lang="less">
.sliding-switch {
  position: relative;
  display: block;
  font-weight: 700;
  line-height: 40px;
  width: 80px;
  height: 40px;
  font-size: 14px;
  cursor: pointer;
  user-select: none;

  input[type='checkbox'] {
    display: none;
  }

  .slider {
    width: 80px;
    height: 40px;
    box-sizing: border-box;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    border: 2px solid #ecf0f3;
    border-radius: 20px;
    box-shadow: -2px -2px 8px #fff, -2px -2px 12px hsl(0deg 0% 100% / 50%), inset -2px -2px 8px #fff, inset -2px -2px 12px hsl(0deg 0% 100% / 50%), inset 2px 2px 4px hsl(0deg 0% 100% / 10%),
      inset 2px 2px 8px rgb(0 0 0 / 30%), 2px 2px 8px rgb(0 0 0 / 30%);
    background-color: #ecf0f3;
    z-index: -1;
  }

  .slider::after {
    position: absolute;
    cursor: pointer;
    display: block;
    content: '';
    width: 24px;
    height: 24px;
    border-radius: 50%;
    top: 6px;
    left: 6px;
    background-color: #ecf0f3;
    box-shadow: -2px -2px 8px #fff, -2px -2px 12px hsl(0deg 0% 100% / 50%), inset 2px 2px 4px hsl(0deg 0% 100% / 10%), 2px 2px 8px rgb(0 0 0 / 30%);
    z-index: 999;
    transition: 0.5s;
  }

  input:checked ~ .off {
    opacity: 0;
  }

  input:checked ~ .slider::after {
    transform: translateX(35px);
  }
  input:not(:checked) ~ .on {
    opacity: 0;
    transform: translateX(0px);
  }

  .on,
  .off {
    display: inline-block;
    margin-left: 3px;
    width: 34px;
    text-align: center;
    transition: 0.2s;
  }

  .on {
    color: #039be5;
  }
  .off {
    color: #999;
  }
}
</style>