xuan-switch.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. id:{
  64. type:null,
  65. default:null
  66. }
  67. },
  68. data () {
  69. return {
  70. isSwitch:true,
  71. initAnimation:{},
  72. animationData1: {},
  73. animationData2: {},
  74. animationData3: {}
  75. };
  76. },
  77. created () {
  78. this.initAnimation = uni.createAnimation({
  79. duration: 500,
  80. timingFunction: 'ease'
  81. });
  82. this.isSwitch = this.defaultSwitch;
  83. this.changeAnimation();
  84. },
  85. methods: {
  86. changeSwitch(isSwitch) {
  87. if(isSwitch == this.isSwitch || this.disabled){
  88. return;
  89. }
  90. if(this.isShowModal){
  91. let index = isSwitch?0:1;
  92. let text = this.switchList[index];
  93. uni.showModal({
  94. title: '提示',
  95. content: `您确定要将其调整为${text}吗?`,
  96. success: (res) => {
  97. if(res.confirm){
  98. this.isSwitch = isSwitch;
  99. this.changeAnimation();
  100. this.callParentEvent(isSwitch);
  101. }
  102. }
  103. });
  104. }else{
  105. this.isSwitch = isSwitch;
  106. this.changeAnimation();
  107. this.callParentEvent(isSwitch);
  108. }
  109. },
  110. changeAnimation(){
  111. if(this.isSwitch){
  112. this.animationData1 = this.initAnimation.left(0).width('60%').step().export();
  113. this.animationData2 = this.initAnimation.width('60%').step().export();
  114. this.animationData3 = this.initAnimation.width('40%').step().export();
  115. }else{
  116. this.animationData1 = this.initAnimation.left('40%').width('60%').step().export();
  117. this.animationData2 = this.initAnimation.width('40%').step().export();
  118. this.animationData3 = this.initAnimation.width('60%').step().export();
  119. }
  120. },
  121. callParentEvent(){
  122. this.$emit('change',this.isSwitch,this.id,()=>{
  123. // 回调方法应用场景:父级组件请求api接口失败调用
  124. this.isSwitch = !this.isSwitch;
  125. this.changeAnimation();
  126. });
  127. }
  128. }
  129. };
  130. </script>
  131. <style lang="scss" scoped>
  132. .switch-container {
  133. display: flex;
  134. flex-direction: row;
  135. width: 398upx;
  136. height: 76upx;
  137. border-radius: 80upx;
  138. border: 3upx solid $fuint-theme;
  139. font-weight: bold;
  140. position: relative;
  141. .switch_view{
  142. position: absolute;
  143. top: 0;
  144. left: 0;
  145. width: 100%;
  146. height: 100%;
  147. z-index: 1;
  148. display: flex;
  149. border-radius: 100upx;
  150. .switch-item {
  151. color: #666;
  152. font-size: 24upx;
  153. height: 100%;
  154. width: 40%;
  155. border-radius: 100upx;
  156. display: flex;
  157. justify-content: center;
  158. align-items: center;
  159. }
  160. }
  161. .position_view{
  162. position: absolute;
  163. top: 0;
  164. left: 0;
  165. width: 60%;
  166. height: 100%;
  167. border-radius: 100upx;
  168. background: $uni-color-primary;
  169. }
  170. .disabled{
  171. position: absolute;
  172. top: 0;
  173. left: 0;
  174. width: 100%;
  175. height: 100%;
  176. z-index: 99;
  177. background: #fff;
  178. opacity: 0.6;
  179. border-radius: 100upx;
  180. }
  181. }
  182. </style>