使用Vue實現(xiàn)點擊按鈕小球加入購物車動畫
本文旨在實現(xiàn)類似點擊按鈕實現(xiàn)小球加入購物車效果。
使用技術(shù):
- 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 內(nèi)只更新一次鼠標位置
核心方法 drop:開啟小球動畫
exportRecordsListNav:小球結(jié)束處的 dom 元素,直接通過 id 獲取了,用 ref 還需要跨組件獲取,覺得有些麻煩
主要流程:獲取結(jié)束元素的位置 -> 設置小球到初始位置 -> 設置結(jié)束位置 -> 動畫結(jié)束后小球隱藏、清除 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 內(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é)束位置的元素及坐標
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`
// 設置小球結(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é)束元素和小球封裝成了一個組件,原因是認為工作項目中小球動畫只和該導航欄相關(guān)。
由于加入購物車的按鈕會在很多不同的單頁面 page 里,因此使用 Pubsub 技術(shù)告訴結(jié)束元素此刻點擊了按鈕,再由結(jié)束元素組件調(diào)用 drop 方法,這樣在其他頁面只需進行發(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>
點擊「加入購物車按鈕」的單頁面
<script>
import Pubsub from 'pubsub-js'
export default {
methods: {
// 點擊按鈕加入購物車
addToCard() {
// 發(fā)布消息
Pubsub.publish('add-to-card')
}
}
}
</script>
參考文檔: 仿加入購物車飛入動畫效果
以上就是使用JS實現(xiàn)點擊按鈕小球加入購物車動畫的詳細內(nèi)容,更多關(guān)于JS購物車動畫的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解vue使用vue-layer-mobile組件實現(xiàn)toast,loading效果
這篇文章主要介紹了詳解vue使用vue-layer-mobile組件實現(xiàn)toast,loading效果,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08
vue+bpmn.js實現(xiàn)自定義流程圖的完整代碼
這篇文章主要介紹了vue+bpmn.js實現(xiàn)自定義流程圖的完整代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借價值,需要的朋友參考下吧2024-03-03
vue2結(jié)合element-ui的gantt圖實現(xiàn)可拖拽甘特圖
因為工作中要用到甘特圖,所以我在網(wǎng)上搜索可以用的甘特圖,搜索了好多,但是網(wǎng)上搜到大多數(shù)都很雞肋,不能直接使用,下面這篇文章主要給大家介紹了關(guān)于vue2結(jié)合element-ui的gantt圖實現(xiàn)可拖拽甘特圖的相關(guān)資料,需要的朋友可以參考下2022-11-11
antd?select?多選限制個數(shù)的實現(xiàn)代碼
這篇文章主要介紹了antd?select?多選限制個數(shù),實現(xiàn)思路和核心代碼都很簡單,其中核心代碼在于disabled,代碼簡單易懂需要的朋友可以參考下2022-11-11
App開發(fā)框架NativeScript-Vue構(gòu)建原生應用(新手使用教程)
"Vue3寫真正的原生App" 一直是塊短板,uni-app雖然"一套代碼多端運行",但性能瓶頸、廠商鎖倉、原生能力常被開發(fā)者詬病,直到NativeScript-Vue出現(xiàn),NativeScript-Vue允許開發(fā)者利用JavaScript、TypeScript或Vue.js來訪問Android和iOS的原生API,實現(xiàn)跨平臺的原生應用開發(fā)2025-10-10

