index.vue 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <template>
  2. <!-- 本组件仅支持外部变更 prop value 改变切换switch状态,避免内部因异步问题出现 switchbar 显示状态与预期状态不符情况 -->
  3. <div :class="['tui-switch', value ? 'tui-switch-checked' : 'tui-switch-no-checked']" />
  4. </template>
  5. <script setup lang="ts">
  6. defineProps({
  7. value: {
  8. type: Boolean,
  9. default: false,
  10. },
  11. });
  12. </script>
  13. <style scoped lang="scss">
  14. .tui-switch {
  15. margin: 2px 5px;
  16. width: 48px;
  17. height: 30px;
  18. position: relative;
  19. border: 1px solid transparent;
  20. box-shadow: #dfdfdf 0 0 0 0 inset;
  21. border-radius: 20px;
  22. background-clip: content-box;
  23. display: inline-block;
  24. appearance: none;
  25. -webkit-appearance: none;
  26. -moz-appearance: none;
  27. user-select: none;
  28. outline: none;
  29. &::before {
  30. content: '';
  31. position: absolute;
  32. width: 24px;
  33. height: 24px;
  34. background-color: #fff;
  35. border-radius: 50%;
  36. top: 0;
  37. bottom: 0;
  38. margin: auto;
  39. transition: 0.3s;
  40. }
  41. &-checked {
  42. background-color: #007aff;
  43. transition: 0.6s;
  44. &::before {
  45. transition: 0.3s;
  46. left: 20px;
  47. }
  48. &:active::before {
  49. width: 28px;
  50. left: 16px;
  51. transition: 0.3s;
  52. }
  53. }
  54. &-no-checked {
  55. background-color: #dcdfe6;
  56. transition: 0.6s;
  57. &::before {
  58. left: 2px;
  59. transition: 0.3s;
  60. }
  61. &:active::before {
  62. width: 28px;
  63. transition: 0.3s;
  64. }
  65. }
  66. }
  67. </style>