index.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. <template>
  2. <view
  3. :class="['s-popup',positionClass,visibleClass,effectClass,customClass]"
  4. :style="styleZindex+styleDuration"
  5. >
  6. <template v-if="mask">
  7. <view
  8. class="s-popup-mask"
  9. @touchmove.stop.prevent="e=>e.preventDefault()"
  10. @click="maskClose && hide()"
  11. :style="'background-color: rgba(0, 0, 0, '+maskOpacity+');'"
  12. ></view>
  13. </template>
  14. <view
  15. v-if="preventTouchmove"
  16. class="s-popup-wrapper"
  17. @touchmove.stop.prevent="e=>e.preventDefault()"
  18. >
  19. <slot></slot>
  20. </view>
  21. <view v-else class="s-popup-wrapper">
  22. <slot></slot>
  23. </view>
  24. </view>
  25. </template>
  26. <script>
  27. // 记录弹框的zIndex
  28. const ZindexMap = new Map();
  29. const getMaxZindex = () => {
  30. return Math.max(200, ...ZindexMap.values()) + 1;
  31. };
  32. export default {
  33. name: 's-popup',
  34. props: {
  35. // class
  36. customClass: {
  37. type: String,
  38. default: ''
  39. },
  40. // v-model双向绑定
  41. value: Boolean,
  42. // 弹框显示位置 center | left | right | top | bottom
  43. position: {
  44. type: String,
  45. default: 'center'
  46. },
  47. // 是否使用过渡效果
  48. effect: {
  49. type: Boolean,
  50. default: true
  51. },
  52. // 过渡时间
  53. effectDuration: {
  54. type: Number,
  55. default: 300
  56. },
  57. // 是否显示遮罩
  58. mask: {
  59. type: Boolean,
  60. default: true
  61. },
  62. // 遮罩透明度
  63. maskOpacity: {
  64. type: Number,
  65. default: 0.7
  66. },
  67. // 点击遮罩是否关闭弹框
  68. maskClose: {
  69. type: Boolean,
  70. default: true
  71. },
  72. // 自动关闭时间
  73. duration: {
  74. type: Number,
  75. default: 0
  76. },
  77. // 是否阻止弹层touchmove滚动,手机上滚动穿透
  78. preventTouchmove: {
  79. type: Boolean,
  80. default: false
  81. },
  82. // 显示时拦截钩子返回promise拦截
  83. beforeShow: Function,
  84. // 隐藏时拦截钩子返回promise拦截
  85. beforeHide: Function
  86. },
  87. data () {
  88. Object.assign(this, {
  89. popupId: 's-popup-id-' + Math.random().toString(36).substr(2),
  90. visibleId: 0,
  91. visibleStatus: false,
  92. effectTimeoutId: 0,
  93. autoCloseTimeoutId: 0
  94. });
  95. return {
  96. styleZindex: '',
  97. visibleClass: '',
  98. styleDuration: '',
  99. effectClass: ''
  100. };
  101. },
  102. computed: {
  103. positionClass () {
  104. return this.position ? 's-popup-position-' + this.position : '';
  105. }
  106. },
  107. watch: {
  108. value () {
  109. this.updateVisible();
  110. }
  111. },
  112. methods: {
  113. async show () {
  114. if (!this.visibleStatus) {
  115. this.visibleId++;
  116. let status = true;
  117. const nowId = this.visibleId;
  118. if (this.beforeShow) {
  119. try {
  120. await this.beforeShow.call(this.$parent);
  121. } catch (error) {
  122. status = false;
  123. }
  124. }
  125. if (nowId === this.visibleId) {
  126. if (status) {
  127. const effectDuration = this.effect ? this.effectDuration : 0;
  128. this.visibleStatus = true;
  129. this.$emit('input', true);
  130. ZindexMap.set(this.popupId, getMaxZindex());
  131. this.styleZindex = `z-index:${ZindexMap.get(this.popupId)};`;
  132. this.styleDuration = `animation-duration:${effectDuration}ms;`;
  133. this.visibleClass = 's-popup-visible';
  134. this.effectClass = 's-popup-effect-enter';
  135. clearTimeout(this.effectTimeoutId);
  136. this.effectTimeoutId = setTimeout(() => {
  137. this.styleDuration = '';
  138. this.effectClass = '';
  139. if (this.visibleStatus) {
  140. this.$emit('show');
  141. // 自动关闭
  142. const duration = parseInt(this.duration);
  143. if (duration > 0) {
  144. clearTimeout(this.autoCloseTimeoutId);
  145. this.autoCloseTimeoutId = setTimeout(() => {
  146. this.visibleStatus && this.hide();
  147. }, duration);
  148. }
  149. }
  150. }, effectDuration);
  151. } else {
  152. this.$emit('input', false);
  153. }
  154. }
  155. }
  156. },
  157. async hide () {
  158. if (this.visibleStatus) {
  159. this.visibleId++;
  160. let status = true;
  161. const nowId = this.visibleId;
  162. if (this.beforeHide) {
  163. try {
  164. await this.beforeHide.call(this.$parent);
  165. } catch (error) {
  166. status = false;
  167. }
  168. }
  169. if (nowId === this.visibleId) {
  170. if (status) {
  171. const effectDuration = this.effect ? this.effectDuration : 0;
  172. this.visibleStatus = false;
  173. this.$emit('input', false);
  174. this.styleDuration = 'animation-duration:' + effectDuration + 'ms;';
  175. this.effectClass = 's-popup-effect-leave';
  176. clearTimeout(this.autoCloseTimeoutId);
  177. clearTimeout(this.effectTimeoutId);
  178. this.effectTimeoutId = setTimeout(() => {
  179. this.visibleClass = '';
  180. this.effectClass = '';
  181. this.styleZindex = '';
  182. this.styleDuration = '';
  183. ZindexMap.delete(this.popupId);
  184. if (!this.visibleStatus) {
  185. this.$emit('hide');
  186. }
  187. }, effectDuration);
  188. } else {
  189. this.$emit('input', true);
  190. }
  191. }
  192. }
  193. },
  194. updateVisible () {
  195. this.$nextTick(() => {
  196. if (this.visibleStatus !== this.value) {
  197. this[this.value ? 'show' : 'hide']();
  198. }
  199. });
  200. }
  201. },
  202. mounted () {
  203. this.updateVisible();
  204. },
  205. beforeDestroy () {
  206. ZindexMap.delete(this.popupId);
  207. clearTimeout(this.effectTimeoutId);
  208. clearTimeout(this.autoCloseTimeoutId);
  209. }
  210. };
  211. </script>
  212. <style lang="scss">
  213. // fade效果
  214. @keyframes s-popup-fade-enter {
  215. 0% {
  216. opacity: 0;
  217. }
  218. to {
  219. opacity: 1;
  220. }
  221. }
  222. @keyframes s-popup-fade-leave {
  223. 0% {
  224. opacity: 1;
  225. }
  226. to {
  227. opacity: 0;
  228. }
  229. }
  230. // 显示和隐藏效果
  231. @keyframes s-popup-center-enter {
  232. 0% {
  233. opacity: 0;
  234. transform: scale(0.7);
  235. }
  236. to {
  237. opacity: 1;
  238. }
  239. }
  240. @keyframes s-popup-center-leave {
  241. 0% {
  242. opacity: 1;
  243. }
  244. to {
  245. opacity: 0;
  246. transform: scale(0.9);
  247. }
  248. }
  249. @keyframes s-popup-top-enter {
  250. 0% {
  251. transform: translate3d(0, -100%, 0);
  252. }
  253. to {
  254. transform: translate3d(0, 0, 0);
  255. }
  256. }
  257. @keyframes s-popup-top-leave {
  258. to {
  259. transform: translate3d(0, -100%, 0);
  260. }
  261. }
  262. @keyframes s-popup-left-enter {
  263. 0% {
  264. transform: translate3d(-100%, 0, 0);
  265. }
  266. to {
  267. transform: translate3d(0, 0, 0);
  268. }
  269. }
  270. @keyframes s-popup-left-leave {
  271. to {
  272. transform: translate3d(-100%, 0, 0);
  273. }
  274. }
  275. @keyframes s-popup-right-enter {
  276. 0% {
  277. transform: translate3d(100%, 0, 0);
  278. }
  279. to {
  280. transform: translate3d(0, 0, 0);
  281. }
  282. }
  283. @keyframes s-popup-right-leave {
  284. to {
  285. transform: translate3d(100%, 0, 0);
  286. }
  287. }
  288. @keyframes s-popup-bottom-enter {
  289. 0% {
  290. transform: translate3d(0, 100%, 0);
  291. }
  292. to {
  293. transform: translate3d(0, 0, 0);
  294. }
  295. }
  296. @keyframes s-popup-bottom-leave {
  297. to {
  298. transform: translate3d(0, 100%, 0);
  299. }
  300. }
  301. .s-popup {
  302. display: none;
  303. position: fixed;
  304. left: 0;
  305. right: 0;
  306. top: var(--window-top);
  307. bottom: var(--window-bottom);
  308. margin: 0;
  309. padding: 0;
  310. box-sizing: border-box;
  311. pointer-events: none;
  312. &-visible {
  313. display: flex;
  314. }
  315. &-mask {
  316. position: absolute;
  317. left: 0;
  318. top: 0;
  319. right: 0;
  320. bottom: 0;
  321. z-index: 1;
  322. pointer-events: auto;
  323. }
  324. &-wrapper {
  325. position: absolute;
  326. max-width: 100%;
  327. max-height: 100%;
  328. z-index: 2;
  329. pointer-events: auto;
  330. background-color: white;
  331. overflow: auto;
  332. }
  333. // 弹框效果
  334. animation-fill-mode: both;
  335. &-mask,
  336. &-wrapper {
  337. animation-fill-mode: both;
  338. animation-duration: inherit;
  339. }
  340. &-effect-enter {
  341. .s-popup-mask,
  342. .s-popup-wrapper {
  343. animation-name: s-popup-fade-enter;
  344. }
  345. }
  346. &-effect-leave {
  347. .s-popup-mask,
  348. .s-popup-wrapper {
  349. animation-name: s-popup-fade-leave;
  350. }
  351. }
  352. }
  353. //center
  354. .s-popup-position-center {
  355. justify-content: center;
  356. align-items: center;
  357. .s-popup-wrapper {
  358. position: relative;
  359. }
  360. &.s-popup-effect-enter {
  361. .s-popup-wrapper {
  362. animation-name: s-popup-center-enter;
  363. }
  364. }
  365. &.s-popup-effect-leave {
  366. .s-popup-wrapper {
  367. animation-name: s-popup-center-leave;
  368. }
  369. }
  370. }
  371. //top
  372. .s-popup-position-top {
  373. .s-popup-wrapper {
  374. left: 0;
  375. right: 0;
  376. top: 0;
  377. }
  378. &.s-popup-effect-enter {
  379. .s-popup-wrapper {
  380. animation-name: s-popup-top-enter;
  381. }
  382. }
  383. &.s-popup-effect-leave {
  384. .s-popup-wrapper {
  385. animation-name: s-popup-top-leave;
  386. }
  387. }
  388. }
  389. //bottom
  390. .s-popup-position-bottom {
  391. .s-popup-wrapper {
  392. left: 0;
  393. right: 0;
  394. bottom: 0;
  395. }
  396. &.s-popup-effect-enter {
  397. .s-popup-wrapper {
  398. animation-name: s-popup-bottom-enter;
  399. }
  400. }
  401. &.s-popup-effect-leave {
  402. .s-popup-wrapper {
  403. animation-name: s-popup-bottom-leave;
  404. }
  405. }
  406. }
  407. //从左侧滑入
  408. .s-popup-position-left {
  409. .s-popup-wrapper {
  410. left: 0;
  411. top: 0;
  412. bottom: 0;
  413. }
  414. &.s-popup-effect-enter {
  415. .s-popup-wrapper {
  416. animation-name: s-popup-left-enter;
  417. }
  418. }
  419. &.s-popup-effect-leave {
  420. .s-popup-wrapper {
  421. animation-name: s-popup-left-leave;
  422. }
  423. }
  424. }
  425. //从右侧滑入
  426. .s-popup-position-right {
  427. .s-popup-wrapper {
  428. right: 0;
  429. top: 0;
  430. bottom: 0;
  431. }
  432. &.s-popup-effect-enter {
  433. .s-popup-wrapper {
  434. animation-name: s-popup-right-enter;
  435. }
  436. }
  437. &.s-popup-effect-leave {
  438. .s-popup-wrapper {
  439. animation-name: s-popup-right-leave;
  440. }
  441. }
  442. }
  443. </style>