1.效果
2.思路
首先,完成这个组件必须拥有的条件有两个,一个是原始图片,一个是占位图片,还有一个可选条件就是原始图片加载完成后切换到原始图片经过的秒数
接下来就是想他的实现思路:在原始图片没加载成功的时候需要展示一个占位图片,当原始图片加载成功后占位图片消失,这里面就涉及到css当中的opacity来设置原始图片的透明度,并且利用transition实现从占位图片到原始图片的平滑过渡
3.代码拆解
html部分,利用v-if来判断占位图片是否展示,并且利用img的@load来定义一个方法判断原始图片是否加载完成,并且用内联样式来处理图片的不透明度确保可以展示成渐进式图片
<div class="image-loader-container">
<img v-if="!everythingDone" class="placeholder" :src="placeholder" alt="" />
<img
@load="handleLoad"
:src="src"
alt=""
:style="{ opacity: originOpacity,transition: `${duration}ms` }"
/>
</div>
数据部分要有两个,一个是原图是否加载完成,还有一个是当原图加载完成
data() {
return {
orginLoaded: false, //原图是否加载完成
everythingDone: false, // 是否一切都尘埃落定了
};
},
利用计算属性来判断原始图片是否加载完,若是加载完不透明度设置为0,否则不透明度设置成1
computed: {
originOpacity() {
return this.orginLoaded ? 1 : 0;
},
},
方法部分,通过这个方法来设置图片加载完成
methods: {
handleLoad() {
console.log("原图加载完成");
this.orginLoaded = true
setTimeout(() => {
this.everythingDone = true;
this.$emit("load");
}, this.duration);
},
},
css代码,需要注意的是图片撑满整个容器可能会出现图片变形的情况,就需要用到object-fit这个属性,既然占位图片是模糊的,我们也可以利用filter属性为整个图片添加一层滤镜
.image-loader-container {
width: 100%;
height: 100%;
position: relative;
overflow: hidden;
img {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
object-fit: cover;
}
.placeholder {
filter: blur(2vw);
}
}
4.完整代码
子组件
<template>
<div class="image-loader-container">
<img v-if="!everythingDone" class="placeholder" :src="placeholder" alt="" />
<img
@load="handleLoad"
:src="src"
alt=""
:style="{ opacity: originOpacity,transition: `${duration}ms` }"
/>
</div>
</template>
<script>
export default {
name: "MySiteImageLoader",
props: {
// 原始图片路径
src: {
type: String,
required: true,
},
// 原始图片加载完成前的占位图片
placeholder: {
type: String,
required: true,
},
// 原始图片加载完成后切换到原始图片经过的秒数
duration: {
type: Number,
default: 500,
},
},
data() {
return {
orginLoaded: false, //原图是否加载完成
everythingDone: false, // 是否一切都尘埃落定了
};
},
computed: {
originOpacity() {
return this.orginLoaded ? 1 : 0;
},
},
mounted() {},
methods: {
handleLoad() {
console.log("原图加载完成");
this.orginLoaded = true
setTimeout(() => {
this.everythingDone = true;
this.$emit("load");
}, this.duration);
},
},
};
</script>
<style lang="less" scoped>
.image-loader-container {
width: 100%;
height: 100%;
position: relative;
overflow: hidden;
img {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
object-fit: cover;
}
.placeholder {
filter: blur(2vw);
}
}
</style>
父组件
<template>
<div class="box">
<ImageLoader
src="https://images.pexels.com/photos/33109/fall-autumn-red-season.jpg?fit=crop&crop=entropy&w=3456&h=2304"
placeholder="https://images.pexels.com/photos/33109/fall-autumn-red-season.jpg?w=100"
@load="handleLoaded"
:duration="1000"
/>
</div>
</template>
<script>
import ImageLoader from "./components/ImageLoader.vue";
export default {
name: "MySiteApp",
components: {
ImageLoader
},
data() {
return {
current:5,
total:300
};
},
mounted() {
},
methods: {
handleLoaded(){
console.log('...');
}
},
};
</script>
<style scoped>
.box{
width: 500px;
height: 400px;
border: 2px solid ;
position: relative;
}
</style>