云链智安app
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

xuan-switch.vue 4.4KB

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