• uniapp踩坑之项目:uniapp数字键盘组件—APP端


    //在components文件夹创建digitKeyboard文件夹,再创建digitKeyboard.vue

    1. <template>
    2. <view class="digit-keyboard">
    3. <view class="digit-keyboard_bg" @tap="hide">view>
    4. <view class="digit-keyboard_area">
    5. <view class="number-area">
    6. <view class="item" @tap="modifyNum(1)">1view>
    7. <view class="item" @tap="modifyNum(2)">2view>
    8. <view class="item" @tap="modifyNum(3)">3view>
    9. <view class="item" @tap="modifyNum('del')">
    10. <icon type="cancel" size="20" />
    11. view>
    12. <view class="item" @tap="modifyNum(4)">4view>
    13. <view class="item" @tap="modifyNum(5)">5view>
    14. <view class="item" @tap="modifyNum(6)">6view>
    15. <view class="item" @tap="modifyNum('add')">加1view>
    16. <view class="item" @tap="modifyNum(7)">7view>
    17. <view class="item" @tap="modifyNum(8)">8view>
    18. <view class="item" @tap="modifyNum(9)">9view>
    19. <view class="item" style="background-color:#2278FA;color: #ffffff;" @tap="confirm">确定view>
    20. <view class="item" @tap="modifyNum('-')">-view>
    21. <view class="item" @tap="modifyNum(0)">0view>
    22. <view class="item" @tap="modifyNum('.')">.view>
    23. <view class="item" @tap="modifyNum('clear')">清除view>
    24. view>
    25. view>
    26. view>
    27. template>
    28. <script>
    29. import NP from '../../utils/numberPrecision'
    30. export default {
    31. props: {
    32. inputVal: {
    33. type: [String],
    34. default: ''
    35. },
    36. label: {
    37. type: String,
    38. default: '现金'
    39. },
    40. },
    41. data() {
    42. return {
    43. val: ''
    44. };
    45. },
    46. created() {
    47. },
    48. methods: {
    49. input() {
    50. // this.$emit('cancel');
    51. let val = this.val;
    52. console.log(val);
    53. this.$emit('inputFocus', val);
    54. },
    55. //隐藏
    56. hide() {
    57. this.$emit('cancel');
    58. },
    59. confirm() {
    60. let val = this.val;
    61. let valNew = val.slice(-1);
    62. if (valNew == '.') {
    63. val = val.slice(0, -1);
    64. }
    65. this.$emit('confirm', val);
    66. },
    67. modifyNum(sign) {
    68. let {
    69. val
    70. } = this;
    71. //删除
    72. if (sign == 'del') {
    73. if (val.length > 0) {
    74. let valNew = val.slice(0, -1);
    75. if (valNew.length == 0) {
    76. val = '';
    77. } else {
    78. val = valNew;
    79. }
    80. }
    81. } else if (sign == 'add') { //加1
    82. val = NP.plus(Number(val), 1) + '';
    83. } else if (sign == 'minus') { //减1
    84. val = NP.minus(Number(val), 1) + '';
    85. } else if (sign == 'clear') { //清除
    86. val = '';
    87. } else if (sign == '-') { //代表负数
    88. if (val.indexOf('-') == -1) {
    89. val = '-' + val;
    90. }
    91. } else if (sign == '.') { //点符号
    92. if (val.indexOf('.') == -1 && val.length > 0) {
    93. val = val + '.';
    94. }
    95. } else {
    96. if ((val == '0' && sign == '0') || (val == '-0' && sign == '0') || (val == '0' && sign != '.') || (val == '-0' && sign != '.')) {
    97. return;
    98. }
    99. val = val + sign;
    100. }
    101. //小数点大于3位不赋值
    102. let arr = val.split('.');
    103. if (arr.length == 2 && arr[1].length > 3) {
    104. return;
    105. }
    106. this.$emit('inputFocus', val);
    107. this.val = val;
    108. }
    109. }
    110. }
    111. script>
    112. <style lang="scss" scoped>
    113. .digit-keyboard {
    114. position: fixed;
    115. left: 0;
    116. top: 0;
    117. width: 100%;
    118. height: 100%;
    119. z-index: 999;
    120. }
    121. .digit-keyboard_bg {
    122. width: 100%;
    123. height: 100%;
    124. background: rgba($color: #000000, $alpha: 0.5);
    125. }
    126. .digit-keyboard_area {
    127. position: absolute;
    128. bottom: 0;
    129. left: 0;
    130. width: 100%;
    131. background: #efefef;
    132. padding-bottom: 20upx;
    133. }
    134. .input-area {
    135. display: flex;
    136. align-items: center;
    137. padding: 10upx;
    138. background: #ffffff;
    139. .item {
    140. font-size: 28upx;
    141. &:nth-of-type(2) {
    142. flex: 1 0 auto;
    143. padding-right: 10upx;
    144. }
    145. &:nth-of-type(3) {
    146. font-size: 0;
    147. }
    148. }
    149. .input {
    150. background: #eeeeee;
    151. text-indent: 10upx;
    152. font-size: 28upx;
    153. height: 60upx;
    154. }
    155. }
    156. .number-area {
    157. display: flex;
    158. justify-content: space-around;
    159. flex-wrap: wrap;
    160. text-align: center;
    161. .item {
    162. margin-top: 20upx;
    163. flex: 0 0 22%;
    164. background: #ffffff;
    165. line-height: 80upx;
    166. font-size: 30upx;
    167. font-weight: bold;
    168. }
    169. }
    170. style>

    //main.js全局引入

    // 数字键盘组件 在项目里main.js中配置如下代码

    1. import digitKeyboard from './components/digitKeyboard/digitKeyboard.vue'
    2. Vue.component('digitKeyboard', digitKeyboard)

     //单页面使用

    1. //html
    2. class="content-input"
    3. @click="clickInput"
    4. @input="input"
    5. v-model="inputValue" />
    6. <view>
    7. <digitKeyboard
    8. v-if="isShowKeyboardWindow"
    9. @inputFocus="inputKeyboard"
    10. :inputVal="inputVal"
    11. :label="label"
    12. @cancel="isShowKeyboardWindow = false"
    13. @confirm="keyboardConfirm" />
    14. view>
    15. //data
    16. // 数字键盘
    17. inputVal: '',
    18. label: '现金支付',
    19. isShowKeyboardWindow: false,//是否显示键盘窗口
    20. //js
    21. // 数字键盘
    22. inputKeyboard(e) {
    23. // console.log(e, '00000000000')
    24. this.inputValue = e //输入框双向绑定
    25. if (e) {
    26. this.isChecked1 = false
    27. this.isChecked2 = false
    28. this.isChecked3 = false
    29. this.isChecked4 = false
    30. this.isChecked5 = false
    31. this.isChecked6 = false
    32. }
    33. },
    34. keyboardConfirm(val) {
    35. console.log(val)
    36. this.inputValue = val
    37. this.isShowKeyboardWindow = false
    38. this.isChecked1 = false;
    39. this.isChecked2 = false;
    40. this.isChecked3 = false;
    41. this.isChecked4 = false;
    42. this.isChecked5 = false;
    43. this.isChecked6 = false;
    44. },
    45. // 输入框点击事件
    46. clickInput() {
    47. this.isShowKeyboardWindow = true // 数字键盘组件显示
    48. },
    49. //充值按钮
    50. recharge: function (e) {
    51. // 进行判断
    52. if (this.inputValue !== '' || null || undefined) {// 为数字
    53. // 可调用支付接口
    54. // #ifdef APP-PLUS
    55. if (this.spaceCheck) {
    56. this.commitDialog()
    57. } else {
    58. // this.cancelDialog()
    59. }
    60. this.$refs.popupRef.show();
    61. // #endif
    62. }
    63. // 进行判断
    64. if (
    65. (this.inputValue == '' || null || undefined)
    66. && this.current_tag == '' &&
    67. this.isChecked1 == false &&
    68. this.isChecked2 == false &&
    69. this.isChecked3 == false &&
    70. this.isChecked4 == false &&
    71. this.isChecked5 == false &&
    72. this.isChecked6 == false
    73. ) { // 为空
    74. // console.log(33333333);
    75. uni.showToast({
    76. title: '请选择数值或输入内容!',
    77. duration: 2000,
    78. icon: 'none',
    79. });
    80. return false
    81. }
    82. else if (
    83. (this.inputValue == '' || null || undefined) &&
    84. this.current_tag &&
    85. this.isChecked1 == false &&
    86. this.isChecked2 == false &&
    87. this.isChecked3 == false &&
    88. this.isChecked4 == false &&
    89. this.isChecked5 == false &&
    90. this.isChecked6 == false
    91. ) {
    92. uni.showToast({
    93. title: '请选择数值或输入内容!',
    94. duration: 2000,
    95. icon: 'none',
    96. });
    97. return false
    98. }
    99. if (this.current_tag || this.isChecked2 == true) {// 进行判断
    100. // 可调用支付接口
    101. // #ifdef APP-PLUS
    102. if (this.spaceCheck) {
    103. this.commitDialog()
    104. } else {
    105. // this.cancelDialog()
    106. }
    107. this.$refs.popupRef.show();
    108. // #endif
    109. }
    110. }

    上一篇文章, 

    vue2踩坑之项目:Swiper轮播图使用_意初的博客-CSDN博客文章浏览阅读456次。首先安装swiper插件,解决方法:npm 版本太高,切换一下就好了,引入Swiper,mounted里面调用https://blog.csdn.net/weixin_43928112/article/details/133681437

  • 相关阅读:
    【Unity】编辑器扩展-03-拓展Inspector视图
    LabVIEW和Arduino的巧妙结合(基础篇—1)
    FireFox火狐浏览器电脑端安装到D盘
    Tomcat 集群介绍
    【Python入门】第二章: Python环境
    【推荐系统】行列式点过程(DPP)算法推导
    如何基于Angular从.ts获取鼠标响应的屏幕坐标,并传递至.html的Style中
    TensorRT详细入门指南
    HSQL总结
    应用启动加速-并发初始化spring bean
  • 原文地址:https://blog.csdn.net/weixin_43928112/article/details/134204799