利用vue组件实现一个渐进式图片

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>

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值