image swap logic implemented

This commit is contained in:
ManjaroOne666 2019-01-22 12:54:21 +00:00
parent 2015797c71
commit 2eac4588d9
1 changed files with 59 additions and 19 deletions

View File

@ -1,10 +1,16 @@
<template> <template>
<div class="background-image-loader"> <div class="background-image-loader">
<div class="background-container"> <div class="background-container">
<div class="background background-loading" <div class="background background-default"
:style="loadingStyle"></div> :style="backgroundDefaultStyle">
<div class="background background-img" </div>
:style="backgroundStyle"></div> <transition name="fade">
<div class="background background-img"
:key="loadedImageUrl"
:style="backgroundStyle"
>
</div>
</transition>
</div> </div>
</div> </div>
</template> </template>
@ -16,42 +22,62 @@ export default {
mixins: [ imageLoader ], mixins: [ imageLoader ],
props: { props: {
bgLoading: { imageUrl: {
type: String, type: String,
required: false, required: false,
default: function () { default: function () {
return 'linear-gradient(310deg, #35405d, #6174aa, #a8b3d0, #e2e5ef)' return null
}
},
img: {
type: String,
required: false,
default: function () {
return 'url(https://via.placeholder.com/1920x1080)'
} }
}, },
}, },
data () { data () {
return { return {
loadedImageUrl: null,
} }
}, },
computed: { computed: {
loadingStyle () { backgroundDefaultStyle () {
return { return {
background: this.bgLoading background: 'linear-gradient(310deg, #35405d, #6174aa, #a8b3d0, #e2e5ef)'
} }
}, },
backgroundStyle () { backgroundStyle () {
return { let style = {}
backgroundImage: this.img if (this.loadedImageUrl) {
style.backgroundImage = `url(${this.loadedImageUrl})`
} }
return style
} }
}, },
watch: {
imageUrl () {
if (this.imageUrl) {
this.setImage(this.imageUrl)
}
},
},
mounted () {
this.setImage(this.imageUrl)
},
methods: { methods: {
} setImage(url) {
if (url === null) { return }
this.loadImage(url)
.then(img => {
this.loadedImageUrl = img.src
this.$emit('imageLoaded', img)
})
.catch(err => {
this.$emit('imageLoadError', err)
})
},
},
} }
</script> </script>
@ -74,6 +100,20 @@ export default {
.background-img { .background-img {
background-size: cover; background-size: cover;
background-position: center center; background-position: center center;
opacity: .5; opacity: 1;
}
.fade {
&-enter-active {
transition: opacity 2s 1s;
}
&-leave-active {
transition: opacity 3s;
}
&-enter, &-leave-to {
opacity: 0;
}
} }
</style> </style>