使用Vue實(shí)現(xiàn)點(diǎn)擊按鈕小球加入購物車動畫
本文旨在實(shí)現(xiàn)類似點(diǎn)擊按鈕實(shí)現(xiàn)小球加入購物車效果。
使用技術(shù):
- Vue2
- 使用 Pubsub 監(jiān)聽按鈕點(diǎn)擊事件(如果不想用也可以自己改造下)
- 監(jiān)聽 onmousemove 來獲取按鈕點(diǎn)擊時的鼠標(biāo)位置

小球組件
html + css:
小球父元素:定義了一些基本樣式。采用 fixed 布局,讓小球相對瀏覽器窗口進(jìn)行定位;通過 opacity 控制顯隱。
小球:采用任意圖片。
<template>
<div class="ball-wrap"
ref="ball"
:style="{
opacity: ball.show,
width: size + 'px',
height: size + 'px',
}"
>
<i class="el-icon-document" ></i>
</div>
</template>
<style scoped>
.ball-wrap {
border-radius: 50%;
z-index: 9999;
position: fixed;
top: 0;
left: 0;
display: flex;
align-items: center;
justify-content: center;
background-color: #165BD3;
}
.el-icon-document {
color: #fff !important;
margin: 0 !important;
}
</style>
js:
props:控制小球大小、動畫持續(xù)時間(不傳也有默認(rèn)值)
data:通過 ball.show 來控制小球的 opacity
mounted:
小球當(dāng)前位置通過變量 currentMousePos 來記錄,通過使用監(jiān)聽函數(shù) onmousemove 修改當(dāng)前鼠標(biāo)位置。
小球掛載時增加監(jiān)聽 onmousemove,使用 debounce 防抖函數(shù),保證 50ms 內(nèi)只更新一次鼠標(biāo)位置
核心方法 drop:開啟小球動畫
exportRecordsListNav:小球結(jié)束處的 dom 元素,直接通過 id 獲取了,用 ref 還需要跨組件獲取,覺得有些麻煩
主要流程:獲取結(jié)束元素的位置 -> 設(shè)置小球到初始位置 -> 設(shè)置結(jié)束位置 -> 動畫結(jié)束后小球隱藏、清除 transition 屬性
<script>
import debounce from 'lodash/debounce'
// 記錄小球當(dāng)前位置、通過監(jiān)聽 onmousemove 來更新小球位置
const currentMousePos = {
x: 0,
y: 0
}
export default {
props: {
// 球的大小
size: {
type: Number,
default: 30
},
//持續(xù)時間
duration: {
type: Number,
default: 1000
},
},
data() {
return {
ball: {
show: 0,
},
};
},
mounted() {
// 初始化小球,控制小球顯隱
this.initBall()
// 小球掛載時監(jiān)聽 onmousemove,使用 debounce 保證 50ms 內(nèi)只更新一次小球位置
window.addEventListener('mousemove', debounce((e) => {
currentMousePos.x = e.clientX
currentMousePos.y = e.clientY
}, 50))
},
methods: {
initBall(){
this.ball.show = 0
},
// 外部調(diào)用方法,開始執(zhí)行動畫
drop(){
// 獲取結(jié)束位置的元素及坐標(biāo)
const exportRecordsListNav = document.getElementById('export-records-list')
const endPos = {}
endPos.x = exportRecordsListNav.getBoundingClientRect().left
endPos.y = exportRecordsListNav.getBoundingClientRect().top
// 小球顯示
this.ball.show = 1
// 設(shè)置小球初始位置
this.$refs.ball.style.transform = `translate(${currentMousePos.x}px, ${currentMousePos.y}px)`
// 延時是為了防止合并移動
setTimeout(() => {
// 增加動畫效果
this.$refs.ball.style.transition = `transform ${this.duration}ms ease-in-out`
// 設(shè)置小球結(jié)束位置
this.$refs.ball.style.transform = `translate(${endPos.x}px, ${endPos.y}px)`
// 動畫結(jié)束后,小球隱藏,清除動畫效果
// 清除動畫效果是為了下次小球從 (0,0) 移動到初始位置時不需要有動畫
setTimeout(()=>{
this.ball.show = 0
this.$refs.ball.style.transition = 'unset'
}, this.duration)
}, 100)
},
}
}
</script>
使用方式
我將結(jié)束元素和小球封裝成了一個組件,原因是認(rèn)為工作項(xiàng)目中小球動畫只和該導(dǎo)航欄相關(guān)。
由于加入購物車的按鈕會在很多不同的單頁面 page 里,因此使用 Pubsub 技術(shù)告訴結(jié)束元素此刻點(diǎn)擊了按鈕,再由結(jié)束元素組件調(diào)用 drop 方法,這樣在其他頁面只需進(jìn)行發(fā)布訂閱,不需要關(guān)注其他操作。
結(jié)束元素組件
<template>
<div>
<span id="export-records-list">購物車</span>
<MovableBall ref="movableBallRef"/>
</div>
</template>
<script>
import MovableBall from '@/components/movable-ball/index.vue'
import Pubsub from 'pubsub-js'
export default {
data () {},
components: {
MovableBall,
},
mounted () {
// 訂閱消息、接受到消息后執(zhí)行 moveBall 方法
Pubsub.subscribe('add-to-card', this.moveBall)
},
methods: {
moveBall() {
if(this.$refs.movableBallRef) {
// 開啟小球動畫
this.$refs.movableBallRef.drop()
}
},
},
}
</script>
點(diǎn)擊「加入購物車按鈕」的單頁面
<script>
import Pubsub from 'pubsub-js'
export default {
methods: {
// 點(diǎn)擊按鈕加入購物車
addToCard() {
// 發(fā)布消息
Pubsub.publish('add-to-card')
}
}
}
</script>
參考文檔: 仿加入購物車飛入動畫效果
以上就是使用JS實(shí)現(xiàn)點(diǎn)擊按鈕小球加入購物車動畫的詳細(xì)內(nèi)容,更多關(guān)于JS購物車動畫的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue使用axios實(shí)現(xiàn)excel文件下載的功能
這篇文章主要介紹了vue中使用axios實(shí)現(xiàn)excel文件下載的功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07
vue3 可拖動的左右面板分割組件實(shí)現(xiàn)
最近在使用vue的時候,遇到一個需求,實(shí)現(xiàn)左右div可通過中間部分拖拽調(diào)整寬度,本文就整理一下,分享給大家,有興趣的可以學(xué)習(xí)2021-06-06
Vue中img的src屬性綁定與static文件夾實(shí)例
本篇文章中主要介紹了Vue中img的src屬性綁定與static文件夾實(shí)例,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-05-05
ant design vue動態(tài)循環(huán)生成表單以及自定義校驗(yàn)規(guī)則詳解
這篇文章主要介紹了ant design vue動態(tài)循環(huán)生成表單以及自定義校驗(yàn)規(guī)則詳解,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
Vue Transition實(shí)現(xiàn)類原生組件跳轉(zhuǎn)過渡動畫的示例
本篇文章主要介紹了Vue Transition實(shí)現(xiàn)類原生組件跳轉(zhuǎn)過渡動畫的示例,具有一定的參考價值,有興趣的可以了解一下2017-08-08
從0搭建Vue3組件庫如何使用?glup?打包組件庫并實(shí)現(xiàn)按需加載
這篇文章主要介紹了從0搭建Vue3組件庫如何使用?glup?打包組件庫并實(shí)現(xiàn)按需加載,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03
有關(guān)vue 組件切換,動態(tài)組件,組件緩存
這篇文章主要介紹了有關(guān)vue 組件切換,動態(tài)組件,組件緩存,在組件化開發(fā)模式下,我們會把整個項(xiàng)目拆分成很多組件,然后按照合理的方式組織起來,達(dá)到預(yù)期效果,下面來看看文章的詳細(xì)內(nèi)容2021-11-11
el-date-picker 選擇日期范圍只保存左側(cè)日期面板的實(shí)現(xiàn)代碼
接到這樣的需求,日期篩選,但限制只能選擇同一個月的數(shù)據(jù),故此應(yīng)該去掉右側(cè)月份面板,今天通過本文給大家分享el-date-picker 選擇日期范圍只保存左側(cè)日期面板的實(shí)現(xiàn)代碼,感興趣的朋友一起看看吧2024-06-06

