Vue 讓元素抖動(dòng)/擺動(dòng)起來的實(shí)現(xiàn)代碼
首先展示一下效果,狠狠點(diǎn)擊 https://zhangkunusergit.github.io/vue-component/dist/jitter.html
代碼github : https://github.com/zhangKunUserGit/vue-component
先說一下用法:
<jitter :start.sync="抖動(dòng)控制器" :range="{ 包含x,y, z }" :shift-percent="0.1">
這里是你要抖動(dòng)的元素
</jitter>
思路:
1.抖動(dòng)就是擺動(dòng),現(xiàn)實(shí)中的鐘擺可以很形象。
2.當(dāng)擺動(dòng)到臨界點(diǎn)后,就會(huì)向相反的方向擺動(dòng)。
3.在沒有動(dòng)力時(shí),擺動(dòng)會(huì)慢慢停止。
初始化抖動(dòng):
/**
* 初始化抖動(dòng)
*/
initJitter() {
// 把start變成false, 方便下次點(diǎn)擊
this.$emit('update:start', false);
// 清除上次動(dòng)畫
this.clearAnimate();
// 設(shè)置currentRange, 填充this.range 中沒有的項(xiàng)
this.currentRange = Object.assign({}, { x: 0, y: 0, z: 0 }, this.range);
// 獲取需要操作的的項(xiàng) 和 每次需要擺動(dòng)的量
const { position, shiftNumber } = this.getPositionAndShiftNumber();
this.position = position;
this.shiftNumber = shiftNumber;
// 初始 move 起始點(diǎn)是0
this.move = { x: 0, y: 0, z: 0 };
// 初始時(shí) 是順時(shí)針
this.isClockwise = true;
// 執(zhí)行動(dòng)畫
this.timer = window.requestAnimationFrame(this.continueJitter);
},
這里準(zhǔn)備動(dòng)畫開始前的工作。
執(zhí)行動(dòng)畫:
// 持續(xù)抖動(dòng)
continueJitter() {
this.refreshMove(this.isClockwise ? -1 : 1);
// 絕對(duì)值
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];
// 判斷是否到達(dá)臨界值,到達(dá)后 應(yīng)該反方向執(zhí)行動(dòng)畫
if (currentRange[p] <= absMove[p]) {
// 等比例縮減
this.currentRange[p] -= this.shiftNumber[p];
// 判斷如果已經(jīng)無力再擺動(dòng),就讓擺動(dòng)停止, 只要有一個(gè)值達(dá)到了0,所有都會(huì)達(dá)到
if (this.currentRange[p] <= 0) {
// 停止在起始點(diǎn)上
this.jitterView({ x: 0, y: 0, z: 0 });
// 清除動(dòng)畫
this.clearAnimate();
return;
}
// 更新move為臨界點(diǎn)
this.move[p] = this.isClockwise ? -this.currentRange[p] : this.currentRange[p];
// 改變擺動(dòng)方向
changeDirection = true;
}
}
if (changeDirection) {
// 擺動(dòng)方向取反
this.isClockwise = !this.isClockwise;
}
// 更新元素位置
this.jitterView(this.move);
// 繼續(xù)執(zhí)行動(dòng)畫
this.timer = window.requestAnimationFrame(this.continueJitter);
},
執(zhí)行動(dòng)畫,當(dāng)判斷已經(jīng)無力擺動(dòng)后,讓元素回歸到原來的位置,并清除動(dòng)畫。
修改元素位置:
/**
* 修改元素位置
* @param x
* @param y
* @param z
*/
jitterView({ x = 0, y = 0, z = 0 }) {
this.$el.style.transform = `translate3d(${x}px, ${y}px, ${z}px)`;
},
這里需要判斷需要 Z 軸擺動(dòng)嗎? 當(dāng)需要時(shí),必須給當(dāng)前元素的父級(jí)添加 perspective, 從而修改子級(jí)透視效果
mounted() {
// 如果要執(zhí)行 z 軸動(dòng)畫需要設(shè)置父級(jí),從而修改子級(jí)透視效果,否則 Z 軸沒有效果
if (this.range.z > 0) {
const parentEl = this.$el.parentNode;
Object.keys(this.perspectiveStyle).forEach((key) => {
parentEl.style[key] = this.perspectiveStyle[key];
});
}
},
最后看看可以傳的屬性:
props: {
// 抖動(dòng)范圍,單位是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, // 移動(dòng)range中初始值的百分比
},
perspectiveStyle: {
type: Object,
default: () => {
return {
perspective: '300px',
perspectiveOrigin: 'center center'
};
}
}
},
上面是可以傳的屬性,大家可以按照情況修改
最后:
這里我只寫了簡(jiǎn)單的動(dòng)畫,也可以根據(jù)不同情況進(jìn)行修改,從而達(dá)到想要的效果。這里已經(jīng)滿足輸入框錯(cuò)誤抖動(dòng)的效果了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于vue項(xiàng)目vue-cli-service啟動(dòng)報(bào)錯(cuò)失敗問題的解決方法
前端拉取代碼后,想要運(yùn)行代碼的時(shí)候可以能遇到啟動(dòng)報(bào)錯(cuò)的問題,這里我們只針對(duì)于vue-cli-service報(bào)錯(cuò)項(xiàng)來說,文中通過圖文結(jié)合的方式給大家介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2024-08-08
vue3+vite實(shí)現(xiàn)在線預(yù)覽pdf功能
這篇文章主要為大家詳細(xì)介紹了如何通過vue3和vite實(shí)現(xiàn)在線預(yù)覽pdf功能,文中的示例代碼簡(jiǎn)潔易懂,具有一定的借鑒價(jià)值,感興趣的小伙伴可以學(xué)習(xí)一下2023-10-10
默認(rèn)瀏覽器設(shè)置及vue自動(dòng)打開頁面的方法
今天小編就為大家分享一篇默認(rèn)瀏覽器設(shè)置及vue自動(dòng)打開頁面的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09
詳解vue配置請(qǐng)求多個(gè)服務(wù)端解決方案
這篇文章主要介紹了詳解vue配置請(qǐng)求多個(gè)服務(wù)端解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
koa2+vue實(shí)現(xiàn)登陸及登錄狀態(tài)判斷
這篇文章主要介紹了koa2+vue實(shí)現(xiàn)登陸及登錄狀態(tài)判斷,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-08-08
解決Vue3報(bào)錯(cuò):Property?“xxx“?was?accessed?during?render?but
這篇文章主要給大家介紹了關(guān)于解決Vue3報(bào)錯(cuò):Property?“xxx“?was?accessed?during?render?but?is?not?defined?on?instance.的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01
Vue3+Canvas實(shí)現(xiàn)坦克大戰(zhàn)游戲(二)
本文主要給大家講解一下子彈擊中物體、物體銷毀、敵方坦克構(gòu)建生成、運(yùn)動(dòng)算法、爆炸效果、以及障礙物的生成,感興趣的小伙伴可以了解一下2022-03-03

