123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- <template>
- <view class="number-box">
- <view class="sub iconfont icon-jianhao" @click="sub"></view>
- <input class="number" type="number" v-model="count" @blur="blur" />
- <view class="add iconfont icon-jiahao" @click="add"></view>
- </view>
- </template>
- <script>
- export default {
- name: 'number-box',
- data() {
- return {
- count: 1
- }
- },
- model: {
- prop: 'value',
- event: 'input'
- },
- props: {
- value: {
- type: Number,
- default: 0
- },
- max: {
- type: Number,
- default: 99999999
- },
- min: {
- type: Number,
- default: 1
- }
- },
- watch: {
- count(nVal){
- this.count = parseInt(nVal)
- }
- },
- created() {
- this.count = this.value
- },
- methods: {
- add() {
- if (this.count === this.max) {
- this.$util.msg(`购买数量不能大于${this.max}`, 2000)
- return
- }
- this.count++
- this.$emit('input', this.count)
- },
- sub() {
- if (this.count === this.min) {
- this.$util.msg(`购买数量不能少于${this.min}`, 2000)
- return
- }
- this.count--
- this.$emit('input', this.count)
- },
- blur() {
- if (this.count > this.max) {
- this.count = this.max
- this.$util.msg(`购买数量不能大于${this.max}`, 2000)
- }
- if (this.count < this.min) {
- this.count = this.min
- this.$util.msg(`购买数量不能少于${this.min}`, 2000)
- }
- this.$emit('input', this.count)
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- .number-box {
- width: 148rpx;
- height: 48rpx;
- box-sizing: border-box;
- display: flex;
- justify-content: space-between;
- align-items: center;
- border-radius: 24rpx;
- border: 1rpx solid #e1e1e1;
- .sub,
- .add {
- flex: 1;
- text-align: center;
- font-size: 28rpx;
- color: #ccc;
- }
- .number {
- text-align: center;
- width: 56rpx;
- font-size: 26rpx;
- font-weight: 500;
- color: #333333;
- box-sizing: border-box;
- border-right: 1rpx solid #e1e1e1;
- border-left: 1rpx solid #e1e1e1;
- }
- }
- </style>
|