Skip to content

Commit c0de7fb

Browse files
committed
optimize: gallery logic
1 parent 7ba766c commit c0de7fb

File tree

6 files changed

+56
-23
lines changed

6 files changed

+56
-23
lines changed

.env.production

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11

22

3-
VUE_APP_BASE_URL = 'http://dev.lin.colorful3.com/'
3+
VUE_APP_BASE_URL = ''http://koa.lin.colorful3.com/

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"fastscan": "^1.0.4",
2222
"good-storage": "^1.1.0",
2323
"js-cookie": "^2.2.0",
24-
"lodash": "^4.17.11",
24+
"lodash": "^4.17.14",
2525
"moment": "^2.24.0",
2626
"photoswipe": "^4.1.2",
2727
"screenfull": "^4.2.0",

src/components/base/preview/preview.vue

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
<script>
5555
// photoswipe接口文档 http://photoswipe.com/documentation/api.html
5656
57+
import { Loading } from 'element-ui'
5758
import 'photoswipe/dist/photoswipe.css'
5859
import 'photoswipe/dist/default-skin/default-skin.css'
5960
import PhotoSwipe from 'photoswipe/dist/photoswipe'
@@ -124,6 +125,7 @@ export default {
124125
options = Object.assign(defaultOptions, options, {
125126
index: imageIndex,
126127
})
128+
const loadingInstance = Loading.service()
127129
const galleryElement = this.$refs.myGallery
128130
this.radom = createId();
129131
let pswpElement = this.$refs.pswpWrap
@@ -144,6 +146,7 @@ export default {
144146
}
145147
this.gallery = new PhotoSwipe(pswpElement, PhotoSwipeUIDefault, items, photoSwipeOptions)
146148
this.gallery.init()
149+
loadingInstance.close()
147150
// Gallery starts closing
148151
this.gallery.listen('close', () => {
149152
if (this.gallery) {
@@ -211,19 +214,23 @@ export default {
211214
}
212215
return items
213216
},
217+
destroy() {
218+
// 销毁
219+
if (this.gallery) {
220+
this.gallery.close();
221+
this.gallery = null
222+
}
223+
},
214224
},
215225
beforeDestroy() {
216-
// 销毁
217-
if (this.gallery) {
218-
this.gallery.close();
219-
this.gallery = null
220-
}
226+
this.destroy()
221227
},
222228
}
223229
</script>
224230

225-
<style lang="css" scoped>
231+
<style lang="scss" scoped>
226232
.my-gallery {
227233
opacity: 0;
234+
display: none;
228235
}
229236
</style>

src/components/layout/AppMain.vue

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
<script>
1313
export default {
1414
name: 'AppMain',
15+
watch: {
16+
$route() {
17+
if (this.$previewInstance) {
18+
this.$previewInstance.destroy()
19+
}
20+
},
21+
},
1522
}
1623
</script>
1724

src/lin/plugins/preview/index.js

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,45 @@
11
import Vue from 'vue'
22
import Preview from '@/components/base/preview/preview'
33

4+
45
const previewImage = {}
56
previewImage.install = (Vue, options = {}) => { // eslint-disable-line
67
const PreviewConstructor = Vue.extend(Preview)
78

9+
let instance = null
10+
811
// eslint-disable-next-line func-names
912
PreviewConstructor.prototype.close = function () {
1013
this.data = []
1114
this.options = {}
1215
this.imageIndex = 0
1316
}
1417

18+
const getInstance = () => {
19+
if (!instance) {
20+
instance = new PreviewConstructor()
21+
}
22+
return instance
23+
}
24+
1525
Vue.prototype.$imagePreview = (opts = {}) => { // eslint-disable-line
1626
const elem = document.createElement('div')
17-
let instance = new PreviewConstructor()
18-
instance.$mount(elem)
19-
instance.data = opts.images || []
20-
instance.imageIndex = opts.index || 0
21-
instance.options = opts.defaultOpt || {}
22-
document.body.appendChild(instance.$el)
23-
instance.$on('close', () => {
24-
instance.close()
25-
document.body.removeChild(instance.$el)
26-
instance.$destroy()
27-
instance = null
28-
})
27+
if (!instance) {
28+
let myInstance = getInstance()
29+
Vue.prototype.$previewInstance = myInstance // eslint-disable-line
30+
myInstance.$mount(elem)
31+
myInstance.data = opts.images || []
32+
myInstance.imageIndex = opts.index || 0
33+
myInstance.options = opts.defaultOpt || {}
34+
document.body.appendChild(myInstance.$el)
35+
myInstance.$on('close', () => {
36+
myInstance.close()
37+
document.body.removeChild(myInstance.$el)
38+
myInstance.$destroy()
39+
myInstance = null
40+
instance = null
41+
})
42+
}
2943
}
3044
}
3145

src/plugins/custom/views/Gallery.vue

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<div
77
class="img-box"
88
@click="preview(index)"
9-
v-for="(url, index) in urls"
9+
v-for="(url, index) in thumbs"
1010
:key="index">
1111
<el-image
1212
class="thumb-item-img"
@@ -35,8 +35,13 @@ export default {
3535
'https://consumerminiaclprd01.blob.core.chinacloudapi.cn/miniappbackground/sfgmember/lin/gallery/gallery3.jpg',
3636
'https://consumerminiaclprd01.blob.core.chinacloudapi.cn/miniappbackground/sfgmember/lin/gallery/gallery4.jpg',
3737
'https://consumerminiaclprd01.blob.core.chinacloudapi.cn/miniappbackground/sfgmember/lin/gallery/gallery5.jpg',
38-
'https://consumerminiaclprd01.blob.core.chinacloudapi.cn/miniappbackground/sfgmember/lin/gallery/gallery6.jpg',
39-
'https://consumerminiaclprd01.blob.core.chinacloudapi.cn/miniappbackground/sfgmember/lin/gallery/gallery7.jpg',
38+
],
39+
thumbs: [
40+
'https://consumerminiaclprd01.blob.core.chinacloudapi.cn/miniappbackground/sfgmember/lin/gallery/thumb/gallery1.jpg',
41+
'https://consumerminiaclprd01.blob.core.chinacloudapi.cn/miniappbackground/sfgmember/lin/gallery/thumb/gallery2.jpg',
42+
'https://consumerminiaclprd01.blob.core.chinacloudapi.cn/miniappbackground/sfgmember/lin/gallery/thumb/gallery3.jpg',
43+
'https://consumerminiaclprd01.blob.core.chinacloudapi.cn/miniappbackground/sfgmember/lin/gallery/thumb/gallery4.jpg',
44+
'https://consumerminiaclprd01.blob.core.chinacloudapi.cn/miniappbackground/sfgmember/lin/gallery/thumb/gallery5.jpg',
4045
],
4146
}
4247
},

0 commit comments

Comments
 (0)