弹出层设计

移动端-自定义弹出层-禁用滑动 - 图1Vue代码组织如下:

  1. <template>
  2. <div class="modal-container" v-if="show" @click="hideModal">
  3. <div class="modal-view">
  4. <div v-if="pure">
  5. <span :class="iconClass"></span>
  6. <span class="title global-short-text">{{title}}</span>
  7. <div class="content global-short-text">{{content}}</div>
  8. </div>
  9. <slot></slot>
  10. </div>
  11. <div class="mask"></div>
  12. </div>
  13. </template>
  14. <script>
  15. import { eventBus } from '../utils';
  16. let htmlDom, bodyDom;
  17. export default {
  18. data() {
  19. return {
  20. show: false,
  21. title: "title",
  22. content: "content",
  23. pure: true,
  24. success: true
  25. }
  26. },
  27. mounted() {
  28. eventBus.$on("showModal", this.showModal);
  29. eventBus.$on("hideModal", this.hideModal);
  30. htmlDom = document.getElementsByTagName("html")[0];
  31. bodyDom = document.getElementsByTagName("body")[0];
  32. },
  33. beforeDestroy() {
  34. htmlDom = null;
  35. bodyDom = null;
  36. },
  37. computed: {
  38. iconClass: function (params) {
  39. return this.success ? 'iconfont iconsuccess modal-icon-success' : 'iconfont iconfail modal-icon-fail'
  40. }
  41. },
  42. methods: {
  43. showModal: function ({ title, content, pure = true, success = true }) {
  44. this.show = true;
  45. this.pure = pure;
  46. this.title = title;
  47. this.content = content;
  48. this.success = success;
  49. window.scrollTo(0, 0);
  50. htmlDom.style.overflow = "hidden";
  51. bodyDom.style.overflow = "hidden";
  52. htmlDom.style.position = "fixed";
  53. bodyDom.style.position = "fixed";
  54. eventBus.$emit("banIframeScroll", "no"); //弹框出现的同时禁止滑动
  55. },
  56. hideModal: function () {
  57. this.show = false;
  58. htmlDom.style.overflowX = "hidden";
  59. htmlDom.style.overflowY = "scroll";
  60. bodyDom.style.overflowX = "hidden";
  61. bodyDom.style.overflowY = "scroll";
  62. htmlDom.style.position = "";
  63. bodyDom.style.position = "";
  64. eventBus.$emit("banIframeScroll", "auto"); //弹框出现的同时禁止滑动
  65. },
  66. noscroll: function (e) {
  67. e.preventDefault();
  68. }
  69. }
  70. }
  71. </script>
  72. <style>
  73. .modal-container {
  74. z-index: 999999;
  75. position: absolute;
  76. width: 100%;
  77. height: 100%;
  78. display: flex;
  79. justify-content: center;
  80. align-items: center;
  81. overflow: hidden;
  82. }
  83. .mask {
  84. position: absolute;
  85. width: 100%;
  86. height: 100%;
  87. background-color: black;
  88. opacity: 0.8;
  89. overflow: hidden;
  90. }
  91. .modal-view {
  92. z-index: 999999;
  93. width: 80%;
  94. min-height: 200px;
  95. background-color: white;
  96. text-align: center;
  97. position: absolute;
  98. top: 25%;
  99. border-radius: 5px;
  100. padding: 10px;
  101. opacity: 1;
  102. }
  103. .title {
  104. font-size: 28px;
  105. }
  106. .content {
  107. font-size: 24px;
  108. position: relative;
  109. top: 40px;
  110. }
  111. .modal-icon-success {
  112. font-size: 30px;
  113. color: var(--global-color);
  114. }
  115. .modal-icon-fail {
  116. font-size: 30px;
  117. color: red;
  118. }
  119. </style>

基于事件触发弹出和隐藏,这样做的好处就是在整个DOM树上只要挂载一处,挂载一次即可。内不展示蓉蓉可以通过配置参数传递,也支持slot。

滑动问题

一开始,在谷歌浏览器上使用对body的样式修改overflow为”hidden”就可以实现弹出层下滑动禁用。但是在真机上测试的时候发现并没有用。
我怀疑是的overflow的问题,所以对也做了修改。这样android手机基本没有问题了,但是我们的基础页面内使用了