xuan-switch.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <template>
  2. <view class="switch-container" :style="[{ background: bj_color}]">
  3. <view class="switch_view">
  4. <view
  5. class="switch-item"
  6. :class="{'checked_switch':isSwitch}"
  7. :style="isSwitch?`color:${checked_color}`:''"
  8. @click.prevent.stop="changeSwitch(true)"
  9. :animation="animationData2"
  10. >
  11. {{switchList[0]}}
  12. </view>
  13. <view
  14. class="switch-item"
  15. :class="{'checked_switch':!isSwitch}"
  16. :style="!isSwitch?`color:${checked_color}`:''"
  17. @click.prevent.stop="changeSwitch(false)"
  18. :animation="animationData3"
  19. >
  20. {{switchList[1]}}
  21. </view>
  22. </view>
  23. <view class="disabled" v-if="disabled"></view>
  24. <view
  25. class="position_view" :animation="animationData1"
  26. :style="[{ background: checked_bj_color}]"
  27. ></view>
  28. </view>
  29. </template>
  30. <script>
  31. export default {
  32. props: {
  33. switchList: {
  34. type: Array,
  35. default: ()=>{
  36. return ["开","关"]
  37. }
  38. },
  39. defaultSwitch:{
  40. type:Boolean,
  41. default:true
  42. },
  43. isShowModal:{//改变开关时,是否弹框提醒
  44. type:Boolean,
  45. default:false
  46. },
  47. disabled:{
  48. type:Boolean,
  49. default:false
  50. },
  51. bj_color:{
  52. type:String,
  53. default:'#fff',
  54. },
  55. checked_bj_color:{
  56. type:String,
  57. default:'#1989fa',
  58. },
  59. checked_color:{
  60. type:String,
  61. default:'#fff',
  62. }
  63. },
  64. data () {
  65. return {
  66. isSwitch:true,
  67. initAnimation:{},
  68. animationData1: {},
  69. animationData2: {},
  70. animationData3: {}
  71. }
  72. },
  73. created () {
  74. this.initAnimation = uni.createAnimation({
  75. duration: 500,
  76. timingFunction: 'ease',
  77. })
  78. this.isSwitch = this.defaultSwitch
  79. this.changeAnimation()
  80. },
  81. methods: {
  82. changeSwitch(isSwitch) {
  83. if(isSwitch == this.isSwitch || this.disabled){
  84. return
  85. }
  86. if(this.isShowModal){
  87. let index = isSwitch?0:1
  88. let text = this.switchList[index]
  89. uni.showModal({
  90. title: '提示',
  91. content: `您确定要将其调整为${text}吗?`,
  92. success: (res) => {
  93. if(res.confirm){
  94. this.isSwitch = isSwitch
  95. this.changeAnimation()
  96. this.callParentEvent(isSwitch)
  97. }
  98. }
  99. })
  100. }else{
  101. this.isSwitch = isSwitch
  102. this.changeAnimation()
  103. this.callParentEvent(isSwitch)
  104. }
  105. },
  106. changeAnimation(){
  107. if(this.isSwitch){
  108. this.animationData1 = this.initAnimation.left(0).width('60%').step().export()
  109. this.animationData2 = this.initAnimation.width('60%').step().export()
  110. this.animationData3 = this.initAnimation.width('40%').step().export()
  111. }else{
  112. this.animationData1 = this.initAnimation.left('40%').width('60%').step().export()
  113. this.animationData2 = this.initAnimation.width('40%').step().export()
  114. this.animationData3 = this.initAnimation.width('60%').step().export()
  115. }
  116. },
  117. callParentEvent(){
  118. this.$emit("change",this.isSwitch)
  119. }
  120. }
  121. }
  122. </script>
  123. <style lang="scss" scoped>
  124. .switch-container {
  125. display: flex;
  126. flex-direction: row;
  127. width: 320upx;
  128. height: 60upx;
  129. border-radius: 30upx;
  130. border: 1upx solid #E8E8E8;
  131. position: relative;
  132. .switch_view{
  133. position: absolute;
  134. top: 0;
  135. left: 0;
  136. width: 100%;
  137. height: 100%;
  138. z-index: 1;
  139. display: flex;
  140. border-radius: 30upx;
  141. .switch-item {
  142. color: #666;
  143. font-size: 26upx;
  144. height: 100%;
  145. width: 40%;
  146. border-radius: 30upx;
  147. display: flex;
  148. justify-content: center;
  149. align-items: center;
  150. }
  151. }
  152. .position_view{
  153. position: absolute;
  154. top: 0;
  155. left: 0;
  156. width: 60%;
  157. height: 100%;
  158. border-radius: 30upx;
  159. background: $uni-color-primary;
  160. }
  161. .disabled{
  162. position: absolute;
  163. top: 0;
  164. left: 0;
  165. width: 100%;
  166. height: 100%;
  167. z-index: 99;
  168. background: #fff;
  169. opacity: 0.6;
  170. border-radius: 30upx;
  171. }
  172. }
  173. </style>