uniapp 自定义弹窗组件

墨蓝 2022-10-30 10:25 764阅读 0赞

先上效果:
在这里插入图片描述
组件源码:slot-modal.vue

  1. <template>
  2. <view class="modal-container" v-if="show" @click.stop="cancel(2)">
  3. <view class="modal-content">
  4. <view class="modal-head modal-title-padding">
  5. <slot name="modal-head"></slot>
  6. </view>
  7. <view class="modal-body">
  8. <slot name="modal-body"></slot>
  9. </view>
  10. <view class="modal-footer">
  11. <view class="modal-col" hover-class="modal-hover" v-if="cancelText" @click.stop="cancel('cancel')">
  12. <text :style="cancelStyle" class="modal-row-text">{ { cancelText}}</text>
  13. </view>
  14. <view :style="confirmStyle" class="modal-col modal-confirm" hover-class="modal-hover" @click.stop="confirm">
  15. <text :style="confirmStyle" class="modal-row-text">{ { confirmText}}</text>
  16. </view>
  17. </view>
  18. </view>
  19. </view>
  20. </template>
  21. <script>
  22. export default {
  23. name: 'modal',
  24. props: {
  25. //默认是否显示
  26. show: {
  27. type: Boolean,
  28. default: true
  29. },
  30. //取消按钮文字
  31. cancelText: {
  32. type: String,
  33. default: ''
  34. },
  35. //取消样式
  36. cancelStyle: {
  37. type: [String, Object]
  38. },
  39. //确定按钮文字
  40. confirmText: {
  41. type: String,
  42. default: '确定'
  43. },
  44. //确定样式
  45. confirmStyle: {
  46. type: [String, Object]
  47. },
  48. //阻止点击对话框外侧锁屏
  49. disableScreenClick: {
  50. type: Boolean,
  51. default: false
  52. }
  53. },
  54. methods: {
  55. confirm() {
  56. this.$emit('confirm')
  57. },
  58. cancel(type) {
  59. if (!this.disableScreenClick || type === 'cancel') {
  60. this.$emit('cancel')
  61. }
  62. }
  63. }
  64. }
  65. </script>
  66. <style lang="scss" scoped>
  67. $fontSizeLg:17px;
  68. $fontSizeSm:15px;
  69. .modal-container {
  70. position: fixed;
  71. top: 0;
  72. left: 0;
  73. right: 0;
  74. bottom: 0;
  75. z-index: 999;
  76. background-color: rgba(0, 0, 0, .6);
  77. transition: all 5s;
  78. display: flex;
  79. align-items: center;
  80. justify-content: center;
  81. .modal-content {
  82. width: 80%;
  83. border-radius: 26rpx;
  84. background: #FFFFFF;
  85. overflow: hidden;
  86. animation: fadeZoom .15s linear;
  87. .modal-head {
  88. padding: 30rpx 30rpx 0;
  89. text-align: center;
  90. color: #000;
  91. font-size: $fontSizeLg;
  92. font-weight: 700;
  93. }
  94. .modal-title-padding {
  95. padding-bottom: 30rpx;
  96. }
  97. .modal-body {
  98. overflow:auto;
  99. padding: 40rpx 30rpx;
  100. font-size: $fontSizeSm;
  101. color: #000;
  102. text-align: center;
  103. }
  104. .modal-footer {
  105. display: flex;
  106. position: relative;
  107. text-align: center;
  108. font-size: $fontSizeLg;
  109. line-height: 100rpx;
  110. color: #007AFF;
  111. border-top: 0.5px solid rgba(9, 20, 31, 0.13);
  112. .modal-col {
  113. flex: 1;
  114. width: 100%;
  115. position: relative;
  116. }
  117. .modal-col:first-child::after {
  118. content: '';
  119. position: absolute;
  120. top: 0;
  121. bottom: 0;
  122. right: 0;
  123. border-right: 1px solid rgba(9, 20, 31, 0.13);
  124. transform: scaleX(.36);
  125. }
  126. .modal-confirm {
  127. color: rgb(0, 122, 255);
  128. }
  129. .modal-hover {
  130. background-color: #f2f2f2;
  131. }
  132. }
  133. .modal-footer::after {
  134. content: '';
  135. position: absolute;
  136. left: 0;
  137. right: 0;
  138. top: 0;
  139. border-top: 0.5px solid rgba(9, 20, 31, 0.13);
  140. transform: scaleY(.36);
  141. }
  142. }
  143. @keyframes fadeZoom {
  144. 0% {
  145. transform: scale(.7);
  146. opacity: .6;
  147. }
  148. 80% {
  149. transform: scale(1.2);
  150. opacity: .3;
  151. }
  152. 100% {
  153. transform: scale(1);
  154. opacity: 1;
  155. }
  156. }
  157. }
  158. </style>

