app-switch.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <template>
  2. <view class="app-view app-background" @click.stop="switchChange" >
  3. <view :class="[themeColor]" class="app" :style="{'width': `${x}rpx`,'opacity': `${x === 0? '0':'1'}`,'transition': 'all .3s'}"></view>
  4. <view class="app-tab" @click.stop="switchChange" :style="{'transform': `translateX(${x}%)`, 'transition': 'all .5s'}"></view>
  5. </view>
  6. </template>
  7. <script>
  8. export default {
  9. name: 'app-switch',
  10. data() {
  11. return {
  12. x: 0,
  13. switch: this.value,
  14. }
  15. },
  16. props: {
  17. theme: String,
  18. value: {
  19. default: false,
  20. }
  21. },
  22. methods: {
  23. switchChange() {
  24. this.switch = !this.switch;
  25. this.$emit('input', this.switch);
  26. }
  27. },
  28. computed: {
  29. themeColor: function() {
  30. if (this.x === 88) {
  31. return `${this.theme}-background`;
  32. } else {
  33. return '';
  34. }
  35. }
  36. },
  37. watch: {
  38. value: {
  39. handler: function(res) {
  40. if (res === false) {
  41. this.x = 0;
  42. } else if (res === true){
  43. this.x = 88;
  44. }
  45. },
  46. immediate: true
  47. }
  48. }
  49. }
  50. </script>
  51. <style lang="scss">
  52. .app {
  53. width: 0;
  54. height: #{48rpx};
  55. border-radius: #{50rpx};
  56. position: absolute;
  57. top: 0;
  58. left: 0;
  59. }
  60. .app-view {
  61. width: #{88rpx};
  62. height: #{48rpx};
  63. border-radius: #{50rpx};
  64. border: #{1rpx} solid $uni-general-color-three;
  65. position: relative;
  66. }
  67. .app-tab {
  68. width: #{50rpx};
  69. height: #{50rpx};
  70. background-color:white;
  71. border-radius: 50%;
  72. border: #{1rpx} solid $uni-general-color-three;
  73. position: absolute;
  74. top: 0;
  75. left: 0;
  76. }
  77. .app-background {
  78. background-color: #ffffff;
  79. }
  80. </style>