溫馨提示×

溫馨提示×

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

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

Vue如何制作波紋按鈕

發(fā)布時間:2021-06-17 12:53:42 來源:億速云 閱讀:179 作者:小新 欄目:web開發(fā)

這篇文章主要介紹了Vue如何制作波紋按鈕,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

先說一下用法:

<zk-button class="btn btn-default">默認按鈕</zk-button>
<zk-button class="btn btn-default btn-round">默認按鈕</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)加入詳細的注釋)

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;
 // 點擊坐標原點
 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和獲取用戶點擊的位置坐標,并開始繪制。

循環(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如何制作波紋按鈕”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

向AI問一下細節(jié)

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

vue
AI