Vue波纹按钮组件制作
2018-04-30 13:59
579 查看
先说一下用法:
<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; } }
总结:
上面代码我没有复制完整,大家想看源码可以下载看一下
这是4月最后一天上班了,5.1要好好休息一下。
您可能感兴趣的文章:
相关文章推荐
- 从实作标题栏按钮开始浅谈组件的制作
- 2018.01.29.使用vue组件modal制作登陆弹窗,并实现背景灰蒙透明效果
- vue--element-ui--tree组件解决从外部按钮取消选中状态
- vue 开发一个按钮组件的示例代码
- Vue 2.0 制作列表组件,实现分页、搜索、批量操作等
- Android自定义组件系列【14】——Android5.0按钮波纹效果实现
- Android自定义组件系列【14】——Android5.0按钮波纹效果实现
- vue组件中点击按钮后修改输入框的状态
- 如何制作Vuejs组件,并且在项目中使用
- 使用vue制作探探滑动堆叠组件的实例代码
- vue 点击按钮实现动态挂载子组件
- Vue 2.0 制作列表组件,实现分页、搜索、批量操作等
- 使用Vue制作切口盒子组件
- Vue 2.0 制作列表组件,实现分页、搜索、批量操作等
- vue 点击按钮实现动态挂载子组件的方法
- 从实作标题栏按钮开始浅谈组件的制作
- vue-video-player 通过自定义按钮组件实现全屏切换效果【推荐】
- 在vue组件的stylus样式总 取消search类型的input按钮中默认样式
- 基于Vue制作组织架构树组件
- 使用Vue制作图片轮播组件思路详解