1- import Vue from 'vue' ;
2- import { $ } from './util' ;
1+ import { $ } from './util' ;
32
43const EV_SCROLL = 'scroll' ,
54
4443}
4544
4645var loaderID = 0 ;
47- function LazyClass ( scope ) {
46+ export function LazyClass ( scope ) {
4847 return class LazyLoader {
4948 constructor ( opts ) {
5049 const me = this ,
@@ -83,8 +82,10 @@ function LazyClass(scope) {
8382 }
8483 else {
8584 // inherit parent LazyLoader's options
86- opts = Object . create ( parent && parent . opts , {
87- parent : scope . $lazy ,
85+ const $rootLazy = scope . $lazy ;
86+ opts = Object . assign ( {
87+ parent : $rootLazy
88+ } , parent && parent . opts || $rootLazy . opts , {
8889 ...opts
8990 } ) ;
9091 }
@@ -141,8 +142,8 @@ function LazyClass(scope) {
141142 preloadRatio = me . opts . preloadRatio ,
142143 isWin = parentEl === win ,
143144 parentElOffset = isWin ? {
144- left : 0 ,
145- top : 0 ,
145+ left : win . scrollX ,
146+ top : win . scrollY ,
146147 width : win . innerWidth ,
147148 height : win . innerHeight
148149 } : offset ( parentEl ) ,
@@ -153,14 +154,14 @@ function LazyClass(scope) {
153154 parentElHeight = parentElOffset . height * preloadRatio ,
154155 elLeft = elOffset . left ,
155156 elTop = elOffset . top ,
156- elWidth = elOffset . width * preloadRatio ,
157- elHeight = elOffset . height * preloadRatio ;
157+ elWidth = elOffset . width ,
158+ elHeight = elOffset . height ;
158159
159160 // Collision detection
160- if ( elLeft > parentElLeft - elLeft &&
161- elLeft < parentElLeft + parentElWidth + elWidth &&
162- elTop > parentElTop - elHeight &&
163- elTop < parentElTop + parentElHeight + elHeight ) {
161+ if ( elLeft < parentElLeft + parentElWidth &&
162+ elLeft + elWidth > parentElLeft &&
163+ elTop < parentElTop + parentElHeight &&
164+ elTop + elHeight > parentElTop ) {
164165 result = true ;
165166 }
166167 }
@@ -186,7 +187,7 @@ function LazyClass(scope) {
186187 cb = cbs [ event ] ;
187188
188189 if ( ! cb ) {
189- cbs [ event ] = throttle ( function ( ) {
190+ cb = cbs [ event ] = throttle ( function ( ) {
190191 for ( var j = 0 , jLen = queue . length ; j < jLen ; j ++ ) {
191192 queue [ j ] . check ( ) ;
192193 }
@@ -208,6 +209,8 @@ function LazyClass(scope) {
208209
209210 // reset
210211 me . stat = STAT_NOT_LOAD ;
212+
213+ me . check ( ) ;
211214 }
212215
213216 rmChild ( lazyLoader ) {
@@ -268,7 +271,6 @@ class Req {
268271 }
269272
270273 me . canceled = false ;
271- me . stat = STAT_LOADING ;
272274 me . retry = retry ;
273275
274276 el . onload = ( ) => {
@@ -327,6 +329,7 @@ function loadHandler(lazyLoader) {
327329 switchClass ( elTarget , classLoaded ) ;
328330 }
329331 else {
332+ lazyLoader . stat = STAT_LOADING ;
330333 lazyLoader . req = new Req ( {
331334 el : el ,
332335 src : src ,
@@ -346,6 +349,3 @@ function loadHandler(lazyLoader) {
346349 }
347350}
348351
349- export default {
350- LazyClass
351- } ;
0 commit comments