switch.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <template>
  2. <view :class="'switch-container switch-container-size-'+propSize" :style="'background:'+propBgColor+';border:1px solid '+propBrColor+';'">
  3. <view class="switch_view">
  4. <view
  5. :class="'switch-item '+propCheckedClass+' '+(isSwitch ? 'checked_switch' : '')"
  6. :style="isSwitch?`color:${propCheckedColor}`:''"
  7. @click.prevent.stop="changeSwitch(true)"
  8. :animation="animationData2"
  9. >
  10. {{propSwitchList[0]}}
  11. </view>
  12. <view
  13. class="switch-item"
  14. :class="{'checked_switch':!isSwitch}"
  15. :style="!isSwitch?`color:${propCheckedColor}`:''"
  16. @click.prevent.stop="changeSwitch(false)"
  17. :animation="animationData3"
  18. >
  19. {{propSwitchList[1]}}
  20. </view>
  21. </view>
  22. <view class="disabled" v-if="propDisabled"></view>
  23. <view
  24. :class="'position_view '+propCheckedBgClass" :animation="animationData1"
  25. :style="[{ background: propCheckedBgColor}]"
  26. ></view>
  27. </view>
  28. </template>
  29. <script>
  30. export default {
  31. props: {
  32. propSwitchList: {
  33. type: Array,
  34. default: ['开','关']
  35. },
  36. propSize:{
  37. type:String,
  38. default:'sm'
  39. },
  40. propDefault:{
  41. type:Boolean,
  42. default:true
  43. },
  44. propIsShowModal:{
  45. type:Boolean,
  46. default:false
  47. },
  48. propDisabled:{
  49. type:Boolean,
  50. default:false
  51. },
  52. propBgColor:{
  53. type:String,
  54. default:'#fff'
  55. },
  56. propBrColor:{
  57. type:String,
  58. default:'#ccc'
  59. },
  60. propCheckedBgColor:{
  61. type:String,
  62. default:'#4caf50'
  63. },
  64. propCheckedBgClass:{
  65. type:String,
  66. default:''
  67. },
  68. propCheckedColor:{
  69. type:String,
  70. default:'#fff'
  71. },
  72. propCheckedClass:{
  73. type:String,
  74. default:''
  75. },
  76. propId:{
  77. type:null,
  78. default:null
  79. }
  80. },
  81. data () {
  82. return {
  83. isSwitch:true,
  84. initAnimation:{},
  85. animationData1: {},
  86. animationData2: {},
  87. animationData3: {}
  88. };
  89. },
  90. created () {
  91. this.initAnimation = uni.createAnimation({
  92. duration: 500,
  93. timingFunction: 'ease'
  94. });
  95. this.isSwitch = this.propDefault;
  96. this.changeAnimation();
  97. },
  98. methods: {
  99. changeSwitch(isSwitch) {
  100. if(isSwitch == this.isSwitch || this.propDisabled){
  101. return;
  102. }
  103. if(this.propIsShowModal){
  104. let index = isSwitch?0:1;
  105. let text = this.propSwitchList[index];
  106. uni.showModal({
  107. title: '提示',
  108. content: `您确定要将其调整为${text}吗?`,
  109. success: (res) => {
  110. if(res.confirm){
  111. this.isSwitch = isSwitch;
  112. this.changeAnimation();
  113. this.callParentEvent(isSwitch);
  114. }
  115. }
  116. });
  117. }else{
  118. this.isSwitch = isSwitch;
  119. this.changeAnimation();
  120. this.callParentEvent(isSwitch);
  121. }
  122. },
  123. changeAnimation(){
  124. if(this.isSwitch){
  125. this.animationData1 = this.initAnimation.left(0).width('60%').step().export();
  126. this.animationData2 = this.initAnimation.width('60%').step().export();
  127. this.animationData3 = this.initAnimation.width('40%').step().export();
  128. }else{
  129. this.animationData1 = this.initAnimation.left('40%').width('60%').step().export();
  130. this.animationData2 = this.initAnimation.width('40%').step().export();
  131. this.animationData3 = this.initAnimation.width('60%').step().export();
  132. }
  133. },
  134. callParentEvent(){
  135. this.$emit('change',this.isSwitch, this.propId,()=>{
  136. // 回调方法应用场景:父级组件请求api接口失败调用
  137. this.isSwitch = !this.isSwitch;
  138. this.changeAnimation();
  139. });
  140. }
  141. }
  142. };
  143. </script>
  144. <style>
  145. .switch-container {
  146. display: flex;
  147. flex-direction: row;
  148. width: 180rpx;
  149. height: 60rpx;
  150. line-height: 60rpx;
  151. border-radius: 1000px;
  152. position: relative;
  153. }
  154. .switch-container .switch_view {
  155. position: absolute;
  156. top: 0;
  157. left: 0;
  158. width: 100%;
  159. height: 100%;
  160. z-index: 1;
  161. display: flex;
  162. border-radius: 1000px;
  163. }
  164. .switch-container .switch_view .switch-item {
  165. color: #666;
  166. font-size: 24rpx;
  167. height: 100%;
  168. width: 40%;
  169. border-radius: 1000px;
  170. display: flex;
  171. justify-content: center;
  172. align-items: center;
  173. }
  174. .switch-container .position_view {
  175. position: absolute;
  176. top: 0;
  177. left: 0;
  178. width: 60%;
  179. height: 100%;
  180. border-radius: 1000px;
  181. background: #1AAD19;
  182. }
  183. .switch-container .disabled {
  184. position: absolute;
  185. top: 0;
  186. left: 0;
  187. width: 100%;
  188. height: 100%;
  189. z-index: 99;
  190. background: #fff;
  191. opacity: 0.6;
  192. border-radius: 1000px;
  193. }
  194. .switch-container-size-xs {
  195. width: 150rpx;
  196. height: 45rpx;
  197. line-height: 45rpx;
  198. }
  199. </style>