ben-photoswipe.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <template>
  2. <view class="content">
  3. <div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
  4. <div class="pswp__bg"></div>
  5. <div class="pswp__scroll-wrap">
  6. <div class="pswp__container">
  7. <div class="pswp__item"></div>
  8. <div class="pswp__item"></div>
  9. <div class="pswp__item"></div>
  10. </div>
  11. <!-- 预览区域顶部的默认UI,可以修改 -->
  12. <div class="pswp__ui pswp__ui--hidden">
  13. <div class="pswp__top-bar">
  14. <!-- 与图片相关的操作 -->
  15. <div class="pswp__counter"></div>
  16. <template v-if="imgs.length>0">
  17. <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
  18. <div>
  19. <button class="btn_item" @click.native.prevent="viewPicture"
  20. v-if="!imgs[idx].picture">查看原图</button>
  21. </div>
  22. </template>
  23. <div class="pswp__preloader">
  24. <div class="pswp__preloader__icn">
  25. <div class="pswp__preloader__cut">
  26. <div class="pswp__preloader__donut"></div>
  27. </div>
  28. </div>
  29. </div>
  30. </div>
  31. <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
  32. <div class="pswp__share-tooltip"></div>
  33. </div>
  34. <div class="pswp__caption">
  35. <div class="pswp__caption__center"></div>
  36. </div>
  37. </div>
  38. </div>
  39. </div>
  40. </view>
  41. </template>
  42. <script>
  43. import PhotoSwipe from 'photoswipe/dist/photoswipe'
  44. import PhotoSwipeUI_Default from 'photoswipe/dist/photoswipe-ui-default'
  45. import 'photoswipe/dist/photoswipe.css'
  46. import 'photoswipe/dist/default-skin/default-skin.css'
  47. var gallery = null
  48. const promixify = (api) => {
  49. return (options, ...params) => {
  50. return new Promise((resolve, reject) => {
  51. api(Object.assign({}, options, {
  52. success: resolve,
  53. fail: resolve
  54. }), ...params)
  55. })
  56. }
  57. }
  58. export default {
  59. name: "photoswipe",
  60. props: {
  61. imgs: {
  62. //图片列表
  63. type: Array,
  64. required: true,
  65. default: () => {
  66. return [];
  67. }
  68. },
  69. },
  70. data() {
  71. return {
  72. idx:0,
  73. };
  74. },
  75. methods: {
  76. resImg(){
  77. if(gallery){
  78. if(gallery.container.clientHeight>0){
  79. gallery.updateSize( true );
  80. }
  81. }
  82. },
  83. initImage(e) {
  84. let that = this
  85. var pswpElement = document.querySelectorAll('.pswp')[0];
  86. var items = [];
  87. const getImageInfo = promixify(uni.getImageInfo)
  88. Promise.all(
  89. that.imgs.map(img => getImageInfo({
  90. src: img.picture ? img.imgUrl : img.acronymUrl
  91. }))
  92. ).then((imageInfos) => {
  93. imageInfos.map((i, k) => {
  94. i.w = i.width,
  95. i.h = i.height
  96. i.src = i.path; //大图
  97. i.msrc = i.path; //小图
  98. })
  99. items = imageInfos
  100. var options = {
  101. index: e, // start at first slide
  102. history: false
  103. };
  104. gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, options);
  105. gallery.init();
  106. that.idx = gallery.getCurrentIndex()
  107. gallery.listen('beforeChange', function() {
  108. that.idx = gallery.getCurrentIndex()
  109. });
  110. })
  111. },
  112. //查看原图
  113. viewPicture(){
  114. let that = this
  115. let idx = gallery.getCurrentIndex()
  116. that.imgs[idx].picture = true
  117. let itm = [that.imgs[idx]]
  118. const getImageInfo = promixify(uni.getImageInfo)
  119. Promise.all(
  120. itm.map(img => getImageInfo({ src:img.imgUrl }))
  121. ).then((imageInfos) => {
  122. gallery.items[idx]={
  123. src:imageInfos[0].path,
  124. w:imageInfos[0].width,
  125. h:imageInfos[0].height,
  126. msrc :imageInfos[0].path,
  127. };
  128. gallery.invalidateCurrItems();
  129. gallery.updateSize( true );
  130. })
  131. },
  132. }
  133. }
  134. </script>
  135. <style lang="scss">
  136. .btn_item{
  137. font-size: 24rpx;
  138. background: none;
  139. border: 2rpx solid #FFFFFF;
  140. color: #FFFFFF;
  141. position: absolute;
  142. bottom: 0;
  143. right: 100rpx;
  144. height: 60rpx;
  145. line-height: 60rpx;
  146. top: 10rpx;
  147. }
  148. .btn_item::after{
  149. display: none;
  150. }
  151. </style>