su-switch.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <template>
  2. <view class="ui-switch" :class="[{ disabled: props.disabled }, props.ui]">
  3. <view class="ui-switch-wrapper" @tap="change">
  4. <view
  5. class="ui-switch-input"
  6. :class="[
  7. { 'ui-switch-input-checked': props.modelValue },
  8. props.modelValue ? props.bg : '',
  9. props.text,
  10. props.size,
  11. ]"
  12. ></view>
  13. </view>
  14. </view>
  15. </template>
  16. <script>
  17. export default {
  18. name: 'UiSwitch',
  19. };
  20. </script>
  21. <script setup>
  22. const props = defineProps({
  23. modelValue: {
  24. type: [Boolean, Number],
  25. default: false,
  26. },
  27. ui: {
  28. type: String,
  29. default: '',
  30. },
  31. bg: {
  32. type: String,
  33. default: 'ui-BG-Main',
  34. },
  35. text: {
  36. type: String,
  37. default: '',
  38. },
  39. size: {
  40. type: String,
  41. default: 'sm',
  42. },
  43. disabled: {
  44. type: Boolean,
  45. default: false,
  46. },
  47. });
  48. const emits = defineEmits(['update:modelValue']);
  49. const change = () => {
  50. emits('update:modelValue', !props.modelValue);
  51. };
  52. </script>
  53. <style lang="scss" scoped>
  54. .ui-switch {
  55. display: inline-block;
  56. cursor: pointer;
  57. .ui-switch-wrapper {
  58. display: inline-flex;
  59. align-items: center;
  60. vertical-align: middle;
  61. }
  62. .ui-switch-input {
  63. appearance: none;
  64. position: relative;
  65. width: 47px;
  66. height: 26px;
  67. outline: 0;
  68. border-radius: 16px;
  69. box-sizing: border-box;
  70. background-color: rgba(119, 119, 119, 0.3);
  71. transition: background-color 0.1s, border 0.1s;
  72. &:after {
  73. content: ' ';
  74. position: absolute;
  75. top: 0;
  76. left: 0;
  77. border-radius: 200px;
  78. transition: transform 0.3s;
  79. width: 20px;
  80. height: 20px;
  81. margin: 3px;
  82. background-color: #fff;
  83. }
  84. &.ui-switch-input-checked {
  85. &:after {
  86. transform: translateX(21px);
  87. }
  88. }
  89. }
  90. &.disabled {
  91. cursor: not-allowed;
  92. .ui-switch-input {
  93. opacity: 0.7;
  94. }
  95. }
  96. }
  97. </style>