Skip to content

Commit a5a7747

Browse files
committed
refactor: CImageLazy: add loadInitially prop, refactor props, fix offset
1 parent 262c9f5 commit a5a7747

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

src/components/Image/CImageLazy.vue

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,25 @@ export default {
44
name: 'CImageLazy',
55
extends: CImage,
66
props: {
7+
loadInitially: Boolean,
78
loadOffset: {
8-
type: [Number, String, Boolean],
9+
type: Number,
910
default: 500
1011
},
11-
fade: {
12-
type: Boolean,
13-
default: true
14-
},
12+
noFade: Boolean,
1513
fadeOffset: {
16-
type: [Number, String, Boolean],
14+
type: Number,
1715
default: -100
1816
},
1917
fadeTime: {
20-
type: [Number, String],
18+
type: Number,
2119
default: 1500
2220
}
2321
},
2422
data () {
2523
return {
26-
active: this.loadOffset === false,
27-
animated: this.fadeOffset === false
24+
active: this.loadInitially,
25+
animated: this.noFade
2826
}
2927
},
3028
mounted () {
@@ -36,31 +34,40 @@ export default {
3634
},
3735
computed: {
3836
animationClasses () {
39-
if (this.fade) {
37+
if (!this.noFade) {
4038
return { 'c-opacity-0' : !this.animated }
4139
}
40+
},
41+
propOffset () {
42+
return !this.active ? this.loadOffset : this.fadeOffset
4243
}
4344
},
4445
methods: {
4546
checkPosition () {
4647
const rect = this.$el.getBoundingClientRect()
47-
const offset = !this.active ? this.loadOffset : this.fadeOffset
48+
const offset = this.getOffset(rect.height || 0)
4849
const positionTop = window.innerHeight - rect.top + offset
4950
const positionBottom = rect.bottom + offset
5051
if (positionTop > 0 && positionBottom > 0) {
5152
!this.active ? this.load() : this.animate()
5253
}
5354
},
55+
getOffset (imageHeight) {
56+
const minimalOffset = - (window.innerHeight + imageHeight) / 2 + 50
57+
return this.propOffset < minimalOffset ? minimalOffset : this.propOffset
58+
},
5459
load () {
5560
this.active = true
56-
if (!this.fade) {
61+
if (this.noFade) {
5762
this.removeListener()
63+
} else if (this.fadeOffset >= this.loadOffset) {
64+
this.$nextTick(() => this.animate())
5865
}
5966
},
6067
animate () {
68+
this.removeListener()
6169
this.$el.style.transition = `opacity ${this.fadeTime}ms ease-in-out`
6270
this.animated = true
63-
this.removeListener()
6471
},
6572
removeListener () {
6673
return document.removeEventListener('scroll', this.checkPosition)

0 commit comments

Comments
 (0)