count2.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <template>
  2. <view class="countContainer">
  3. <view class="reduceBtn" @click="toReduce" />
  4. <input
  5. v-model="num"
  6. class="countNum"
  7. :class="Number(num) > 0 ? 'active' : ''"
  8. @focus="inputClear"
  9. @blur="inputCountNum"
  10. />
  11. <view class="plusBtn" @click="toPlus" />
  12. </view>
  13. </template>
  14. <script>
  15. export default {
  16. props: {
  17. countNum: {
  18. type: Number,
  19. default: 0
  20. },
  21. min: {
  22. type: Number,
  23. default: 0
  24. },
  25. max: {
  26. type: Number,
  27. default: 9999
  28. }
  29. },
  30. watch: {
  31. countNum(a) {
  32. this.num = a
  33. }
  34. },
  35. data() {
  36. return {
  37. num: this.countNum
  38. }
  39. },
  40. methods: {
  41. toReduce() {
  42. let _v = this.num
  43. if(_v <= this.min) return false
  44. this.$emit('update:countNum', --_v)
  45. },
  46. toPlus() {
  47. let _v = this.num
  48. if(_v > this.max) return false
  49. this.$emit('update:countNum', ++_v)
  50. },
  51. inputClear() {
  52. this.num = ''
  53. },
  54. inputCountNum(e) {
  55. if(e.target.value === '') {
  56. this.num = this.countNum
  57. return false
  58. }
  59. this.$emit('update:countNum', Number(e.target.value))
  60. }
  61. }
  62. }
  63. </script>
  64. <style lang="scss" scoped>
  65. .countContainer {
  66. flex: 1;
  67. overflow: hidden;
  68. height: 72rpx;
  69. border: 1px solid #F76454;
  70. box-sizing: border-box;
  71. border-radius: 16rpx;
  72. display: flex;
  73. align-items: center;
  74. justify-content: space-between;
  75. background-color: #FFF4F3;
  76. .reduceBtn, .plusBtn {
  77. width: 60rpx;
  78. height: 100%;
  79. display: flex;
  80. align-items: center;
  81. justify-content: center;
  82. position: relative;
  83. &::before {
  84. content: "";
  85. display: block;
  86. width: 18rpx;
  87. height: 2rpx;
  88. background-color: #F76454;
  89. }
  90. &.plusBtn {
  91. &::after {
  92. content: "";
  93. display: block;
  94. position: absolute;
  95. width: 2rpx;
  96. height: 18rpx;
  97. background-color: #F76454;
  98. top: 50%;
  99. left: 50%;
  100. transform: translate(-50%, -50%);
  101. }
  102. }
  103. }
  104. .countNum {
  105. flex: 1;
  106. overflow: hidden;
  107. border-left: 1px solid #F76454;
  108. border-right: 1px solid #F76454;
  109. text-align: center;
  110. line-height: 72rpx;
  111. height: 100%;
  112. &.active {
  113. color: #F76454;
  114. }
  115. }
  116. }
  117. </style>