tm-switch.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <!-- 开关。 -->
  2. <template>
  3. <view @click="onclick" class="d-inline-block tm-switch " >
  4. <view class="tm-switch-wk relative " :style="[{width:width+'rpx',height:(height+4)+'rpx'}]">
  5. <view class="tm-switch-bg round-l-24 round-r-24 flex-between"
  6. :class="[bgColorCusjs,loaddingState?'opacity-7':'']"
  7. >
  8. <view class="text-size-xs tm-switch-txt text-align-center flex-center" style="width:50%">
  9. <text>{{text[0]?text[0]:''}}</text>
  10. </view>
  11. <view class="text-size-xs tm-switch-txt text-align-center flex-center" style="width:50%">
  12. <text class="pr-8">{{text[1]?text[1]:''}}</text>
  13. </view>
  14. </view>
  15. <!-- bar -->
  16. <view class="tm-switch-wk-bar absolute round-24 white flex-center "
  17. :style="[
  18. {transform:changValue?`translateX(${(width-(width/2+4))}rpx)`:`translateX(2rpx)`},
  19. {width:(width/2-4)+'rpx',height:(height-4)+'rpx'}
  20. ]"
  21. :class="[changValue?'shadow-'+color+'-10':'',changValue?'on aniOn':'aniOff']">
  22. <text v-if="loaddingState" class="iconfont icon-loading" :class="[loaddingState?'load':'',`text-${color_tmeme}`]"></text>
  23. </view>
  24. </view>
  25. </view>
  26. </template>
  27. <script>
  28. /**
  29. * 开关
  30. * @property {String|Boolean} value = [true|false] 同v-model一样的值,如果需要双向绑定需要value.sync,推荐使用v-model
  31. * @property {String|Number|Boolean|Object} name = [] 自定义数据,change时携带。
  32. * @property {Function} change {checked,value:携带的name数据}
  33. * @property {Boolean} disabled = [true|false] 默认:false, 禁用
  34. * @property {String} color = [] 默认:primary, 主题色
  35. * @property {Boolean} black = [true|false] 默认:false, 暗黑模式
  36. * @property {Boolean} loadding = [true|false] 默认:false, 是否加载中
  37. * @property {Array} text = [true|false] 默认: ['开','关'], 左右两边的字符
  38. * @property {Number} width = [] 默认:100,单位rpx,宽度
  39. * @property {Number} height = [] 默认:50,单位rpx,高度
  40. * @property {String} offBgcolor = [] 默认:'grey-lighten-2 text-grey', 主题色可以是文字色组合来改变文字和背景色.
  41. * @example <tm-switch v-model="checked"></tm-switch>
  42. *
  43. */
  44. export default {
  45. name: 'tm-switch',
  46. model: {
  47. prop: 'value',
  48. event: 'input'
  49. },
  50. props:{
  51. value:{
  52. type:String|Boolean,
  53. default:false
  54. },
  55. name:{
  56. type:String|Number|Boolean|Object,
  57. default:''
  58. },
  59. // 禁用。
  60. disabled: {
  61. type:Boolean|String,
  62. default:false
  63. },
  64. loadding: {
  65. type:Boolean|String,
  66. default:false
  67. },
  68. color:{
  69. type:String,
  70. default:'primary'
  71. },
  72. // 暗黑
  73. black: {
  74. type:Boolean|String,
  75. default:null
  76. },
  77. // 左右两边的字符。
  78. text:{
  79. type:Array,
  80. default:()=>{
  81. return ['开','关']
  82. }
  83. },
  84. width:{
  85. type:String|Number,
  86. default:100
  87. },
  88. height:{
  89. type:String|Number,
  90. default:50
  91. },
  92. offBgcolor:{
  93. type:String,
  94. default:'grey-lighten-2 text-grey'
  95. },
  96. // 跟随主题色的改变而改变。
  97. fllowTheme:{
  98. type:Boolean|String,
  99. default:true
  100. }
  101. },
  102. watch: {
  103. value: function(newval, oldval) {
  104. if (newval !== oldval) {
  105. if (!this.jiancMax()) {
  106. this.changValue = false;
  107. return;
  108. }
  109. }
  110. }
  111. },
  112. computed: {
  113. black_tmeme: function() {
  114. if (this.black !== null) return this.black;
  115. return this.$tm.vx.state().tmVuetify.black;
  116. },
  117. color_tmeme:function(){
  118. if(this.$tm.vx.state().tmVuetify.color!==null&&this.$tm.vx.state().tmVuetify.color && this.fllowTheme){
  119. return this.$tm.vx.state().tmVuetify.color;
  120. }
  121. return this.color;
  122. },
  123. changValue: {
  124. get: function() {
  125. return this.value;
  126. },
  127. set: function(newValue) {
  128. this.$emit('input', newValue)
  129. // 如果不想用v-model. 直接使用value,需要:value.sync
  130. this.$emit('update:value', newValue);
  131. }
  132. },
  133. loaddingState:function(){
  134. return this.loadding;
  135. },
  136. bgColorCusjs(){
  137. if(this.disabled) {
  138. if(this.black_tmeme){
  139. return 'grey-darken-4 bk';
  140. }else{
  141. return this.color_tmeme+' opacity-5';
  142. }
  143. }
  144. if(this.black_tmeme){
  145. if(this.changValue) return this.color_tmeme + ' bk ';
  146. if(!this.changValue) return 'grey-darken-1 bk ';
  147. }else{
  148. if(this.changValue) return this.color_tmeme + ' text-white ';
  149. if(!this.changValue) return this.offBgcolor;
  150. }
  151. }
  152. },
  153. data() {
  154. return {
  155. animationOn:null,
  156. animationOff:null,
  157. aniData:null,
  158. };
  159. },
  160. mounted() {
  161. },
  162. methods: {
  163. // 检查是否超过了最大选择。
  164. jiancMax() {
  165. if(this.disabled) return false;
  166. return true;
  167. },
  168. onclick(e) {
  169. if (this.disabled||this.loaddingState) return;
  170. if (!this.jiancMax()) {
  171. return;
  172. }
  173. this.changValue = !this.changValue;
  174. this.change();
  175. },
  176. change() {
  177. this.$nextTick(function(){
  178. this.$emit('change', {
  179. checked: this.changValue,
  180. value: this.name
  181. });
  182. })
  183. }
  184. },
  185. }
  186. </script>
  187. <style lang="scss" scoped>
  188. .tm-switch{
  189. vertical-align: middle;
  190. .tm-switch-wk{
  191. // width: 100rpx;
  192. // height: 52rpx;
  193. }
  194. .tm-switch-bg{
  195. width: 100%;
  196. height: 100%;
  197. transition: all 0.5s;
  198. .tm-switch-txt{
  199. // width: 50upx;
  200. height: 100%;
  201. // line-height: 48upx;
  202. }
  203. }
  204. .tm-switch-wk-bar{
  205. left: 4rpx;
  206. top: 4rpx;
  207. // width:40rpx;
  208. // height: 46rpx;
  209. transition: all 0.35s ease-in-out;
  210. .load{
  211. animation: xhRote 0.8s infinite linear;
  212. }
  213. }
  214. }
  215. .aniOn{
  216. left: inherit;
  217. }
  218. .aniOff{
  219. right: inherit;
  220. }
  221. @keyframes xhRote{
  222. 0%{
  223. transform: rotate(0deg);
  224. }
  225. 100%{
  226. transform: rotate(360deg);
  227. }
  228. }
  229. </style>