px-popup-bottom.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <template>
  2. <view :class="['popup',{'popup-show':show}]" @mousewheel.prevent.stop @touchmove.stop.prevent
  3. :style="{'z-index':zindex}">
  4. <view class="mask" :style="{'z-index':maskZindex,bottom:bottom+'rpx'}" v-show="show" @click.stop="onClose"
  5. @touchmove.prevent.stop></view>
  6. <view :class="['content',{show}]" @click.stop @touchmove.prevent.stop :style="{'background-color':bgColor,height:`${height}px`,maxHeight:show ? cotMaxHeight:0,'border-top-right-radius':cotRadius,
  7. 'border-top-left-radius':cotRadius,transition: `all ${animaTime}s ease-in`,bottom:bottom+'rpx','z-index':zindex}">
  8. <view id="title-bar" class="title-bar" v-show="title">
  9. <view class="title" :style="{fontWeight:fontweight}">{{title}}</view>
  10. <view class="close-wrap" @click.stop="onClose">
  11. <image class="close-icon" :src="closeIcon" mode="widthFix"></image>
  12. </view>
  13. </view>
  14. <view class="scroll-wrap">
  15. <scroll-view :class="{'scroll-view':isAnimaStart}" scroll-y="true" style="height:100%;"
  16. @scrolltolower="onScrollToLower">
  17. <view id="popup_content" class="popup_content">
  18. <slot></slot>
  19. </view>
  20. </scroll-view>
  21. </view>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. import iconClose from '@/static/images/close40.png'
  27. export default {
  28. props: {
  29. title: { //标题
  30. type: String,
  31. default: ''
  32. },
  33. fontweight: {
  34. type: [String, Number],
  35. default: 'normal'
  36. },
  37. visible: { //隐藏显示标识
  38. type: Boolean,
  39. default: false
  40. },
  41. maxHeight: { //内容区域最大高度
  42. type: [String, Number],
  43. default: '75%'
  44. },
  45. radius: { //顶部圆角
  46. type: [String, Number],
  47. default: '0'
  48. },
  49. animaTime: { //弹窗动画时间
  50. type: Number,
  51. default: 0.2
  52. },
  53. bottom: { //离底部距离
  54. type: [String, Number],
  55. default: 0
  56. },
  57. bgColor: {
  58. type: [String],
  59. default: '#ffffff'
  60. },
  61. zindex: {
  62. type: [String, Number],
  63. default: 1000
  64. },
  65. maskZindex: {
  66. type: [String, Number],
  67. default: 999
  68. },
  69. always: { //是否每次打开都重新计算内容高度
  70. type: Boolean,
  71. default: false
  72. },
  73. },
  74. data() {
  75. return {
  76. show: false,
  77. height: 0,
  78. PopHeight: 0,
  79. cotMaxHeight: '',
  80. isAnimaStart: false,
  81. rpxRate: "",
  82. cotRadius: 0,
  83. closeIcon: iconClose
  84. }
  85. },
  86. watch: {
  87. visible(newval) {
  88. this.isAnimaStart = true;
  89. setTimeout(() => {
  90. this.isAnimaStart = false;
  91. }, this.animaTime * 1000)
  92. if (newval && this.height === 0) {
  93. if (this.PopHeight === 0 || this.always) {
  94. this.setContViewHeight();
  95. } else {
  96. this.height = this.PopHeight
  97. }
  98. // #ifdef H5
  99. this.setBodyOverFlow('hidden') //阻止滚动穿透
  100. //#endif
  101. this.$emit('open')
  102. } else {
  103. this.height = 0;
  104. // #ifdef H5
  105. this.setBodyOverFlow('visible')
  106. //#endif
  107. }
  108. this.show = newval
  109. },
  110. maxHeight: {
  111. handler(newval) {
  112. this.cotMaxHeight = this.unitCheck(newval);
  113. },
  114. immediate: true
  115. },
  116. radius: {
  117. handler(newval) {
  118. this.cotRadius = this.unitCheck(newval);
  119. },
  120. immediate: true
  121. }
  122. },
  123. created() {
  124. this.rpxRate = this.getRpxRate()
  125. },
  126. mounted() {
  127. this.$nextTick(() => {
  128. // #ifdef H5
  129. this.preventTouch(document.querySelector(
  130. '.scroll-wrap .uni-scroll-view .uni-scroll-view')); //防止浏览器报错
  131. //#endif
  132. })
  133. },
  134. methods: {
  135. onClose() {
  136. this.$emit("update:visible", false);
  137. this.$emit('close')
  138. },
  139. //触底
  140. onScrollToLower(e) {
  141. this.$emit("reachBottom");
  142. },
  143. getRpxRate() {
  144. let res = uni.getSystemInfoSync();
  145. let width = res.windowWidth;
  146. let rate = 750.00 / width;
  147. return rate
  148. },
  149. unitCheck(value) {
  150. const val = String(value);
  151. if (!val.includes('px') && !val.includes('%')) {
  152. return `${val}rpx`;
  153. }
  154. return val;
  155. },
  156. preventTouch(el) {
  157. el.addEventListener('touchmove', function(e) {
  158. e.stopPropagation();
  159. }, {
  160. passive: false
  161. });
  162. },
  163. setBodyOverFlow(val) {
  164. document.body.style.overflow = val
  165. },
  166. //设置内容区域高度
  167. async setContViewHeight() {
  168. let data = await this.computeHeight();
  169. this.height = data.height + (this.title ? 100 / parseFloat(this.rpxRate) : 0);
  170. this.PopHeight = this.height;
  171. },
  172. //计算内容区域高度
  173. computeHeight() {
  174. return new Promise(resolve => {
  175. this.$nextTick(() => {
  176. const query = uni.createSelectorQuery().in(this);
  177. query.select('#popup_content').boundingClientRect(data => {
  178. resolve(data)
  179. }).exec();
  180. })
  181. })
  182. }
  183. }
  184. }
  185. </script>
  186. <style lang="scss" scoped>
  187. .popup {
  188. &.popup-show {
  189. position: fixed;
  190. top: 0;
  191. right: 0;
  192. left: 0;
  193. bottom: 0;
  194. overflow: hidden;
  195. z-index: 999;
  196. }
  197. .mask {
  198. position: fixed;
  199. top: 0;
  200. right: 0;
  201. bottom: 0;
  202. left: 0;
  203. background-color: rgba($color: #000000, $alpha: 0.5);
  204. z-index: 999;
  205. }
  206. .content {
  207. position: fixed;
  208. bottom: 0;
  209. left: 0;
  210. right: 0;
  211. height: 0;
  212. height: auto;
  213. background-color: #ffffff;
  214. transition: all 0.2s ease-in;
  215. z-index: 1000;
  216. display: flex;
  217. flex-direction: column;
  218. align-items: center;
  219. overflow: hidden;
  220. .title-bar {
  221. width: 100%;
  222. flex-shrink: 0;
  223. text-align: center;
  224. position: relative;
  225. padding: 10rpx 70rpx 0;
  226. box-sizing: border-box;
  227. height: 80rpx;
  228. .title {
  229. font-size: 34upx;
  230. font-family: PingFang SC;
  231. font-weight: bold !important;
  232. color: #111111;
  233. width: 100%;
  234. overflow: hidden;
  235. text-overflow: ellipsis;
  236. white-space: nowrap;
  237. }
  238. .close-wrap {
  239. position: absolute;
  240. top: 20rpx;
  241. right: 10rpx;
  242. padding: 10rpx 20rpx;
  243. box-sizing: border-box;
  244. }
  245. .close-icon {
  246. width: 40rpx;
  247. height: 40rpx;
  248. }
  249. }
  250. .scroll-wrap {
  251. flex: 1;
  252. height: 0;
  253. width: 100%;
  254. }
  255. &.visible {
  256. max-height: 75%;
  257. overflow-y: hidden;
  258. height: auto;
  259. }
  260. }
  261. }
  262. .scroll-view ::-webkit-scrollbar {
  263. display: none !important;
  264. width: 0 !important;
  265. height: 0 !important;
  266. -webkit-appearance: none;
  267. background: transparent;
  268. }
  269. .popup_content {
  270. width: 100%;
  271. padding: 0rpx 30rpx;
  272. box-sizing: border-box;
  273. &::after {
  274. display: block;
  275. width: 100%;
  276. content: "\00A0";
  277. overflow: hidden;
  278. opacity: 0;
  279. height: 1rpx;
  280. }
  281. }
  282. </style>