Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. <template>
  2. <view v-if="visibleSync" :style="[customStyle, {
  3. zIndex: uZindex - 1
  4. }]" class="u-drawer" hover-stop-propagation>
  5. <u-mask :blur="blur" :duration="duration" :custom-style="maskCustomStyle" :maskClickAble="maskCloseAble" :z-index="uZindex - 2" :show="showDrawer && mask" @click="maskClick"></u-mask>
  6. <!-- 移除 @tap.stop.prevent -->
  7. <view
  8. class="u-drawer-content"
  9. @tap="modeCenterClose(mode)"
  10. :class="[
  11. safeAreaInsetBottom ? 'safe-area-inset-bottom' : '',
  12. 'u-drawer-' + mode,
  13. showDrawer ? 'u-drawer-content-visible' : '',
  14. zoom && mode == 'center' ? 'u-animation-zoom' : ''
  15. ]"
  16. @touchmove.stop.prevent
  17. :style="[style]"
  18. >
  19. <view class="u-mode-center-box" @tap.stop.prevent @touchmove.stop.prevent v-if="mode == 'center'" :style="[centerStyle]">
  20. <u-icon
  21. @click="close"
  22. v-if="closeable"
  23. class="u-close"
  24. :class="['u-close--' + closeIconPos]"
  25. :name="closeIcon"
  26. :color="closeIconColor"
  27. :size="closeIconSize"
  28. ></u-icon>
  29. <scroll-view class="u-drawer__scroll-view" scroll-y="true">
  30. <slot></slot>
  31. </scroll-view>
  32. </view>
  33. <scroll-view class="u-drawer__scroll-view" scroll-y="true" v-else>
  34. <slot></slot>
  35. </scroll-view>
  36. <view @tap="close" class="u-close" :class="['u-close--' + closeIconPos]">
  37. <u-icon
  38. v-if="mode != 'center' && closeable"
  39. :name="closeIcon"
  40. :color="closeIconColor"
  41. :size="closeIconSize"
  42. ></u-icon>
  43. </view>
  44. </view>
  45. </view>
  46. </template>
  47. <script>
  48. /**
  49. * popup 弹窗
  50. * @description 弹出层容器,用于展示弹窗、信息提示等内容,支持上、下、左、右和中部弹出。组件只提供容器,内部内容由用户自定义
  51. * @tutorial https://www.uviewui.com/components/popup.html
  52. * @property {String} mode 弹出方向(默认left)
  53. * @property {Boolean} mask 是否显示遮罩(默认true)
  54. * @property {Stringr | Number} length mode=left | 见官网说明(默认auto)
  55. * @property {Boolean} zoom 是否开启缩放动画,只在mode为center时有效(默认true)
  56. * @property {Boolean} safe-area-inset-bottom 是否开启底部安全区适配(默认false)
  57. * @property {Boolean} mask-close-able 点击遮罩是否可以关闭弹出层(默认true)
  58. * @property {Object} custom-style 用户自定义样式
  59. * @property {Stringr | Number} negative-top 中部弹出时,往上偏移的值
  60. * @property {Numberr | String} border-radius 弹窗圆角值(默认0)
  61. * @property {Numberr | String} z-index 弹出内容的z-index值(默认1075)
  62. * @property {Boolean} closeable 是否显示关闭图标(默认false)
  63. * @property {String} close-icon 关闭图标的名称,只能uView的内置图标
  64. * @property {String} close-icon-pos 自定义关闭图标位置(默认top-right)
  65. * @property {String} close-icon-color 关闭图标的颜色(默认#909399)
  66. * @property {Number | String} close-icon-size 关闭图标的大小,单位rpx(默认30)
  67. * @event {Function} open 弹出层打开
  68. * @event {Function} close 弹出层收起
  69. * @example <u-popup v-model="show"><view>出淤泥而不染,濯清涟而不妖</view></u-popup>
  70. */
  71. export default {
  72. name: 'u-popup',
  73. emits: ["update:modelValue", "input", "open", "close"],
  74. props: {
  75. value: {
  76. type: Boolean,
  77. default: false
  78. },
  79. modelValue: {
  80. type: Boolean,
  81. default: false
  82. },
  83. /**
  84. * 显示状态
  85. */
  86. show: {
  87. type: Boolean,
  88. default: false
  89. },
  90. /**
  91. * 弹出方向,left|right|top|bottom|center
  92. */
  93. mode: {
  94. type: String,
  95. default: 'left'
  96. },
  97. /**
  98. * 是否显示遮罩
  99. */
  100. mask: {
  101. type: Boolean,
  102. default: true
  103. },
  104. // 抽屉的宽度(mode=left|right),或者高度(mode=top|bottom),单位rpx,或者"auto"
  105. // 或者百分比"50%",表示由内容撑开高度或者宽度
  106. length: {
  107. type: [Number, String],
  108. default: 'auto'
  109. },
  110. // 是否开启缩放动画,只在mode=center时有效
  111. zoom: {
  112. type: Boolean,
  113. default: true
  114. },
  115. // 是否开启底部安全区适配,开启的话,会在iPhoneX机型底部添加一定的内边距
  116. safeAreaInsetBottom: {
  117. type: Boolean,
  118. default: false
  119. },
  120. // 是否可以通过点击遮罩进行关闭
  121. maskCloseAble: {
  122. type: Boolean,
  123. default: true
  124. },
  125. // 用户自定义样式
  126. customStyle: {
  127. type: Object,
  128. default() {
  129. return {};
  130. }
  131. },
  132. // 此为内部参数,不在文档对外使用,为了解决Picker和keyboard等融合了弹窗的组件
  133. // 对v-model双向绑定多层调用造成报错不能修改props值的问题
  134. popup: {
  135. type: Boolean,
  136. default: true
  137. },
  138. // 显示显示弹窗的圆角,单位rpx
  139. borderRadius: {
  140. type: [Number, String],
  141. default: 0
  142. },
  143. zIndex: {
  144. type: [Number, String],
  145. default: ''
  146. },
  147. // 是否显示关闭图标
  148. closeable: {
  149. type: Boolean,
  150. default: false
  151. },
  152. // 关闭图标的名称,只能uView的内置图标
  153. closeIcon: {
  154. type: String,
  155. default: 'close'
  156. },
  157. // 自定义关闭图标位置,top-left为左上角,top-right为右上角,bottom-left为左下角,bottom-right为右下角
  158. closeIconPos: {
  159. type: String,
  160. default: 'top-right'
  161. },
  162. // 关闭图标的颜色
  163. closeIconColor: {
  164. type: String,
  165. default: '#909399'
  166. },
  167. // 关闭图标的大小,单位rpx
  168. closeIconSize: {
  169. type: [String, Number],
  170. default: '30'
  171. },
  172. // 宽度,只对左,右,中部弹出时起作用,单位rpx,或者"auto"
  173. // 或者百分比"50%",表示由内容撑开高度或者宽度,优先级高于length参数
  174. width: {
  175. type: String,
  176. default: ''
  177. },
  178. // 高度,只对上,下,中部弹出时起作用,单位rpx,或者"auto"
  179. // 或者百分比"50%",表示由内容撑开高度或者宽度,优先级高于length参数
  180. height: {
  181. type: String,
  182. default: ''
  183. },
  184. // 给一个负的margin-top,往上偏移,避免和键盘重合的情况,仅在mode=center时有效
  185. negativeTop: {
  186. type: [String, Number],
  187. default: 0
  188. },
  189. // 遮罩的样式,一般用于修改遮罩的透明度
  190. maskCustomStyle: {
  191. type: Object,
  192. default() {
  193. return {}
  194. }
  195. },
  196. // 遮罩打开或收起的动画过渡时间,单位ms
  197. duration: {
  198. type: [String, Number],
  199. default: 250
  200. },
  201. // 遮罩的模糊度
  202. blur: {
  203. type: [String, Number],
  204. default: 0
  205. },
  206. },
  207. data() {
  208. return {
  209. visibleSync: false,
  210. showDrawer: false,
  211. timer: null,
  212. closeFromInner: false, // value的值改变,是发生在内部还是外部
  213. };
  214. },
  215. computed: {
  216. valueCom(){
  217. // #ifndef VUE3
  218. return this.value;
  219. // #endif
  220. // #ifdef VUE3
  221. return this.modelValue;
  222. // #endif
  223. },
  224. // 根据mode的位置,设定其弹窗的宽度(mode = left|right),或者高度(mode = top|bottom)
  225. style() {
  226. let style = {};
  227. // 如果是左边或者上边弹出时,需要给translate设置为负值,用于隐藏
  228. if (this.mode == 'left' || this.mode == 'right') {
  229. style = {
  230. width: this.width ? this.getUnitValue(this.width) : this.getUnitValue(this.length),
  231. height: '100%',
  232. transform: `translate3D(${this.mode == 'left' ? '-100%' : '100%'},0px,0px)`
  233. };
  234. } else if (this.mode == 'top' || this.mode == 'bottom') {
  235. style = {
  236. width: '100%',
  237. height: this.height ? this.getUnitValue(this.height) : this.getUnitValue(this.length),
  238. transform: `translate3D(0px,${this.mode == 'top' ? '-100%' : '100%'},0px)`
  239. };
  240. }
  241. style.zIndex = this.uZindex;
  242. // 如果用户设置了borderRadius值,添加弹窗的圆角
  243. if (this.borderRadius) {
  244. switch (this.mode) {
  245. case 'left':
  246. style.borderRadius = `0 ${this.borderRadius}rpx ${this.borderRadius}rpx 0`;
  247. break;
  248. case 'top':
  249. style.borderRadius = `0 0 ${this.borderRadius}rpx ${this.borderRadius}rpx`;
  250. break;
  251. case 'right':
  252. style.borderRadius = `${this.borderRadius}rpx 0 0 ${this.borderRadius}rpx`;
  253. break;
  254. case 'bottom':
  255. style.borderRadius = `${this.borderRadius}rpx ${this.borderRadius}rpx 0 0`;
  256. break;
  257. default:
  258. }
  259. // 不加可能圆角无效
  260. style.overflow = 'hidden';
  261. }
  262. if(this.duration) style.transition = `all ${this.duration / 1000}s linear`;
  263. return style;
  264. },
  265. // 中部弹窗的特有样式
  266. centerStyle() {
  267. let style = {};
  268. style.width = this.width ? this.getUnitValue(this.width) : this.getUnitValue(this.length);
  269. // 中部弹出的模式,如果没有设置高度,就用auto值,由内容撑开高度
  270. style.height = this.height ? this.getUnitValue(this.height) : 'auto';
  271. style.zIndex = this.uZindex;
  272. style.marginTop = `-${this.$u.addUnit(this.negativeTop)}`;
  273. if (this.borderRadius) {
  274. style.borderRadius = `${this.borderRadius}rpx`;
  275. // 不加可能圆角无效
  276. style.overflow = 'hidden';
  277. }
  278. return style;
  279. },
  280. // 计算整理后的z-index值
  281. uZindex() {
  282. return this.zIndex ? this.zIndex : this.$u.zIndex.popup;
  283. }
  284. },
  285. watch: {
  286. valueCom:{
  287. immediate: true,
  288. handler(val){
  289. if (val) {
  290. this.open();
  291. } else if(!this.closeFromInner) {
  292. this.close();
  293. }
  294. this.closeFromInner = false;
  295. }
  296. }
  297. },
  298. mounted() {
  299. // 组件渲染完成时,检查value是否为true,如果是,弹出popup
  300. this.valueCom && this.open();
  301. },
  302. methods: {
  303. // 判断传入的值,是否带有单位,如果没有,就默认用rpx单位
  304. getUnitValue(val) {
  305. if(/(%|px|rpx|auto)$/.test(val)) return val;
  306. else return val + 'rpx'
  307. },
  308. // 遮罩被点击
  309. maskClick() {
  310. this.close();
  311. },
  312. close() {
  313. // 标记关闭是内部发生的,否则修改了value值,导致watch中对value检测,导致再执行一遍close
  314. // 造成@close事件触发两次
  315. this.closeFromInner = true;
  316. this.change('showDrawer', 'visibleSync', false);
  317. },
  318. // 中部弹出时,需要.u-drawer-content将居中内容,此元素会铺满屏幕,点击需要关闭弹窗
  319. // 让其只在mode=center时起作用
  320. modeCenterClose(mode) {
  321. if (mode != 'center' || !this.maskCloseAble) return;
  322. this.close();
  323. },
  324. open() {
  325. this.change('visibleSync', 'showDrawer', true);
  326. },
  327. // 此处的原理是,关闭时先通过动画隐藏弹窗和遮罩,再移除整个组件
  328. // 打开时,先渲染组件,延时一定时间再让遮罩和弹窗的动画起作用
  329. change(param1, param2, status) {
  330. // 如果this.popup为false,意味着为picker,actionsheet等组件调用了popup组件
  331. if (this.popup == true) {
  332. this.$emit('input', status);
  333. this.$emit("update:modelValue", status);
  334. }
  335. this[param1] = status;
  336. if(status) {
  337. // #ifdef H5 || MP
  338. this.timer = setTimeout(() => {
  339. this[param2] = status;
  340. this.$emit(status ? 'open' : 'close');
  341. }, 50);
  342. // #endif
  343. // #ifndef H5 || MP
  344. this.$nextTick(() => {
  345. this[param2] = status;
  346. this.$emit(status ? 'open' : 'close');
  347. })
  348. // #endif
  349. } else {
  350. this.timer = setTimeout(() => {
  351. this[param2] = status;
  352. this.$emit(status ? 'open' : 'close');
  353. }, this.duration);
  354. }
  355. }
  356. }
  357. };
  358. </script>
  359. <style scoped lang="scss">
  360. @import "../../libs/css/style.components.scss";
  361. .u-drawer {
  362. /* #ifndef APP-NVUE */
  363. display: block;
  364. /* #endif */
  365. position: fixed;
  366. top: 0;
  367. left: 0;
  368. right: 0;
  369. bottom: 0;
  370. overflow: hidden;
  371. }
  372. .u-drawer-content {
  373. /* #ifndef APP-NVUE */
  374. display: block;
  375. /* #endif */
  376. position: absolute;
  377. z-index: 1003;
  378. transition: all 0.25s linear;
  379. }
  380. .u-drawer__scroll-view {
  381. width: 100%;
  382. height: 100%;
  383. }
  384. .u-drawer-left {
  385. top: 0;
  386. bottom: 0;
  387. left: 0;
  388. background-color: #ffffff;
  389. }
  390. .u-drawer-right {
  391. right: 0;
  392. top: 0;
  393. bottom: 0;
  394. background-color: #ffffff;
  395. }
  396. .u-drawer-top {
  397. top: 0;
  398. left: 0;
  399. right: 0;
  400. background-color: #ffffff;
  401. }
  402. .u-drawer-bottom {
  403. bottom: 0;
  404. left: 0;
  405. right: 0;
  406. background-color: #ffffff;
  407. }
  408. .u-drawer-center {
  409. @include vue-flex;
  410. flex-direction: column;
  411. bottom: 0;
  412. left: 0;
  413. right: 0;
  414. top: 0;
  415. justify-content: center;
  416. align-items: center;
  417. opacity: 0;
  418. z-index: 99999;
  419. }
  420. .u-mode-center-box {
  421. min-width: 100rpx;
  422. min-height: 100rpx;
  423. /* #ifndef APP-NVUE */
  424. display: block;
  425. /* #endif */
  426. position: relative;
  427. background-color: #ffffff;
  428. }
  429. .u-drawer-content-visible.u-drawer-center {
  430. transform: scale(1);
  431. opacity: 1;
  432. }
  433. .u-animation-zoom {
  434. transform: scale(1.15);
  435. }
  436. .u-drawer-content-visible {
  437. transform: translate3D(0px, 0px, 0px) !important;
  438. }
  439. .u-close {
  440. position: absolute;
  441. z-index: 3;
  442. }
  443. .u-close--top-left {
  444. top: 30rpx;
  445. left: 30rpx;
  446. }
  447. .u-close--top-right {
  448. top: 30rpx;
  449. right: 30rpx;
  450. }
  451. .u-close--bottom-left {
  452. bottom: 30rpx;
  453. left: 30rpx;
  454. }
  455. .u-close--bottom-right {
  456. right: 30rpx;
  457. bottom: 30rpx;
  458. }
  459. </style>