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

小球組件
html + css:
小球父元素:定義了一些基本樣式。采用 fixed 布局,讓小球相對瀏覽器窗口進行定位;通過 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ù)時間(不傳也有默認值)
data:通過 ball.show 來控制小球的 opacity
mounted:
小球當前位置通過變量 currentMousePos 來記錄,通過使用監(jiān)聽函數(shù) onmousemove 修改當前鼠標位置。
小球掛載時增加監(jiān)聽 onmousemove,使用 debounce 防抖函數(shù),保證 50ms 內只更新一次鼠標位置
核心方法 drop:開啟小球動畫
exportRecordsListNav:小球結束處的 dom 元素,直接通過 id 獲取了,用 ref 還需要跨組件獲取,覺得有些麻煩
主要流程:獲取結束元素的位置 -> 設置小球到初始位置 -> 設置結束位置 -> 動畫結束后小球隱藏、清除 transition 屬性
<script>
import debounce from 'lodash/debounce'
// 記錄小球當前位置、通過監(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 內只更新一次小球位置
window.addEventListener('mousemove', debounce((e) => {
currentMousePos.x = e.clientX
currentMousePos.y = e.clientY
}, 50))
},
methods: {
initBall(){
this.ball.show = 0
},
// 外部調用方法,開始執(zhí)行動畫
drop(){
// 獲取結束位置的元素及坐標
const exportRecordsListNav = document.getElementById('export-records-list')
const endPos = {}
endPos.x = exportRecordsListNav.getBoundingClientRect().left
endPos.y = exportRecordsListNav.getBoundingClientRect().top
// 小球顯示
this.ball.show = 1
// 設置小球初始位置
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`
// 設置小球結束位置
this.$refs.ball.style.transform = `translate(${endPos.x}px, ${endPos.y}px)`
// 動畫結束后,小球隱藏,清除動畫效果
// 清除動畫效果是為了下次小球從 (0,0) 移動到初始位置時不需要有動畫
setTimeout(()=>{
this.ball.show = 0
this.$refs.ball.style.transition = 'unset'
}, this.duration)
}, 100)
},
}
}
</script>
使用方式
我將結束元素和小球封裝成了一個組件,原因是認為工作項目中小球動畫只和該導航欄相關。
由于加入購物車的按鈕會在很多不同的單頁面 page 里,因此使用 Pubsub 技術告訴結束元素此刻點擊了按鈕,再由結束元素組件調用 drop 方法,這樣在其他頁面只需進行發(fā)布訂閱,不需要關注其他操作。
結束元素組件
<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>
點擊「加入購物車按鈕」的單頁面
<script>
import Pubsub from 'pubsub-js'
export default {
methods: {
// 點擊按鈕加入購物車
addToCard() {
// 發(fā)布消息
Pubsub.publish('add-to-card')
}
}
}
</script>
參考文檔: 仿加入購物車飛入動畫效果
以上就是使用JS實現(xiàn)點擊按鈕小球加入購物車動畫的詳細內容,更多關于JS購物車動畫的資料請關注腳本之家其它相關文章!
相關文章
vue使用axios實現(xiàn)excel文件下載的功能
這篇文章主要介紹了vue中使用axios實現(xiàn)excel文件下載的功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07
ant design vue動態(tài)循環(huán)生成表單以及自定義校驗規(guī)則詳解
這篇文章主要介紹了ant design vue動態(tài)循環(huán)生成表單以及自定義校驗規(guī)則詳解,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
Vue Transition實現(xiàn)類原生組件跳轉過渡動畫的示例
本篇文章主要介紹了Vue Transition實現(xiàn)類原生組件跳轉過渡動畫的示例,具有一定的參考價值,有興趣的可以了解一下2017-08-08
從0搭建Vue3組件庫如何使用?glup?打包組件庫并實現(xiàn)按需加載
這篇文章主要介紹了從0搭建Vue3組件庫如何使用?glup?打包組件庫并實現(xiàn)按需加載,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03
el-date-picker 選擇日期范圍只保存左側日期面板的實現(xiàn)代碼
接到這樣的需求,日期篩選,但限制只能選擇同一個月的數(shù)據(jù),故此應該去掉右側月份面板,今天通過本文給大家分享el-date-picker 選擇日期范圍只保存左側日期面板的實現(xiàn)代碼,感興趣的朋友一起看看吧2024-06-06

