新西兰服务器

Vue怎么制作波纹按钮


Vue怎么制作波纹按钮

发布时间:2022-05-05 18:12:57 来源:高防服务器网 阅读:70 作者:iii 栏目:大数据

这篇文章主要介绍“Vue怎么制作波纹按钮”,在日常操作中,相信很多人在Vue怎么制作波纹按钮问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”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(兼容性可以网上找一下解决方法) 绘制的波纹,有些用的是css transform + setTimeout做的,我感觉不太好。

模板(template):

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

点击代码如下(我已经加入详细的注释)

ripple(event) {   // 清除上次没有执行的动画   if (this.timer) {    window.cancelAnimationFrame(this.timer);   }   this.el = event.target;   // 执行初始化   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和获取用户点击的位置坐标,并开始绘制。

循环绘制

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怎么制作波纹按钮”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注高防服务器网网站,小编会继续努力为大家带来更多实用的文章!

[微信提示:高防服务器能助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。

[图文来源于网络,不代表本站立场,如有侵权,请联系高防服务器网删除]
[