Switch.vue 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <template>
  2. <div class="track aria-theme-independent" :class="value ? 'on' : 'off'"
  3. @click.passive="onClick"
  4. @keydown.enter.passive="onEnter"
  5. >
  6. <div class="slider"></div>
  7. <input v-show="false" type="checkbox" :id="id">
  8. </div>
  9. </template>
  10. <script>
  11. export default {
  12. props: {
  13. value: {
  14. type: Boolean,
  15. required: true,
  16. },
  17. id: {
  18. type: String,
  19. default: '',
  20. }
  21. },
  22. methods: {
  23. onClick() {
  24. this.$emit('input', !this.value)
  25. },
  26. onEnter() {
  27. this.$emit('input', !this.value)
  28. },
  29. }
  30. }
  31. </script>
  32. <style lang="less" scoped>
  33. .track {
  34. width: 40px;
  35. height: 20px;
  36. border-radius: 10px;
  37. position: relative;
  38. transition: all 0.3s;
  39. cursor: pointer;
  40. display: inline-block;
  41. vertical-align: middle;
  42. .slider {
  43. width: 16px;
  44. height: 16px;
  45. border-radius: 50%;
  46. background: white;
  47. position: absolute;
  48. top: 2px;
  49. }
  50. }
  51. .off {
  52. background: #DCDFE6;
  53. .slider {
  54. left: 2px;
  55. right: auto;
  56. }
  57. }
  58. .on {
  59. background: #AB3434;
  60. .slider {
  61. left: auto;
  62. right: 2px;
  63. }
  64. }
  65. </style>