1
1
<template >
2
2
<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 " >
4
4
<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" >
6
6
<img preview :src =" l" alt =" " >
7
7
</div >
8
8
</div >
52
52
</template >
53
53
54
54
<script >
55
-
56
55
// photoswipe接口文档 http://photoswipe.com/documentation/api.html
57
56
58
57
import ' photoswipe/dist/photoswipe.css'
59
58
import ' photoswipe/dist/default-skin/default-skin.css'
60
59
import PhotoSwipe from ' photoswipe/dist/photoswipe'
61
60
import PhotoSwipeUIDefault from ' photoswipe/dist/photoswipe-ui-default'
62
-
61
+ /** 生成随机字符串 */
62
+ function createId () {
63
+ return Math .random ().toString (36 ).substring (2 )
64
+ }
63
65
export default {
64
66
name: ' PreviewImage' ,
65
67
props: {
@@ -78,10 +80,13 @@ export default {
78
80
},
79
81
data () {
80
82
return {
83
+ radom: createId (),
81
84
slides: this .data || [],
82
- gallery: null , //
85
+ gallery: null ,
83
86
}
84
87
},
88
+ destroyed () {
89
+ },
85
90
watch: {
86
91
data (newVal ) {
87
92
if (Array .isArray (newVal)) {
@@ -103,13 +108,14 @@ export default {
103
108
},
104
109
methods: {
105
110
/* eslint-disable */
106
- initPhotoSwipe () {
111
+ async initPhotoSwipe () {
107
112
const that = this
108
113
const defaultOptions = {
109
114
fullscreenEl: true ,
110
115
shareEl: false ,
111
116
arrowEl: true ,
112
117
preloaderEl: true ,
118
+ history: false ,
113
119
loop: false ,
114
120
bgOpacity: 0.85 ,
115
121
showHideOpacity: true ,
@@ -121,10 +127,11 @@ export default {
121
127
index: imageIndex,
122
128
})
123
129
const galleryElement = this .$refs .myGallery
130
+ this .radom = createId ();
124
131
let pswpElement = this .$refs .pswpWrap
125
- const items = this .transThumbnailElements ()
132
+ const items = await this .transThumbnailElements ()
126
133
let photoSwipeOptions = {
127
- galleryUID: galleryElement . getAttribute ( ' data-pswp-uid ' ) ,
134
+ galleryUID: this . radom ,
128
135
getThumbBoundsFn : function (index ) {
129
136
let thumbnail = items[index].el
130
137
let pageYScroll = window .pageYOffset || document .documentElement .scrollTop
@@ -144,29 +151,59 @@ export default {
144
151
if (this .gallery ) {
145
152
this .gallery .close ()
146
153
this .gallery = null
154
+ this .slides = []
147
155
}
148
156
that .$emit (' close' )
149
157
})
150
158
},
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 () {
152
195
const galleryElement = this .$refs .myGallery
153
196
const items = []
154
197
const previewElements = galleryElement .querySelectorAll (' img[preview]' )
198
+
155
199
for (let i = 0 , l = previewElements .length ; i < l; i++ ) {
156
200
let elem = previewElements[i]
157
201
let rw = 0
158
202
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
+
170
207
items .push ({
171
208
el: elem,
172
209
src: elem .getAttribute (' src' ),
0 commit comments