@@ -4,27 +4,25 @@ export default {
4
4
name: ' CImageLazy' ,
5
5
extends: CImage,
6
6
props: {
7
+ loadInitially: Boolean ,
7
8
loadOffset: {
8
- type: [ Number , String , Boolean ] ,
9
+ type: Number ,
9
10
default: 500
10
11
},
11
- fade: {
12
- type: Boolean ,
13
- default: true
14
- },
12
+ noFade: Boolean ,
15
13
fadeOffset: {
16
- type: [ Number , String , Boolean ] ,
14
+ type: Number ,
17
15
default: - 100
18
16
},
19
17
fadeTime: {
20
- type: [ Number , String ] ,
18
+ type: Number ,
21
19
default: 1500
22
20
}
23
21
},
24
22
data () {
25
23
return {
26
- active: this .loadOffset === false ,
27
- animated: this .fadeOffset === false
24
+ active: this .loadInitially ,
25
+ animated: this .noFade
28
26
}
29
27
},
30
28
mounted () {
@@ -36,31 +34,40 @@ export default {
36
34
},
37
35
computed: {
38
36
animationClasses () {
39
- if (this .fade ) {
37
+ if (! this .noFade ) {
40
38
return { ' c-opacity-0' : ! this .animated }
41
39
}
40
+ },
41
+ propOffset () {
42
+ return ! this .active ? this .loadOffset : this .fadeOffset
42
43
}
43
44
},
44
45
methods: {
45
46
checkPosition () {
46
47
const rect = this .$el .getBoundingClientRect ()
47
- const offset = ! this .active ? this . loadOffset : this . fadeOffset
48
+ const offset = this .getOffset ( rect . height || 0 )
48
49
const positionTop = window .innerHeight - rect .top + offset
49
50
const positionBottom = rect .bottom + offset
50
51
if (positionTop > 0 && positionBottom > 0 ) {
51
52
! this .active ? this .load () : this .animate ()
52
53
}
53
54
},
55
+ getOffset (imageHeight ) {
56
+ const minimalOffset = - (window .innerHeight + imageHeight) / 2 + 50
57
+ return this .propOffset < minimalOffset ? minimalOffset : this .propOffset
58
+ },
54
59
load () {
55
60
this .active = true
56
- if (! this .fade ) {
61
+ if (this .noFade ) {
57
62
this .removeListener ()
63
+ } else if (this .fadeOffset >= this .loadOffset ) {
64
+ this .$nextTick (() => this .animate ())
58
65
}
59
66
},
60
67
animate () {
68
+ this .removeListener ()
61
69
this .$el .style .transition = ` opacity ${ this .fadeTime } ms ease-in-out`
62
70
this .animated = true
63
- this .removeListener ()
64
71
},
65
72
removeListener () {
66
73
return document .removeEventListener (' scroll' , this .checkPosition )
0 commit comments