vue switch组件

左手的ㄟ右手 2022-10-02 11:51 240阅读 0赞

组件

  1. <template>
  2. <view>
  3. <view class="weui-switch" :class="{'weui-switch-on' : me_checked}" :value="value" @click="toggle"></view>
  4. </view>
  5. </template>
  6. <script>
  7. export default {
  8. props: {
  9. value: {
  10. type: Boolean,
  11. default: true
  12. },
  13. event:'',
  14. index:0,
  15. item:{},
  16. actionurl:''
  17. },
  18. data() {
  19. return {
  20. me_checked: this.value,
  21. cc:this.value
  22. }
  23. },
  24. watch: {
  25. me_checked(val) {
  26. let status = val*1
  27. this.$emit(this.event, status,this.index);
  28. }
  29. },
  30. methods: {
  31. toggle(e) {
  32. this.me_checked = !this.me_checked;
  33. let status = this.me_checked
  34. let id = this.item.zw_id
  35. let g_status= status*1
  36. this.$api.request(this.actionurl,{id:id, status: g_status},'post').then((res)=>{
  37. uni.showToast({
  38. title:res.msg,
  39. icon:'none'
  40. })
  41. }).catch((e)=>{
  42. uni.showToast({
  43. title:e.msg,
  44. icon:'none'
  45. })
  46. let failstatus = false;
  47. if(status== false){
  48. failstatus=true
  49. }else if(status==true){
  50. failstatus=false
  51. }
  52. this.me_checked = failstatus
  53. })
  54. }
  55. }
  56. }
  57. </script>
  58. <style>
  59. .weui-switch {
  60. display: block;
  61. position: relative;
  62. width: 84upx;
  63. height: 42upx;
  64. border: 1px solid #DFDFDF;
  65. outline: 0;
  66. border-radius: 22px;
  67. box-sizing: border-box;
  68. background-color: #DFDFDF;
  69. transition: background-color 0.1s, border 0.1s;
  70. cursor: pointer;
  71. }
  72. .weui-switch:before {
  73. content: " ";
  74. position: absolute;
  75. top: 0;
  76. left: 0;
  77. width: 80upx;
  78. height: 38upx;
  79. border-radius: 20upx;
  80. background-color: #FDFDFD;
  81. transition: transform 0.35s cubic-bezier(0.45, 1, 0.4, 1);
  82. }
  83. .weui-switch:after {
  84. content: " ";
  85. position: absolute;
  86. top: 0;
  87. left: 0;
  88. width: 40upx;
  89. height: 38upx;
  90. border-radius: 20upx;
  91. background-color: #FFFFFF;
  92. box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
  93. transition: transform 0.35s cubic-bezier(0.4, 0.4, 0.25, 1.35);
  94. }
  95. .weui-switch-on {
  96. border-color: #1AAD19;
  97. background-color: #1AAD19;
  98. }
  99. .weui-switch-on:before {
  100. border-color: #1AAD19;
  101. background-color: #1AAD19;
  102. }
  103. .weui-switch-on:after {
  104. transform: translateX(40upx);
  105. }
  106. </style>

调用

  1. <myswitch v-model="item.status" actionurl='/wapbusiness/changestatus' :item="item" :index='index' event='ffrrr' v-on:ffrrr='statuschange' style=' margin-left: 10upx;transform:scale(0.7)'></myswitch>
  2. <script>
  3. import myswitch from '../../components/switch'
  4. statuschange:function(e,i){
  5. console.log(e)
  6. }
  7. </script>

发表评论

表情:
评论列表 (有 0 条评论,240人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Vue

    为什么使用组件 JavaScript能力的复用。Vue.js的组件提高重复性,让代码可以复用。 组件的用法 组件在使用前先需要注册。注册分为两种方式:全局注册和局

    相关 Vue

    Vue组件 1. 什么是组件 1. 组件的概念:组件即自定义控件,是Vue.js最强大的功能之一 2. 组件的用途:组件能够封装可重用代码,扩展html标签功能