CSS左右摆动动效

 

基础图片,是由一个手部图和一张箭头图而成

代码开箱即用

<template>
  <div class="canvas_box" @click="hanldShow">
    <div class="hander-icon">
      <div class="shou" @animationend="hanldShow"></div>
    </div>
  </div>
</template>

<script>
let timer = null;

let backgroundBlock = 0; // 当前播放进度
let positionTime = null; // 控制动画

export default {
  data() {
    return {
      point: 0,
      isLoading: true,
      height: 0,
      maxPoint: 4,
      isShow: true,
      position: {
        x: 0,
        y: 0,
      },
    };
  },
  mounted() {
    for (let i = 0; i < this.maxPoint; i++) {
      // setTimeout(() => {
      this.move(i);
      // }, 100);
    }
  },
  methods: {
    hanldShow() {
      this.isShow = false;
    },
    move(point) {
      if (point === this.point) return;
      this.point = point;
      const target = point * 20 - 1;
      this.movePosition(target > 0 ? target : 0);
    },

    movePosition(target) {
      const height = 240;
      let animation = () => {
        const tag = target > backgroundBlock ? -1 : 1;
        this.position.y += tag * height;
        backgroundBlock += -tag * 1;
        clearTimeout(positionTime);
        if (Math.abs(this.position.y + height * target) > 1) {
          positionTime = setTimeout(animation, 60);
        }
      };
      clearTimeout(positionTime);
      positionTime = setTimeout(animation, 10);
    },

    handleStopTimer() {
      clearInterval(timer);
      timer = null;
    },
  },
};
</script>

<style lang="scss" scoped>
.hander-icon {
  width: r(750);
  height: r(680);
  border: 1px solid red;
  // background: url('https://.png') no-repeat
  //   center center;
  // background-size: 100% auto;
  display: flex;
  justify-content: center;
  align-items: center;
  .shou {
    width: r(200);
    height: r(200);
    animation: shou 1.5s linear 120 forwards;
    transform-origin: center center;
    border: 1px solid;
    // background: url('https://.png') no-repeat center center / 100% auto;
  }
}
@keyframes shou {
  25%,
  75% {
    transform: translateX(0) rotate(35deg);
  }
  50% {
    transform: translateX(-30%) rotate(20deg);
  }
  0%,
  100% {
    transform: translateX(30%) rotate(50deg);
  }
}
.canvas_box {
  // position: relative;
  width: r(750);
  height: r(750);
  // background: linear-gradient(to bottom, #fbf0dd, #f5e5cc);
  border-radius: r(8);
  box-sizing: border-box;
  position: absolute;
  z-index: 10020;
  border: 1px solid red;
  background-color: navajowhite;
}
</style>

 

 

### 使用 CSS 创建微风徐徐的动态效果 通过 CSS 的 `@keyframes` 和画属性可以轻松实现模拟自然界的微风果。以下是具体的方法: #### 关键技术点 1. **使用 @keyframes 定义画帧** 利用 `@keyframes` 可以定义一组关键帧来控制元素的变化过程,比如位移、透明度变化或者旋转角度等[^1]。 2. **应用 animation 属性** 将定义好的画绑定到目标元素上,并设置其持续时间、延迟时间和循环模式等参数[^2]。 3. **结合 transform 实现平滑运** 微风的果通常表现为轻微的位置偏移或缩放变换,这可以通过 `transform: translateX()` 或者其他变形函数完成[^3]。 #### 示例代码 下面是一个简单的例子展示如何让一个叶子图标随风摇曳: ```css /* 定义名为 &#39;blowing&#39; 的 keyframe 画 */ @keyframes blowing { 0% { transform: translateY(0px); /* 起始位置无移 */ } 50% { transform: translateY(-10px) rotate(-5deg); /* 向上飘并稍微倾斜 */ } 100% { transform: translateY(0px); /* 返回原点 */ } } /* 应用于某个类 .leaf 上 */ .leaf { width: 50px; height: 80px; background-image: url(&#39;leaf.png&#39;); /* 假设有一个树叶图片 */ background-size: cover; /* 设置画特性 */ animation-name: blowing; /* 指定使用的画名称 */ animation-duration: 4s; /* 单次播放时间为4秒 */ animation-timing-function: ease-in-out; /* 平缓加速减速 */ animation-iteration-count: infinite; /* 无限重复 */ } ``` 此段代码会使得带有 `.leaf` 类别的 HTML 元素按照设定的时间周期不断上下摆动以及略微扭转,从而营造出被风吹拂的感觉[^4]。 #### 进阶技巧 为了增加真实感还可以加入更多细节调整,例如随机化不同对象的速度和方向,或是叠加多个层叠的小幅度震等等[^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值