vue 动画 抖动效果_如果在vue中实现一个输入框的抖动效果?

本文介绍了如何在Vue中创建一个输入框抖动的动画效果。通过理解钟摆原理,定义初始状态、动画执行逻辑以及调整元素位置,实现了一个顺时针和逆时针交替的抖动动画。在动画过程中,当元素达到临界点后,会向相反方向摆动,最终在没有动力时逐渐停止。
摘要由CSDN通过智能技术生成

1. 先来理下思路?

1)抖动就是摆动,现实中的钟摆可以很形象。

2)当摆动到临界点后,就会向相反的方向摆动。

3)在没有动力时,摆动会慢慢停止。

2.用法:

ae3f704f91a3a03eef5f74e66c980f07.png

:start.sync 里面是抖动器名字(不同name), :range ={包含x,y, z}

3.初始化抖动

initJitter() {

// 把start变成false, 方便下次点击

this.$emit('update:start', false);

// 清除上次动画

this.clearAnimate();

// 设置currentRange, 填充this.range 中没有的项

this.currentRange = Object.assign({}, { x: 0, y: 0, z: 0 }, this.range);

// 获取需要操作的的项 和 每次需要摆动的量

const { position, shiftNumber } = this.getPositionAndShiftNumber();

this.position = position;

this.shiftNumber = shiftNumber;

// 初始 move 起始点是0

this.move = { x: 0, y: 0, z: 0 };

// 初始时 是顺时针

this.isClockwise = true;

// 执行动画

this.timer = window.requestAnimationFrame(this.continueJitter);

},

4.执行动画:

// 持续抖动

continueJitter() {

this.refreshMove(this.isClockwise ? -1 : 1);

// 绝对值

const absMove = this.getAbsMove();

const currentRange = this.currentRange;

let changeDirection = false;

for (let i = 0, l = this.position.length, p; i < l; i += 1) {

p = this.position[i];

// 判断是否到达临界值,到达后 应该反方向执行动画

if (currentRange[p] <= absMove[p]) {

// 等比例缩减

this.currentRange[p] -= this.shiftNumber[p];

// 判断如果已经无力再摆动,就让摆动停止, 只要有一个值达到了0,所有都会达到

if (this.currentRange[p] <= 0) {

// 停止在起始点上

this.jitterView({ x: 0, y: 0, z: 0 });

// 清除动画

this.clearAnimate();

return;

}

// 更新move为临界点

this.move[p] = this.isClockwise ? -this.currentRange[p] : this.currentRange[p];

// 改变摆动方向

changeDirection = true;

}

}

if (changeDirection) {

// 摆动方向取反

this.isClockwise = !this.isClockwise;

}

// 更新元素位置

this.jitterView(this.move);

// 继续执行动画

this.timer = window.requestAnimationFrame(this.continueJitter);

},

5.修改元素位置:

jitterView({ x = 0, y = 0, z = 0 }) {

this.$el.style.transform = translate3d(${x}px, ${y}px, ${z}px);

},

6.需要时,必须给当前元素的父级添加 perspective, 从而修改子级透视效果

mounted() {

// 如果要执行 z 轴动画需要设置父级,从而修改子级透视效果,否则 Z 轴没有效果

if (this.range.z > 0) {

const parentEl = this.$el.parentNode;

Object.keys(this.perspectiveStyle).forEach((key) => {

parentEl.style[key] = this.perspectiveStyle[key];

});

}

},

7.传入的属性

props: {

// 抖动范围,单位是px, 例如:{x: 4, y: 2, z: 10}

range: {

type: Object,

default: () => { return { z: 8 }; },

},

start: {

type: Boolean,

required: true,

},

shiftPercent: {

type: Number,

default: 0.1, // 移动range中初始值的百分比

},

perspectiveStyle: {

type: Object,

default: () => {

return {

perspective: '300px',

perspectiveOrigin: 'center center'

};

}

}

},

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue项目使用requestAnimationFrame实现动画效果的步骤如下: 1. 在Vue组件,创建一个方法来处理动画效果。例如,我们可以创建一个名为`animate`的方法。 2. 在`animate`方法,使用`requestAnimationFrame`函数来循环执行动画效果。在每一帧,你可以更新组件的数据或者操作DOM元素来实现动画效果。 3. 在`animate`方法,使用`this.$refs`来获取需要操作的DOM元素。例如,如果你想旋转一个3D模型,你可以使用`this.$refs`来获取模型的引用。 4. 在`animate`方法,使用`this.$nextTick`来确保DOM元素已经更新完毕。这样可以避免在更新DOM元素之前执行动画效果。 5. 在`animate`方法,使用`this.$refs`来更新DOM元素的样式或属性。例如,你可以使用`this.$refs.element.style.transform`来旋转一个元素。 6. 在`animate`方法,使用`requestAnimationFrame`函数来递归调用`animate`方法,以实现动画的连续播放。 下面是一个示例代码,演示了如何在Vue项目使用requestAnimationFrame实现动画效果: ```javascript <template> <div ref="element" class="box"></div> </template> <script> export default { mounted() { this.animate(); }, methods: { animate() { // 更新动画效果 // this.$refs.element.style.transform = 'rotate(45deg)'; // 更新数据 // this.data = 'new data'; // 执行其他操作 // 确保DOM元素已经更新完毕 this.$nextTick(() => { // 更新DOM元素的样式或属性 // this.$refs.element.style.transform = 'rotate(90deg)'; // 执行其他操作 // 递归调用animate方法,实现动画的连续播放 requestAnimationFrame(this.animate); }); } } } </script> <style> .box { width: 100px; height: 100px; background-color: red; } </style> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值