• Vue结合Jquery 实现网页端数字键盘


    • 效果图
    • js文件
    1. import $ from 'jquery'
    2. /**
    3. * 调用键盘
    4. * @param obj input
    5. * @param round 小数后几位 0不带小数
    6. */
    7. export function loadNumberKeyboard(obj, round) {
    8. if ($("#numberkeyboard").length == 0) {
    9. let numbtnhtml =
    10. '
      7
      8
      9
      '
      +
    11. '
      4
      5
      6
      '
      +
    12. '
      1
      2
      3
      '
      +
    13. '
      .
      0
      退格
      '
      ;
    14. $("body").append('
      ' + numbtnhtml + '
      '
      );
    15. $("#numberkeyboard").find(".numbtn").bind("mousedown", function () {
    16. $(this).addClass("numbtndown");
    17. });
    18. $("#numberkeyboard").find(".numbtn").bind("mouseup", function () {
    19. $(this).removeClass("numbtndown");
    20. });
    21. }
    22. let containerDiv = $("#numberkeyboard").parent();
    23. containerDiv.show();
    24. containerDiv.css("z-index", 9100);
    25. obj = $(obj);
    26. if (obj.attr("id")) {
    27. var objpadding = parseInt(obj.css("padding-top").replace("px", "")) + parseInt(obj.css("padding-bottom").replace("px", ""));
    28. if (obj.offset().left + 340 >= $(window).width()) {
    29. containerDiv.css("left", $(window).width() - 340);
    30. } else {
    31. containerDiv.css("left", obj.offset().left);
    32. }
    33. if (obj.offset().top + 291 + obj.height() + objpadding + 2 + 5 >= $(window).height() + $(window).scrollTop()) {
    34. containerDiv.css("top", obj.offset().top - 291 - 5);
    35. } else {
    36. containerDiv.css("top", obj.offset().top + obj.height() + objpadding + 2 + 5);
    37. }
    38. }
    39. // 点击键盘外隐藏键盘
    40. $('html,body')
    41. .click((e) => {
    42. let target = e.target;
    43. console.log(target)
    44. if (target.localName !== 'input' && target.className !== 'numbtn') {
    45. containerDiv.hide();
    46. $('.numberkeyboard').hide();
    47. }
    48. })
    49. $("#numberkeyboard").find(".numbtn").unbind("click");
    50. $("#numberkeyboard").find(".numbtn").bind("click", function (e) {
    51. // obj.focus();
    52. let key = $(this).attr("key");
    53. switch (key) {
    54. case "backspace":
    55. if (obj.val().length > 0) {
    56. obj.val(obj.val().substr(0, obj.val().length - 1));
    57. }
    58. break;
    59. case "sign":
    60. if (obj.val().length > 0) {
    61. if (obj.val().substr(0, 1) === "-") {
    62. obj.val(obj.val().substr(1, obj.val().length - 1));
    63. } else {
    64. obj.val("-" + obj.val());
    65. }
    66. }
    67. break;
    68. case "close":
    69. $("#numberkeyboard").find(".numbtn").unbind("click");
    70. containerDiv.hide();
    71. break;
    72. default:
    73. let newVar = obj.val() + key;
    74. if (round === 2) {
    75. newVar = newVar.match(/^\d{0,5}(?:\.\d{0,2})?/);
    76. } else if (round === 3) {
    77. newVar = newVar.match(/^\d{0,5}(?:\.\d{0,3})?/);
    78. } else {
    79. newVar = newVar.replace(/[^\d]/g, '')
    80. }
    81. obj.val(newVar);
    82. break;
    83. }
    84. // 模拟输入
    85. obj[0].dispatchEvent(new Event('input'));
    86. });
    87. }
    • css文件
    1. #numberkeyboard {
    2. background-color: rgb(17, 17, 17);
    3. height: 295px;
    4. margin: 0;
    5. padding: 2px;
    6. position: relative;
    7. width: 290px;
    8. border-radius: 5px;
    9. display: flex;
    10. justify-content: center;
    11. flex-wrap: wrap;
    12. }
    13. #numberkeyboard .numbtn {
    14. border-radius: 5px;
    15. height: 68px;
    16. width: 91px;
    17. background-color: rgba(91, 91, 91, 0.6);
    18. color: white;
    19. margin: 2px;
    20. line-height: 68px;
    21. text-align: center;
    22. font-weight: bold;
    23. font-size: 25px;
    24. }
    25. #numberkeyboard .numbtn:last-child{
    26. background-color: red;
    27. font-size: 20px;
    28. }
    29. #numberkeyboard .numbtn:active {
    30. background-color: #cccccc;
    31. color: black;
    32. }
    • 使用方式

      1.首先再main.js中引入上方的css文件
      2.vue代码中直接使用即可
      getKeyboardNum(id) {
        loadNumberKeyboard(`#${id}`,0)
      },

  • 相关阅读:
    【深度学习实战(25)】搭建训练框架之ModelEMA
    快速的比特反序操作
    【C++代码】二叉树的最大深度,二叉树的最小深度,完全二叉树的节点个数--代码随想录
    根据Word模板,使用POI生成文档
    LLM大语言模型(十二):关于ChatGLM3-6B不兼容Langchain 的Function Call
    【nginx】详细配置
    LeetCode 25. K 个一组翻转链表
    Google Earth Engine(GEE)——ui.util.debounce的使用
    tensorflow2.x --------------------DenseNet-----------------------------
    Redis分布式锁这样用,有坑?
  • 原文地址:https://blog.csdn.net/weixin_44912855/article/details/133136334