溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Vue怎么實現(xiàn)波紋按鈕效果

發(fā)布時間:2022-11-19 10:05:47 來源:億速云 閱讀:164 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹了Vue怎么實現(xiàn)波紋按鈕效果的相關(guān)知識,內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇Vue怎么實現(xiàn)波紋按鈕效果文章都會有所收獲,下面我們一起來看看吧。

先說一下用法:

<zk-button class="btn btn-default">默認(rèn)按鈕</zk-button>
<zk-button class="btn btn-default btn-round">默認(rèn)按鈕</zk-button>
<zk-button class="btn btn-default btn-round" :speed="4" :opacity="0.6">定義速度和初始的波浪透明度</zk-button>

原理:

這里用的是canvas + requestAnimationFrame(兼容性可以網(wǎng)上找一下解決方法) 繪制的波紋,有些用的是css transform + setTimeout做的,我感覺不太好。

模板(template):

<template>
 <button class="zk-btn">
  <canvas class="zk-ripple" @click="ripple"></canvas>
  <slot></slot>
 </button>
</template>

點擊代碼如下(我已經(jīng)加入詳細(xì)的注釋)

ripple(event) {
 // 清除上次沒有執(zhí)行的動畫
 if (this.timer) {
  window.cancelAnimationFrame(this.timer);
 }
 this.el = event.target;
 // 執(zhí)行初始化
 if (!this.initialized) {
  this.initialized = true;
  this.init(this.el);
 }
 this.radius = 0;
 // 點擊坐標(biāo)原點
 this.origin.x = event.offsetX;
 this.origin.y = event.offsetY;
 this.context.clearRect(0, 0, this.el.width, this.el.height);
 this.el.style.opacity = this.opacity;
 this.draw();
},

這里主要初始化canvas和獲取用戶點擊的位置坐標(biāo),并開始繪制。

循環(huán)繪制

draw() {
 this.context.beginPath();
 // 繪制波紋
 this.context.arc(this.origin.x, this.origin.y, this.radius, 0, 2 * Math.PI, false);
 this.context.fillStyle = this.color;
 this.context.fill();
 // 定義下次的繪制半徑和透明度
 this.radius += this.speed;
 this.el.style.opacity -= this.speedOpacity;
 // 通過判斷半徑小于元素寬度或者還有透明度,不斷繪制圓形
 if (this.radius < this.el.width || this.el.style.opacity > 0) {
  this.timer = window.requestAnimationFrame(this.draw);
 } else {
  // 清除畫布
  this.context.clearRect(0, 0, this.el.width, this.el.height);
  this.el.style.opacity = 0;
 }
}

Vue的優(yōu)點

Vue具體輕量級框架、簡單易學(xué)、雙向數(shù)據(jù)綁定、組件化、數(shù)據(jù)和結(jié)構(gòu)的分離、虛擬DOM、運行速度快等優(yōu)勢,Vue中頁面使用的是局部刷新,不用每次跳轉(zhuǎn)頁面都要請求所有數(shù)據(jù)和dom,可以大大提升訪問速度和用戶體驗。

關(guān)于“Vue怎么實現(xiàn)波紋按鈕效果”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“Vue怎么實現(xiàn)波紋按鈕效果”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

vue
AI