国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表

圖片精選

主站蜘蛛池模板: 崇仁县| 长岛县| 耒阳市| 衡南县| 嵩明县| 张家界市| 布尔津县| 青田县| 浦县| 县级市| 伊宁县| 龙南县| 牙克石市| 喀喇| 璧山县| 金溪县| 克什克腾旗| 宜兴市| 长白| 阜城县| 永兴县| 湟源县| 乾安县| 香格里拉县| 汝阳县| 曲靖市| 云龙县| 宽甸| 通州区| 托克逊县| 无极县| 关岭| 德兴市| 福海县| 漳浦县| 梁平县| 富顺县| 新宁县| 蓬安县| 含山县| 昌图县|