px-popup-bottom.vue 6.3 KB

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