px-popup-bottom.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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="{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" :style="{height:`${PopHeight}px`}">
  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. zindex: {
  58. type: [String, Number],
  59. default: 1000
  60. },
  61. maskZindex: {
  62. type: [String, Number],
  63. default: 999
  64. },
  65. always: { //是否每次打开都重新计算内容高度
  66. type: Boolean,
  67. default: false
  68. },
  69. },
  70. data() {
  71. return {
  72. show: false,
  73. height: 0,
  74. PopHeight: 0,
  75. cotMaxHeight: '',
  76. isAnimaStart: false,
  77. rpxRate: "",
  78. cotRadius: 0,
  79. closeIcon: iconClose
  80. }
  81. },
  82. watch: {
  83. visible(newval) {
  84. this.isAnimaStart = true;
  85. setTimeout(() => {
  86. this.isAnimaStart = false;
  87. }, this.animaTime * 1000)
  88. if (newval && this.height === 0) {
  89. if (this.PopHeight === 0 || this.always) {
  90. this.setContViewHeight();
  91. } else {
  92. this.height = this.PopHeight
  93. }
  94. // #ifdef H5
  95. this.setBodyOverFlow('hidden') //阻止滚动穿透
  96. //#endif
  97. this.$emit('open')
  98. } else {
  99. this.height = 0;
  100. // #ifdef H5
  101. this.setBodyOverFlow('visible')
  102. //#endif
  103. }
  104. this.show = newval
  105. },
  106. maxHeight: {
  107. handler(newval) {
  108. this.cotMaxHeight = this.unitCheck(newval);
  109. },
  110. immediate: true
  111. },
  112. radius: {
  113. handler(newval) {
  114. this.cotRadius = this.unitCheck(newval);
  115. },
  116. immediate: true
  117. }
  118. },
  119. created() {
  120. this.rpxRate = this.getRpxRate()
  121. },
  122. mounted() {
  123. this.$nextTick(() => {
  124. // #ifdef H5
  125. this.preventTouch(document.querySelector(
  126. '.scroll-wrap .uni-scroll-view .uni-scroll-view')); //防止浏览器报错
  127. //#endif
  128. })
  129. },
  130. methods: {
  131. onClose() {
  132. this.$emit("update:visible", false);
  133. this.$emit('close')
  134. },
  135. //触底
  136. onScrollToLower(e) {
  137. this.$emit("reachBottom");
  138. },
  139. getRpxRate() {
  140. let res = uni.getSystemInfoSync();
  141. let width = res.windowWidth;
  142. let rate = 750.00 / width;
  143. return rate
  144. },
  145. unitCheck(value) {
  146. const val = String(value);
  147. if (!val.includes('px') && !val.includes('%')) {
  148. return `${val}rpx`;
  149. }
  150. return val;
  151. },
  152. preventTouch(el) {
  153. el.addEventListener('touchmove', function(e) {
  154. e.stopPropagation();
  155. },{
  156. passive: false
  157. });
  158. },
  159. setBodyOverFlow(val) {
  160. document.body.style.overflow = val
  161. },
  162. //设置内容区域高度
  163. async setContViewHeight() {
  164. let data = await this.computeHeight();
  165. console.log("qxj setContViewHeight data:"+JSON.stringify(data));
  166. let temHeight=data.height + (this.title ? 100 / parseFloat(this.rpxRate) : 0);
  167. const systemInfo = uni.getSystemInfoSync();
  168. let viewHeight=systemInfo.screenHeight;
  169. this.height =viewHeight*0.7;
  170. console.log("qxj viewHeight:"+this.height);
  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: white;
  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: 15rpx 70rpx 0;
  227. padding-top: 30rpx;
  228. box-sizing: border-box;
  229. height: 80rpx;
  230. .title {
  231. font-size: 26rpx;
  232. font-family: PingFang SC;
  233. font-weight: normal !important;
  234. color: #111111;
  235. width: 100%;
  236. overflow: hidden;
  237. text-overflow: ellipsis;
  238. white-space: nowrap;
  239. }
  240. .close-wrap {
  241. position: absolute;
  242. top: 20rpx;
  243. right: 10rpx;
  244. padding: 10rpx 20rpx;
  245. box-sizing: border-box;
  246. }
  247. .close-icon {
  248. width: 40rpx;
  249. height: 40rpx;
  250. }
  251. }
  252. .scroll-wrap {
  253. flex: 1;
  254. height: 0;
  255. width: 100%;
  256. }
  257. &.visible {
  258. max-height: 75%;
  259. overflow-y: hidden;
  260. height: auto;
  261. }
  262. }
  263. }
  264. .scroll-view ::-webkit-scrollbar {
  265. display: none !important;
  266. width: 0 !important;
  267. height: 0 !important;
  268. -webkit-appearance: none;
  269. background: transparent;
  270. }
  271. .popup_content {
  272. width: 100%;
  273. padding: 30rpx;
  274. padding-top: 0;
  275. box-sizing: border-box;
  276. &::after {
  277. display: block;
  278. width: 100%;
  279. content: "\00A0";
  280. overflow: hidden;
  281. opacity: 0;
  282. height: 1rpx;
  283. }
  284. }
  285. </style>