国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

Vue波紋按鈕組件制作

2024-05-06 15:34:44
字體:
供稿:網(wǎng)友

先說一下用法:

<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>

點(diǎn)擊代碼如下(我已經(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; // 點(diǎn)擊坐標(biāo)原點(diǎn) 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和獲取用戶點(diǎn)擊的位置坐標(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; }}

總結(jié):

上面代碼我沒有復(fù)制完整,大家想看源碼可以下載看一下

這是4月最后一天上班了,5.1要好好休息一下。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 饶河县| 西充县| 石阡县| 桐城市| 柘荣县| 呼图壁县| 大荔县| 沙雅县| 二手房| 黄浦区| 瑞昌市| 遂溪县| 遂昌县| 长沙市| 北安市| 军事| 汝南县| 阳曲县| 永胜县| 时尚| 辉县市| 应用必备| 连江县| 南丹县| 桃源县| 永福县| 天门市| 武宣县| 明溪县| 娱乐| 韶关市| 农安县| 清徐县| 县级市| 翁牛特旗| 磐石市| 青冈县| 建平县| 托克逊县| 济阳县| 鄂尔多斯市|