经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » Vue.js » 查看文章
vue实现内容可滚动的弹窗效果
来源:jb51  时间:2021/9/6 10:18:12  对本文有异议

本文实例为大家分享了vue实现内容可滚动的弹窗效果具体代码,供大家参考,具体内容如下

这是第一种实现方式

效果图:

弹窗代码:

Popup.vue

  1. <template lang="html">
  2. <div v-if="show" class="modal-bg" @click="closeModal">
  3. <div class="modal_con">
  4. <div class="modal_content">
  5. <div class="modal-container">
  6. <div class="modal_main">
  7. <p class="modal-header">{{dataList.title}}</p>
  8. <div class="rules_text">
  9. <p
  10. v-for="(item, index) in dataList.rules"
  11. class="rules_txt"
  12. :key="index"
  13. >
  14. {{ item }}
  15. </p>
  16. </div>
  17. </div>
  18. </div>
  19. <div class="footer_rules">
  20. <div class="tips"></div>
  21. <div class="rules_button">
  22. <div class="button" @click="closeModal">
  23. <p class="button_text">我知道了</p>
  24. </div>
  25. </div>
  26. </div>
  27. </div>
  28. </div>
  29.  
  30. </div>
  31. </template>
  32.  
  33. <script>
  34. export default {
  35. name: 'Popup',
  36. props: {
  37. show: {
  38. type: Boolean,
  39. default: false
  40. },
  41. },
  42. data () {
  43. return {
  44. dataList: {
  45. rules: [
  46. '1.这是第一条数据啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊',
  47. '2.这是第二条数据啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊',
  48. '3.这是第三条数据啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊',
  49. '4.这是第四条数据啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊'
  50. ],
  51. reward: [
  52. '1.活动规则第一条数据数据数据数据',
  53. '2.活动规则第二条数据数据数据',
  54. '2.活动规则第三条数据数据数据'
  55. ]
  56.  
  57. }
  58. }
  59. },
  60. methods: {
  61. closeModal () {
  62. this.$emit('closeModal')
  63. },
  64. }
  65. }
  66. </script>
  67.  
  68. <style lang="css" scoped>
  69. .modal_con {
  70. width: 80%;
  71. height: 287px;
  72. background: #ffffff;
  73. overflow: hidden;
  74. margin: 0 auto;
  75.  
  76. position: fixed;
  77. top: 50%;
  78. left: 50%;
  79. transform: translate(-50%, -50%);
  80. border-radius: 8px;
  81. }
  82. .modal_content {
  83. height: 100%;
  84. background-color: #fff;
  85. }
  86. .modal-bg {
  87. width: 100%;
  88. height: 100%;
  89. background-color: rgba(0, 0, 0, 0.6);
  90. position: fixed;
  91. top: 0;
  92. left: 0;
  93. z-index: 999;
  94. }
  95. .modal-container {
  96. height: 78%;
  97. text-align: center;
  98. display: flex;
  99. flex-direction: column;
  100. overflow-y: scroll;
  101. /* ios需要下面这个属性 */
  102. -webkit-overflow-scrolling: touch;
  103. }
  104.  
  105. .rules_txt {
  106. font-size: 15px;
  107. font-family: PingFangSC, PingFangSC-Regular;
  108. font-weight: 400;
  109. text-align: justify;
  110. color: #666666;
  111. padding: 0 20px;
  112. margin-top: 8px;
  113. margin-bottom: 0;
  114. }
  115.  
  116. .rules_txt:last-child {
  117. padding-bottom: 40px;
  118. }
  119. .modal-header {
  120. font-size: 17px;
  121. font-family: PingFang, PingFang-SC;
  122. font-weight: bold;
  123. text-align: center;
  124. color: #333333;
  125. margin: 0;
  126. padding-top: 20px;
  127. }
  128. .reward_title {
  129. font-size: 17px;
  130. font-family: PingFang, PingFang-SC;
  131. font-weight: bold;
  132. text-align: center;
  133. color: #333333;
  134. padding: 0;
  135. margin-top: 14px;
  136. margin-bottom: 6px;
  137. }
  138. .footer_rules {
  139. width: 100%;
  140. height: 22%;
  141. position: absolute;
  142. bottom: 0;
  143. }
  144. .tips {
  145. /* width: 100%;
  146. opacity: 0.6;
  147. height: 49px;
  148. background: linear-gradient(180deg, rgba(255, 255, 255, 0.5), #ffffff);
  149. text-align: center;
  150. line-height: 49px;
  151. font-size: 18px; */
  152. }
  153. .rules_button {
  154. width: 100%;
  155. background: #ffffff;
  156. padding-bottom: 20px;
  157. border-bottom-right-radius: 8px;
  158. border-bottom-left-radius: 8px;
  159. }
  160. .button {
  161. width: 90%;
  162. display: flex;
  163. justify-content: center;
  164. align-content: center;
  165. background: linear-gradient(270deg, #1283ff, #50a3ff);
  166. border-radius: 4px;
  167. text-align: center;
  168. margin: 0 auto;
  169. }
  170. .button_text {
  171. font-size: 15px;
  172. font-family: PingFang, PingFang-SC;
  173. font-weight: SC;
  174. color: #ffffff;
  175. display: flex;
  176. justify-content: center;
  177. align-content: center;
  178. margin: 0;
  179. padding: 12px 0;
  180. }
  181. .rules_con {
  182. padding-bottom: 80px;
  183. }
  184. </style>

在Home.vue页面使用弹窗:

  1. <!-- 活动规则弹窗 -->
  2. <template>
  3. <div>
  4. <div @click="clickPopup">
  5. <span>点击弹出弹窗</span>
  6. </div>
  7. <Popup
  8. v-show="isRulesShow"
  9. @closeModal="isRulesShow = false"
  10. :show="isRulesShow"
  11. >
  12. </Popup>
  13. </div>
  14. </template>
  15. <script>
  16. import Popup from './Popup'
  17. export default {
  18. name:"Home",
  19. components: {
  20. Popup
  21. },
  22. data () {
  23. return {
  24. isRulesShow:flase
  25. }
  26. },
  27. watch: {
  28. isRulesShow (v) {
  29. if (v) {
  30. //禁止主页面滑动方法在main.js
  31. this.noScroll()
  32. } else {
  33. //主页面可滑动
  34. this.canScroll()
  35. }
  36. },
  37. },
  38. methods:{
  39. //活动规则弹窗
  40. clickPopup () {
  41. this.isRulesShow = true
  42. },
  43. }
  44. }
  45. </script>
  46. <style lang="scss" scoped>
  47. * {
  48. touch-action: pan-y;
  49. }
  50. </style>

解决弹窗滚动,里面的body也跟着滚动问题

在main.js中

  1. //弹出框禁止滑动
  2. Vue.prototype.noScroll = function () {
  3. var mo = function (e) { e.preventDefault() }
  4. document.body.style.overflow = 'hidden'
  5. document.addEventListener('touchmove', mo, false,{ passive: false })// 禁止页面滑动
  6. }
  7. //弹出框可以滑动
  8. Vue.prototype.canScroll = function () {
  9. var mo = function (e) {
  10. e.preventDefault()
  11. }
  12. document.body.style.overflow = ''// 出现滚动条
  13. document.removeEventListener('touchmove', mo, false,{ passive: false })
  14. }

在页面使用时:

  1. //禁止主页面滑动
  2. this.noScroll()
  3. //主页面可滑动
  4. this.canScroll()
  5.  
  6. //还要加上样式:
  7. * {
  8. touch-action: pan-y;
  9. }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号