js实现动画效果基础

<template>
  <div>
    <div class="video-water" ref="video-water">
      <div class="container-video" ref="container"></div>
    </div>
  </div>
</template>

<script>

export default {
  data() {
    return {
      imgList: [],
      showImgIndex: 0
    }
  },

  mounted() {
    this.imgList = this.dataList

    // this.getInfo()
    this.goAnimation()
  },

  computed: {
    dataList() {
      return  [
        'https://img-s-msn-com.akamaized.net/tenant/amp/entityid/BB1oubUP.img',
        'https://img-s-msn-com.akamaized.net/tenant/amp/entityid/BB1ou9BV.img',
        'https://img-s-msn-com.akamaized.net/tenant/amp/entityid/BB1ou4Zo.img',
        'https://img-s-msn-com.akamaized.net/tenant/amp/entityid/BB1ou0xY.img',
        'https://img-s-msn-com.akamaized.net/tenant/amp/entityid/BB1oubV4.img',
        'https://img-s-msn-com.akamaized.net/tenant/amp/entityid/BB1oueqf.img',
        'https://img-s-msn-com.akamaized.net/tenant/amp/entityid/BB1oueqi.img',
        'https://img-s-msn-com.akamaized.net/tenant/amp/entityid/BB1ou0y1.img',
      ]
    }
  },




  methods: {
    goAnimation() {
      if (!this.imgList.length) return this.$emit("openAllWork");
      this.getInfo();
    },
    getInfo() {
      const randomIndex = Math.floor(Math.random() * this.imgList.length);
      const spliceItem = this.imgList[randomIndex];
      this.imgList.splice(randomIndex, 1);
      this.imgList.forEach((item,) => {
        const img = document.createElement("img");
        img.src = item;
        img.className = `v-waterfall-item`;
        this.$refs["container"].appendChild(img);
        this.handleContainer(img);
      });
      // 中间展示图片
      this.handleCenterImg(spliceItem);
    },

    handleCenterImg(spliceItem) {
      const img = document.createElement("img");
      img.src = spliceItem;
      img.className = `v-waterfall-item`;
      this.$refs["container"].appendChild(img);
      this.handleContainer(img, true);
      this.centerImage = img;
    },

    async handleContainer(img, isScale=false) {
      // img.style.cursor = "pointer";

      const videoWater = this.$refs["video-water"];
      // 创建元素的宽高等
      let origin = {
        X: 0,
        Y: 0,
        scale: 1,
        opacity: 0,
        width: videoWater.offsetWidth * 0.05,
        height: videoWater.offsetWidth * 0.05 * 0.6,
      };
      // x移动范围
      const Xorigin = videoWater.offsetWidth / 2 - origin.width;
      const cw = origin.width * 3;
      const Yorigin = videoWater.offsetHeight / 2 - origin.height;
      const ch = origin.height * 2;
      // 正方向
      const ZXSize = Math.round(Math.random() * (Xorigin - cw) + cw);
      // y移动范围// 正方向
      const ZYSize = Math.round(Math.random() * (Yorigin - ch) + ch);
      console.log(`
      大盒子半径宽:${videoWater.offsetWidth / 2},高:${
          videoWater.offsetHeight / 2
      },
      小盒子宽:${origin.width}, 高:${origin.height}
        正x轴max:${Xorigin},min:${cw},
        正y轴max:${Yorigin},min:${ch},
        反x轴max:${Xorigin},min:${-cw},
        反y轴max:${Yorigin},min:${-ch},
        `);
      const azimuthType = {
        0: {
          x: -1,
          y: 1,
        },
        1: {
          x: 1,
          y: 1,
        },
        2: {
          x: 1,
          y: -1,
        },
        3: {
          x: -1,
          y: -1,
        },
      };
      const azimuthRandom = Math.floor(Math.random() * 4);
      const XSize = azimuthType[azimuthRandom].x * ZXSize;
      const YSize = azimuthType[azimuthRandom].y * ZYSize;
      let timer = await setInterval(() => {
        let isReturnX = XSize > 0 ? origin.X > XSize : origin.X < XSize;
        let isReturnY = YSize > 0 ? origin.Y > YSize : origin.Y < YSize;

        if (isReturnX || isReturnY || (isScale && origin.scale >= 2)) {
          img.style.left = isScale ? 0 : XSize + "px";
          img.style.top = isScale ? 0 : YSize + "px";
          img.style.opacity = 1;
          img.style.scale = isScale ? '3': '1';
          img.style.zIndex = isScale ? 999 : "";
          return clearInterval(timer);
        } // 2 秒后结束动画
        origin = {
          X: XSize > 0 ? origin.X + 1 : origin.X - 1,
          Y: YSize > 0 ? origin.Y + 1 : origin.Y - 1,
          opacity: origin.opacity === 1 ? 1 : origin.opacity + 0.1,
          scale: origin.scale === 3 ? 3 : origin.scale + 0.1,
          width: origin.width,
          height: origin.height,
        };
        origin.X = isScale ? 0 : origin.X;
        origin.Y = isScale ? 0 : origin.Y;
        img.style.opacity = origin.opacity;
        img.style.scale = isScale ? origin.scale : 1;
        img.style.left = origin.X + "px";
        img.style.top = origin.Y + "px";
      }, 1);
      img.addEventListener("click", (e) => {
        const transfromUrl = this.centerImage.src;
        this.centerImage.src = e.target.src;
        e.target.src = transfromUrl;

        this.$refs["container"].removeChild(
            this.$refs["container"].children[
            this.$refs["container"].children.length - 1
                ]
        );
        this.handleCenterImg(this.centerImage.src);
      });
    },
  }
}
</script>

<style scoped>

.video-water {
  display: flex;
  justify-content: center;
  align-items: center;
  //background-color: #000;
  min-height: 600px;
  overflow: hidden;

  .container-video {
    position: relative;
    .v-waterfall-item {
      position: absolute;
      width: 100px;
      height: 60px;
      opacity: 0;
      transition-duration: 3s;
      cursor: pointer;
      object-fit: contain;
    }
  }
}
</style>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值