使用示例:

  1. <template>
  2. <view class="content">
  3. <image class="logo" src="/static/logo.png"></image>
  4. <view class="text-area">
  5. <text class="title">{ { title}}</text>
  6. </view>
  7. <view><button type="default" @click="privacyDialogShow=true">用户协议</button></view>
  8. <slot-modal
  9. class="modal-privacy"
  10. :show="privacyDialogShow"
  11. :disableScreenClick="true"
  12. confirmText="同意"
  13. cancelText="不同意"
  14. @cancel="cancelPrivacy"
  15. @confirm="confirmPrivacy">
  16. <template slot="modal-head">
  17. <text>用户协议及隐私政策</text>
  18. </template>
  19. <template slot="modal-body">
  20. <view class="index-content">
  21. <text>
  22. 我们非常重视隐私和个人信息保护,请您先认真阅读
  23. <text class="privacyPolicy" @click.stop="goPage('agreement')">《用户服务协议》</text>
  24. <text class="privacyPolicy" @click.stop="goPage('privacy')">《隐私政策》</text>的全部条款,接受全部条款后再开始使用我们的服务。
  25. <text v-for="item in 40">我们非常重视隐私和个人信息保护,请您先认真阅读我们非常重视隐私和个人信息保护,请您先认真阅读我们非常重视隐私和个人信息保护,请您先认真阅读</text>
  26. </text>
  27. </view>
  28. </template>
  29. </slot-modal>
  30. </view>
  31. </template>
  32. <script>
  33. export default {
  34. data() {
  35. return {
  36. title: 'Hello',
  37. privacyDialogShow:false
  38. }
  39. },
  40. onLoad() {
  41. },
  42. methods: {
  43. goPage(pageUrl){
  44. console.log(pageUrl)
  45. uni.navigateTo({
  46. url:'../agreement/agreement'
  47. })
  48. },
  49. confirmPrivacy(){
  50. console.log('同意了用户协议')
  51. console.log(this.privacyDialogShow)
  52. this.privacyDialogShow = false
  53. console.log(this.privacyDialogShow)
  54. },
  55. cancelPrivacy(){
  56. console.log('拒绝了用户协议')
  57. this.privacyDialogShow=false
  58. }
  59. }
  60. }
  61. </script>
  62. <style>
  63. .content {
  64. display: flex;
  65. flex-direction: column;
  66. align-items: center;
  67. justify-content: center;
  68. }
  69. .logo {
  70. height: 200rpx;
  71. width: 200rpx;
  72. margin-top: 200rpx;
  73. margin-left: auto;
  74. margin-right: auto;
  75. margin-bottom: 50rpx;
  76. }
  77. .text-area {
  78. display: flex;
  79. justify-content: center;
  80. }
  81. .title {
  82. font-size: 36rpx;
  83. color: #8f8f94;
  84. }
  85. .index-content{
  86. max-height: 800rpx;
  87. }
  88. </style>

通过这次学习,遗留了一个问题还未解决:如何限制modal-body的高度为80%,尝试了多种方法无效,只能写固定高度了。
巩固了所学的vue的内容:

  • (1). 组件自定义事件
  • (2). 对话框的css布局

发表评论

表情:
评论列表 (有 0 条评论,764人围观)

还没有评论,来说两句吧...

相关阅读