首页 / VUE / 如何制作并使用Vue波纹按钮组件
如何制作并使用Vue波纹按钮组件
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何制作并使用Vue波纹按钮组件,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1973字,纯文字阅读大概需要3分钟。
内容图文
这次给大家带来如何制作并使用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; } }
总结:
上面代码我没有复制完整,大家想看源码可以下载看一下
相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!
推荐阅读:
怎样实现Vue项目中使用Vux
不用JS实现菜单打开关闭
以上就是如何制作并使用Vue波纹按钮组件的详细内容,更多请关注Gxl网其它相关文章!
内容总结
以上是互联网集市为您收集整理的如何制作并使用Vue波纹按钮组件全部内容,希望文章能够帮你解决如何制作并使用Vue波纹按钮组件所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。