px-popup-bottom.vue 6.3 KB

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