cm-number-box.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <template>
  2. <view class="number-box">
  3. <view class="sub iconfont icon-jianhao" @click="sub"></view>
  4. <input class="number" type="number" v-model="count" @blur="blur" />
  5. <view class="add iconfont icon-jiahao" @click="add"></view>
  6. </view>
  7. </template>
  8. <script>
  9. export default {
  10. name: 'number-box',
  11. data() {
  12. return {
  13. count: 1
  14. }
  15. },
  16. model: {
  17. prop: 'value',
  18. event: 'input'
  19. },
  20. props: {
  21. value: {
  22. type: Number,
  23. default: 0
  24. },
  25. max: {
  26. type: Number,
  27. default: 99999999
  28. },
  29. min: {
  30. type: Number,
  31. default: 1
  32. }
  33. },
  34. watch: {
  35. count(nVal){
  36. this.count = parseInt(nVal)
  37. }
  38. },
  39. created() {
  40. this.count = this.value
  41. },
  42. methods: {
  43. add() {
  44. if (this.count === this.max) {
  45. this.$util.msg(`购买数量不能大于${this.max}`, 2000)
  46. return
  47. }
  48. this.count++
  49. this.$emit('input', this.count)
  50. },
  51. sub() {
  52. if (this.count === this.min) {
  53. this.$util.msg(`购买数量不能少于${this.min}`, 2000)
  54. return
  55. }
  56. this.count--
  57. this.$emit('input', this.count)
  58. },
  59. blur() {
  60. if (this.count > this.max) {
  61. this.count = this.max
  62. this.$util.msg(`购买数量不能大于${this.max}`, 2000)
  63. }
  64. if (this.count < this.min) {
  65. this.count = this.min
  66. this.$util.msg(`购买数量不能少于${this.min}`, 2000)
  67. }
  68. this.$emit('input', this.count)
  69. }
  70. }
  71. }
  72. </script>
  73. <style lang="scss" scoped>
  74. .number-box {
  75. width: 148rpx;
  76. height: 48rpx;
  77. box-sizing: border-box;
  78. display: flex;
  79. justify-content: space-between;
  80. align-items: center;
  81. border-radius: 24rpx;
  82. border: 1rpx solid #e1e1e1;
  83. .sub,
  84. .add {
  85. flex: 1;
  86. text-align: center;
  87. font-size: 28rpx;
  88. color: #ccc;
  89. }
  90. .number {
  91. text-align: center;
  92. width: 56rpx;
  93. font-size: 26rpx;
  94. font-weight: 500;
  95. color: #333333;
  96. box-sizing: border-box;
  97. border-right: 1rpx solid #e1e1e1;
  98. border-left: 1rpx solid #e1e1e1;
  99. }
  100. }
  101. </style>