当前位置:   article > 正文

记录uni-app横屏项目:自定义弹出框

记录uni-app横屏项目:自定义弹出框

目录

前言:

正文:


前言:横屏的尺寸问题

        最近使用了uniapp写了一个横屏的微信小程序和H5的项目,也是本人首次写的横屏项目,多少是有点踩坑+不太适应。。。

先说最让我一脸懵的点,尺寸大小,下面一段代码,设置文字的大小伟24rpx;横屏,竖屏下的效果如图

<view class="text" style="font-size: 24rpx">Jay丶萧邦</view>

可以很直观的看出来,横竖屏之间的尺寸差异是蛮大的,大概相差2倍的样子,所以要是业务设计要求可以旋转屏幕的话,得做适配工作,这里就不再多说;

正文:直接附上源码,不多说

言归正传,因为我看UI库好像都不太满足横屏的项目,所以有很多的东西都需要自己手撕一个,弹出框就是其一,先看效果哈:

uniapp横屏弹出框

如果觉得还比较符合您的需求,拿来整改整改即可,直接上代码:

  1. <template>
  2. <view class="dialog-overlay" v-if="visible" :style="{ zIndex: zIndex }" @tap="closeMask">
  3. <view class="dialog" v-if="dialogVisible" :style="[getStyle]" :class="[showAnimate ? 'bounce-enter-active' : 'bounce-leave-active']" @tap.stop>
  4. <view class="close" v-if="showClose" @tap="close">
  5. <view class="iconfont icon-guanbi"></view>
  6. </view>
  7. <slot></slot>
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. export default {
  13. name: 'CustomDialog',
  14. props: {
  15. visible: {
  16. type: Boolean,
  17. default: false
  18. },
  19. width: {
  20. type: String,
  21. default: 'auto'
  22. },
  23. height: {
  24. type: String,
  25. default: 'auto'
  26. },
  27. radius: {
  28. type: String,
  29. default: '16rpx'
  30. },
  31. bgColor: {
  32. type: String,
  33. default: '#fff'
  34. },
  35. customStyle: {
  36. type: Object,
  37. default: () => ({})
  38. },
  39. /* 是否展示右上角关闭按钮 */
  40. showClose: {
  41. type: Boolean,
  42. default: true
  43. },
  44. /* 是否点击遮罩层可以关闭弹出框 */
  45. maskCloseAble: {
  46. type: Boolean,
  47. default: true
  48. },
  49. /* 弹出框层级 */
  50. zIndex: {
  51. type: Number,
  52. default: 999
  53. }
  54. },
  55. data() {
  56. return {
  57. dialogVisible: this.visible,
  58. showAnimate: this.visible,
  59. timer: null
  60. };
  61. },
  62. beforeDestroy() {
  63. this.clearTimeout();
  64. },
  65. watch: {
  66. visible: {
  67. handler(val) {
  68. setTimeout(() => {
  69. this.dialogVisible = val;
  70. this.showAnimate = val;
  71. }, 50);
  72. },
  73. immediate: true
  74. }
  75. },
  76. computed: {
  77. getStyle() {
  78. return {
  79. width: this.width,
  80. height: this.height,
  81. background: this.bgColor,
  82. borderRadius: this.radius,
  83. ...this.customStyle
  84. };
  85. }
  86. },
  87. methods: {
  88. clearTimeout() {
  89. if (this.timer) {
  90. clearTimeout(this.timer);
  91. this.timer = null;
  92. }
  93. },
  94. closeMask() {
  95. if (!this.maskCloseAble) return;
  96. this.close();
  97. },
  98. close() {
  99. this.closeAnimate();
  100. this.timer = setTimeout(() => {
  101. this.$emit('close');
  102. this.$emit('update:visible', false);
  103. }, 500);
  104. },
  105. closeAnimate() {
  106. this.showAnimate = false;
  107. this.clearTimeout();
  108. }
  109. }
  110. };
  111. </script>
  112. <style lang="scss" scoped>
  113. .dialog-overlay {
  114. position: fixed;
  115. top: 0;
  116. left: 0;
  117. right: 0;
  118. bottom: 0;
  119. display: flex;
  120. align-items: center;
  121. justify-content: center;
  122. background-color: rgba(#000, 0.3);
  123. }
  124. .dialog {
  125. position: relative;
  126. border-radius: 16px;
  127. padding: 20rpx;
  128. padding-bottom: 14rpx;
  129. margin-left: -50rpx;
  130. opacity: 0;
  131. .close {
  132. position: absolute;
  133. width: 28rpx;
  134. height: 28rpx;
  135. border-radius: 50%;
  136. background-color: rgba(#000, 0.6);
  137. top: -10rpx;
  138. right: -10rpx;
  139. .icon {
  140. width: 10rpx;
  141. height: 10rpx;
  142. }
  143. }
  144. }
  145. /* 打开与关闭的类名 */
  146. .bounce-enter-active {
  147. animation: bounceIn 0.5s both;
  148. }
  149. .bounce-leave-active {
  150. animation: bounceOut 0.5s both;
  151. }
  152. /* 定义bounceIn动画 */
  153. @keyframes bounceIn {
  154. 0% {
  155. opacity: 0;
  156. transform: scale(0);
  157. }
  158. 50% {
  159. opacity: 1;
  160. transform: scale(1.2);
  161. }
  162. 70% {
  163. opacity: 1;
  164. transform: scale(0.9);
  165. }
  166. 100% {
  167. opacity: 1;
  168. transform: scale(1);
  169. }
  170. }
  171. /* 定义 bounceOut 动画 */
  172. @keyframes bounceOut {
  173. 0% {
  174. opacity: 1;
  175. transform: scale(1);
  176. }
  177. 25% {
  178. opacity: 1;
  179. transform: scale(0.95);
  180. }
  181. 50% {
  182. opacity: 0;
  183. transform: scale(1.1);
  184. }
  185. 100% {
  186. opacity: 0;
  187. transform: scale(0);
  188. }
  189. }
  190. .icon-guanbi {
  191. color: #94ffd8;
  192. font-size: 16rpx;
  193. }
  194. </style>

使用:

  1. <template>
  2. <view class="index">
  3. <button @click="visible = true">click</button>
  4. <custom-dialog :visible.sync="visible" width="500rpx" height="240rpx" @close="close">
  5. <view class="content">hello,hello</view>
  6. </custom-dialog>
  7. </view>
  8. </template>
  9. <script>
  10. import CustomDialog from '@/components/CustomDialog/index.vue';
  11. export default {
  12. components: {
  13. CustomDialog
  14. },
  15. data() {
  16. return {
  17. visible: false
  18. };
  19. },
  20. methods: {
  21. close() {
  22. console.log('我可以做点什么');
  23. }
  24. }
  25. };
  26. </script>
  27. <style lang="scss" scoped>
  28. .index {
  29. width: 100vw;
  30. height: 100vh;
  31. display: flex;
  32. align-items: center;
  33. justify-content: center;
  34. }
  35. </style>

 若是想根据内容大小来撑开宽度高度的话,那就不用设置width 和 height;

喜欢的可以用了!

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/861071
推荐阅读
相关标签
  

闽ICP备14008679号