您好,欢迎访问一九零五行业门户网

如何制作并使用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项目中使用vux
不用js实现菜单打开关闭
以上就是如何制作并使用vue波纹按钮组件的详细内容。
其它类似信息

推荐信息