11<template >
22 <div v-if =" data.length" >
3- <div ref =" myGallery" class =" my-gallery" data-pswp-uid =" 1 " >
3+ <div ref =" myGallery" class =" my-gallery" : data-pswp-uid =" radom " >
44 <div v-if =" slides.length" >
5- <div :key =" index" v-for =" (l ,index) in slides" >
5+ <div :key =" radom + '_' + index" v-for =" (l ,index) in slides" >
66 <img preview :src =" l" alt =" " >
77 </div >
88 </div >
5252</template >
5353
5454<script >
55-
5655// photoswipe接口文档 http://photoswipe.com/documentation/api.html
5756
5857import ' photoswipe/dist/photoswipe.css'
5958import ' photoswipe/dist/default-skin/default-skin.css'
6059import PhotoSwipe from ' photoswipe/dist/photoswipe'
6160import PhotoSwipeUIDefault from ' photoswipe/dist/photoswipe-ui-default'
62-
61+ /** 生成随机字符串 */
62+ function createId () {
63+ return Math .random ().toString (36 ).substring (2 )
64+ }
6365export default {
6466 name: ' PreviewImage' ,
6567 props: {
@@ -78,10 +80,13 @@ export default {
7880 },
7981 data () {
8082 return {
83+ radom: createId (),
8184 slides: this .data || [],
82- gallery: null , //
85+ gallery: null ,
8386 }
8487 },
88+ destroyed () {
89+ },
8590 watch: {
8691 data (newVal ) {
8792 if (Array .isArray (newVal)) {
@@ -103,13 +108,14 @@ export default {
103108 },
104109 methods: {
105110 /* eslint-disable */
106- initPhotoSwipe () {
111+ async initPhotoSwipe () {
107112 const that = this
108113 const defaultOptions = {
109114 fullscreenEl: true ,
110115 shareEl: false ,
111116 arrowEl: true ,
112117 preloaderEl: true ,
118+ history: false ,
113119 loop: false ,
114120 bgOpacity: 0.85 ,
115121 showHideOpacity: true ,
@@ -121,10 +127,11 @@ export default {
121127 index: imageIndex,
122128 })
123129 const galleryElement = this .$refs .myGallery
130+ this .radom = createId ();
124131 let pswpElement = this .$refs .pswpWrap
125- const items = this .transThumbnailElements ()
132+ const items = await this .transThumbnailElements ()
126133 let photoSwipeOptions = {
127- galleryUID: galleryElement . getAttribute ( ' data-pswp-uid ' ) ,
134+ galleryUID: this . radom ,
128135 getThumbBoundsFn : function (index ) {
129136 let thumbnail = items[index].el
130137 let pageYScroll = window .pageYOffset || document .documentElement .scrollTop
@@ -144,29 +151,59 @@ export default {
144151 if (this .gallery ) {
145152 this .gallery .close ()
146153 this .gallery = null
154+ this .slides = []
147155 }
148156 that .$emit (' close' )
149157 })
150158 },
151- transThumbnailElements () {
159+ async getWH (elem ) {
160+ return new Promise ((resolve , reject ) => {
161+ if (typeof elem .naturalWidth === " undefined" ) {
162+ // IE 6/7/8
163+ let img = new window.Image ()
164+ img .src = elem .getAttribute (' src' )
165+ img .onload = function () {
166+ resolve ({
167+ w: this .width ,
168+ h: this .height ,
169+ })
170+ }
171+ img .onerror = function () {
172+ reject ({
173+ w: 0 ,
174+ h: 0 ,
175+ })
176+ }
177+ } else {
178+ if (elem .naturalWidth > 0 ) {
179+ resolve ({
180+ w: elem .naturalWidth ,
181+ h: elem .naturalHeight ,
182+ })
183+ } else {
184+ elem .onload = function () {
185+ resolve ({
186+ w: this .naturalWidth ,
187+ h: this .naturalHeight ,
188+ })
189+ }
190+ }
191+ }
192+ })
193+ },
194+ async transThumbnailElements () {
152195 const galleryElement = this .$refs .myGallery
153196 const items = []
154197 const previewElements = galleryElement .querySelectorAll (' img[preview]' )
198+
155199 for (let i = 0 , l = previewElements .length ; i < l; i++ ) {
156200 let elem = previewElements[i]
157201 let rw = 0
158202 let rh = 0
159- if (typeof elem .naturalWidth === " undefined" ) {
160- // IE 6/7/8
161- let img = new window.Image ()
162- img .src = elem .getAttribute (' src' )
163- rw = img .width
164- rh = img .height
165- } else {
166- // HTML5 browsers
167- rw = elem .naturalWidth
168- rh = elem .naturalHeight
169- }
203+ const wh = await this .getWH (elem)
204+ rw = wh .w
205+ rh = wh .h
206+
170207 items .push ({
171208 el: elem,
172209 src: elem .getAttribute (' src' ),
0 commit comments