ホームページ  >  記事  >  ウェブフロントエンド  >  Vueリップルボタンコンポーネントの作り方と使い方

Vueリップルボタンコンポーネントの作り方と使い方

php中世界最好的语言
php中世界最好的语言オリジナル
2018-06-01 14:27:281346ブラウズ

今回は、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 プロジェクトで Vux を使用する方法

JS を使用せずにメニューを開いたり閉じたりする

以上がVueリップルボタンコンポーネントの作り方と使い方の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。