이번에는 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 변환 + setTimeout 예, 기분이 별로 좋지 않습니다.
템플릿:
<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();
},
여기에서는 주로 캔버스를 초기화하고 사용자 클릭의 위치 좌표를 얻어서 그리기 시작합니다.
루프 그리기
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;
}
}
요약:
위의 전체 코드를 복사하지 않았습니다. 소스 코드를 보고 싶다면 다운로드하여 살펴보세요
방법을 숙지하신 것 같습니다. 이 기사의 사례를 읽은 후 더 흥미로운 내용을 보려면 온라인에서 PHP 중국어 관련 기사를 주목하세요!
추천 도서:
위 내용은 Vue 리플 버튼 컴포넌트 제작 및 사용 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!