栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > Vue.js

Vue 波纹按钮组件

Vue.js 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Vue 波纹按钮组件

Vue 波纹按钮组件

文章出处:zhanglearning---原文地址

代码链接:https://github.com/zhangKunUserGit/vue-component

效果图:

大家可以在线运行: https://zhangkunusergit.github.io/vue-component/dist/btnRipple.html 看看效果(一定要狠狠的点击哦)。

先说一下用法:

默认按钮
默认按钮
定义速度和初始的波浪透明度

原理:

这里用的是canvas + requestAnimationframe(兼容性可以网上找一下解决方法) 绘制的波纹,有些用的是css transform + setTimeout做的,我感觉不太好。

模板(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;
  }
}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/241276.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号