11<template >
22 <div class =" vue-image-kit" >
3- <div v-if =" dataUrl" :style =" { backgroundColor }" class = " vue-image-kit__placeholder " >
4- <img :src =" placeholder || dataUrl" alt =" Placeholder" :style =" { width: `${width}px`, height: `${height}px` }" >
3+ <div v-if =" dataUrl" class = " vue-image-kit__placeholder " :style =" { backgroundColor }" >
4+ <img :src =" placeholder || dataUrl" alt =" Placeholder" :style =" { width: `${width}px`, height: `${height}px` }" / >
55 </div >
6- <img :srcset =" getSrcset" :sizes =" getSizes" :src =" getSrc" :alt =" alt" :style =" { width: `${width}px`, height: `${height}px` }" class = " vue-image-kit__img " />
6+ <img class = " vue-image-kit__img " :srcset =" getSrcset" :sizes =" getSizes" :src =" getSrc" :alt =" alt" :style =" { width: `${width}px`, height: `${height}px` }" />
77 </div >
88</template >
99
@@ -77,35 +77,37 @@ export default {
7777 },
7878 getSrc () {
7979 const { showCanvas , dataUrl , imageKitPrefix , hash , src } = this
80+
8081 return showCanvas
8182 ? dataUrl
8283 : ` ${ imageKitPrefix} /${ hash} /${ src} `
8384 },
8485 getSrcset () {
85- return this .srcset
86- .map (size => ` ${ this .imageKitPrefix } /${ this .hash } /tr:w-${ size}${ this .customTransform ? ' ,' + this .customTransform : ' ' } /${ this .src } ${ size} w` )
86+ const { srcset , imageKitPrefix , hash , customTransform , src } = this
87+
88+ return srcset
89+ .map (size => ` ${ imageKitPrefix} /${ hash} /tr:w-${ size}${ customTransform ? ' ,' + customTransform : ' ' } /${ src} ${ size} w` )
8790 .join (' , ' )
8891 },
8992 getSizes () {
90- let sizes = ' '
91- if (this .sizes && this .sizes .length && (this .sizes .length === this .srcset .length )) {
92- sizes = this .srcset .map ((size , idx ) => ` (max-width: ${ size} px) ${ this .sizes [idx]} px` ).join (' , ' )
93+ const { sizes , srcset , defaultSize } = this
94+ let sizesString = ' '
95+ if (sizes && sizes .length && (sizes .length === srcset .length )) {
96+ sizesString = srcset .map ((size , idx ) => ` (max-width: ${ size} px) ${ sizes[idx]} px` ).join (' , ' )
9397 }
9498
95- sizes = this . srcset .map (size => ` (max-width: ${ size} px) ${ parseInt (size, 10 ) - 40 } px` ).join (' , ' )
96- sizes += ` ${ this . defaultSize } px`
99+ sizesString = srcset .map (size => ` (max-width: ${ size} px) ${ parseInt (size, 10 ) - 40 } px` ).join (' , ' )
100+ sizesString += ` ${ defaultSize} px`
97101
98- return sizes
102+ return sizesString
99103 }
100104 },
101105 mounted () {
102106 this .showCanvas = true
103-
104107 this .observer = new IntersectionObserver (([entry ]) => {
105108 this .triggerIntersection (entry)
106109 })
107110 this .observer .observe (this .$el )
108-
109111 this .$once (' hook:beforeDestroy' , () => {
110112 this .observer .disconnect ()
111113
@@ -116,27 +118,29 @@ export default {
116118 },
117119 methods: {
118120 triggerIntersection (entry = {}) {
119- const img = this .$el .querySelector (' .vue-image-kit__img' )
120- const placeholder = this .$el .querySelector (' .vue-image-kit__placeholder' )
121+ const { $el , timeOut , srcset , getSrcset , imageKitPrefix , hash , src } = this
122+ const img = $el .querySelector (' .vue-image-kit__img' )
123+ const placeholder = $el .querySelector (' .vue-image-kit__placeholder' )
121124
122125 img .onload = function () {
123126 delete img .onload
124- this . $el .classList .add (' vue-image-kit--loaded' )
127+ $el .classList .add (' vue-image-kit--loaded' )
125128
126129 if (placeholder) {
127- this . timeOut = setTimeout (() => {
130+ timeOut = setTimeout (() => {
128131 placeholder .remove ()
129132 }, 300 )
130133 }
131134 }
135+
132136 if (entry .isIntersecting ) {
133137 this .showCanvas = false
134138
135- if (this . srcset ) {
136- img .srcset = this . getSrcset
139+ if (srcset) {
140+ img .srcset = getSrcset
137141 }
138142
139- img .src = ` ${ this . imageKitPrefix } /${ this . hash } /${ this . src } `
143+ img .src = ` ${ imageKitPrefix} /${ hash} /${ src} `
140144 this .observer .disconnect ()
141145 }
142146 }
0 commit comments