使用vue做图片的闪光效果

思路:使用一个空标签,宽高和图片一样,使用background-image: -moz-linear-gradient()将空标签渲染为一个渐变色的图形,然后使用transform: skewX()使得图形倾斜,然后当鼠标移到图片上时,这个图形从图片上面平移过去;

<template>
  <div class="container">
    <div class="image">
      <img :src="img" alt="图片加载失败" />
      <div class="flash"></div>
    </div>
  </div>
</template>
export default {
  props: {
    img: {
      type: String,
      default: ""
    }
  }
}
.container {
  overflow: hidden;
  width: 100%;
  height: 100%;
  .image {
    width: 100%;
    height: 100%;
    position: relative;
    img {
      display: block;
      width: 100%;
      height: 100%;
    }
    .flash {
      position: absolute;
      width: 100%;
      height: 100%;
      left: -137%;
      top: 0;
      background-image: -moz-linear-gradient(
        0deg,
        rgba(255, 255, 255, 0),
        rgba(255, 255, 255, 0.5),
        rgba(255, 255, 255, 0)
      );
      background-image: -webkit-linear-gradient(
        0deg,
        rgba(255, 255, 255, 0),
        rgba(255, 255, 255, 0.5),
        rgba(255, 255, 255, 0)
      );
      transform: skewX(-25deg);
    }
    &:hover .flash {
      animation: flash 0.8s ease-in forwards;
    }
  }
}
@keyframes flash {
  0% {
    left: -137%;
  }
  100% {
    display: none;
    left: 137%;
  }
}

使用该组件的例子

<template>
  <div>
    <div style="width: 300px; height: 480px;padding-bottom: 50px;">
      <flash :img="flashImg"></flash>
    </div>
  </div>
</template>
import flash from './Flash/flash';
export default {
  components: { flash },
  data() {
    return {
      flashImg: require("../assets/flash.jpg")
    };
  },
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值