number-box.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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="number" @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. number: 0
  14. }
  15. },
  16. props: {
  17. value: {
  18. type: Number,
  19. default: 0
  20. },
  21. max: {
  22. type:Number,
  23. default: 99999999
  24. }
  25. },
  26. created() {
  27. this.number = this.value
  28. },
  29. watch:{
  30. value(val){
  31. if(val > this.max) {
  32. this.number = this.max
  33. }else{
  34. this.number = val
  35. }
  36. }
  37. },
  38. methods: {
  39. add() {
  40. if(this.number < this.max) {
  41. this.number++
  42. this.$emit('change', this.number)
  43. }else{
  44. this.$util.msg(`最多购买${this.max}件`, 2000)
  45. }
  46. },
  47. sub() {
  48. if (this.number > 1) {
  49. this.number--
  50. this.$emit('change', this.number)
  51. } else {
  52. this.$util.msg('购买数量不能少于1', 2000)
  53. }
  54. },
  55. blur() {
  56. if (this.number <= 0) {
  57. this.number = 1
  58. this.$util.msg('购买数量不能少于1', 2000)
  59. }
  60. this.number = parseInt(this.number)
  61. this.$emit('change', this.number)
  62. }
  63. }
  64. }
  65. </script>
  66. <style lang="scss" scoped>
  67. .number-box {
  68. width: 148rpx;
  69. height: 48rpx;
  70. box-sizing: border-box;
  71. display: flex;
  72. justify-content: space-between;
  73. align-items: center;
  74. border-radius: 24rpx;
  75. border: 1rpx solid #e1e1e1;
  76. .sub,
  77. .add {
  78. flex: 1;
  79. text-align: center;
  80. font-size: 28rpx;
  81. color: #ccc;
  82. }
  83. .number {
  84. text-align: center;
  85. width: 56rpx;
  86. font-size: 26rpx;
  87. font-weight: 500;
  88. color: #333333;
  89. box-sizing: border-box;
  90. border-right: 1rpx solid #e1e1e1;
  91. border-left: 1rpx solid #e1e1e1;
  92. }
  93. }
  94. </style>