欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Vue實(shí)現(xiàn)點(diǎn)擊按鈕小球加入購物車動(dòng)畫

 更新時(shí)間:2024年03月08日 08:48:34   作者:荔枝litchi  
這篇文章主要為大家詳細(xì)介紹了如何使用Vue實(shí)現(xiàn)點(diǎn)擊按鈕小球加入購物車動(dòng)畫,文中的示例代碼講解詳細(xì),有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下

本文旨在實(shí)現(xiàn)類似點(diǎn)擊按鈕實(shí)現(xiàn)小球加入購物車效果。

使用技術(shù):

  • Vue2
  • 使用 Pubsub 監(jiān)聽按鈕點(diǎn)擊事件(如果不想用也可以自己改造下)
  • 監(jiān)聽 onmousemove 來獲取按鈕點(diǎn)擊時(shí)的鼠標(biāo)位置

小球組件

html + css:

小球父元素:定義了一些基本樣式。采用 fixed 布局,讓小球相對(duì)瀏覽器窗口進(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:控制小球大小、動(dòng)畫持續(xù)時(shí)間(不傳也有默認(rèn)值)

data:通過 ball.show 來控制小球的 opacity

mounted:

小球當(dāng)前位置通過變量 currentMousePos 來記錄,通過使用監(jiān)聽函數(shù) onmousemove 修改當(dāng)前鼠標(biāo)位置。

小球掛載時(shí)增加監(jiān)聽 onmousemove,使用 debounce 防抖函數(shù),保證 50ms 內(nèi)只更新一次鼠標(biāo)位置

核心方法 drop:開啟小球動(dòng)畫

exportRecordsListNav:小球結(jié)束處的 dom 元素,直接通過 id 獲取了,用 ref 還需要跨組件獲取,覺得有些麻煩

主要流程:獲取結(jié)束元素的位置 -> 設(shè)置小球到初始位置 -> 設(shè)置結(jié)束位置 -> 動(dòng)畫結(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ù)時(shí)間
      duration: {
        type: Number,
        default: 1000
      },
    },
    data() {
      return {
        ball: {
          show: 0,
        },
      };
    },
    mounted() {
      // 初始化小球,控制小球顯隱
      this.initBall()
      // 小球掛載時(shí)監(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í)行動(dòng)畫
      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)`
      
        // 延時(shí)是為了防止合并移動(dòng)
        setTimeout(() => {
           // 增加動(dòng)畫效果
          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)`
          
          // 動(dòng)畫結(jié)束后,小球隱藏,清除動(dòng)畫效果
          // 清除動(dòng)畫效果是為了下次小球從 (0,0) 移動(dòng)到初始位置時(shí)不需要有動(dòng)畫
          setTimeout(()=>{
            this.ball.show = 0
            this.$refs.ball.style.transition = 'unset'
          }, this.duration)
        }, 100)
      },
    }
  }
 </script>

使用方式

我將結(jié)束元素和小球封裝成了一個(gè)組件,原因是認(rèn)為工作項(xiàng)目中小球動(dòng)畫只和該導(dǎo)航欄相關(guān)。

由于加入購物車的按鈕會(huì)在很多不同的單頁面 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) {
        // 開啟小球動(dòng)畫
        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>

參考文檔: 仿加入購物車飛入動(dòng)畫效果

以上就是使用JS實(shí)現(xiàn)點(diǎn)擊按鈕小球加入購物車動(dòng)畫的詳細(xì)內(nèi)容,更多關(guān)于JS購物車動(dòng)畫的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論