日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
點(diǎn)擊產(chǎn)生水波紋效果,Vue自定義指令20行代碼搞定~

前言

大家好,我是林三心,用最通俗易懂的話講最難的知識點(diǎn)是我的座右銘,基礎(chǔ)是進(jìn)階的前提是我的初心~

最近在看一些組件庫的時(shí)候,發(fā)現(xiàn)他們有一種效果還挺好看的,就是點(diǎn)擊會有水波效果~

圖片

所以就想寫一個(gè) Vue 的自定義指令指令來實(shí)現(xiàn)這個(gè)效果:v-ripple

使用方式是這樣的:

實(shí)現(xiàn)思路

思路就是,點(diǎn)擊了按鈕某一處時(shí),往按鈕dom中插入一個(gè)圓dom,這個(gè)圓dom是相對于按鈕去定位的,坐標(biāo)就是(x1,y1),至于(x1,y1)要怎么去算呢?其實(shí)很簡單啊

1、先算出鼠標(biāo)點(diǎn)擊相對于按鈕的坐標(biāo)(x,y)

2、(x-半徑,y-半徑) -> (x1,y1)

至于(x,y)要怎么算?也很簡單?。ㄓ玫絞etBoundingClientRect)

1、算出鼠標(biāo)點(diǎn)擊的全局坐標(biāo)

2、算出按鈕的全局坐標(biāo)

3、鼠標(biāo)按鈕坐標(biāo)減去按鈕坐標(biāo),就能得到(x,y)

圖片

開始實(shí)現(xiàn)

首先我們準(zhǔn)備好基礎(chǔ)的樣式

// ripple.less
#ripple {
  position: absolute;
  pointer-events: none;
  background-color: rgb(30 184 245 / 70%);
  border-radius: 50%;
  transform: scale(0);
  animation: ripple 600ms linear;
}

@keyframes ripple {
  to {
    opacity: 0;
    transform: scale(4);
  }
}

接著就開始開發(fā)自定義指令了,我們要注意一件事,在插入圓dom之前,要?jiǎng)h除圓dom,這樣才能確保只有一個(gè)圓dom

import './ripple.less';
import type { Directive } from 'vue';
export const RIPPLE_NAME = 'ripple';

const createRipple = (el: HTMLElement, e: MouseEvent) => {
  // 設(shè)置按鈕overflow
  el.style.overflow = 'hidden';
  // 獲取按鈕的長寬
  const { clientWidth, clientHeight } = el;
  // 算出直徑
  const diameter = Math.ceil(Math.sqrt(clientWidth ** 2 + clientHeight ** 2));
  // 算出半徑
  const radius = diameter / 2;
  // 獲取按鈕的全局坐標(biāo)
  const { left, top } = el.getBoundingClientRect();
  // 設(shè)置按鈕的定位是relative
  const position = el.style.position;
  if (!position || position === 'static') {
    el.style.position = 'relative';
  }
  // 獲取鼠標(biāo)點(diǎn)擊全局坐標(biāo)
  const { clientX, clientY } = e;

  // 創(chuàng)建一個(gè)圓dom
  const rippleEle = document.createElement('div');
  // 設(shè)置唯一標(biāo)識id
  rippleEle.id = RIPPLE_NAME;
  // 設(shè)置長寬
  rippleEle.style.width = rippleEle.style.height = `${diameter}px`;
  rippleEle.style.left = `${clientX - radius - left}px`;
  rippleEle.style.top = `${clientY - radius - top}px`;
  // 插入圓dom
  el.appendChild(rippleEle);
};

const removeRipple = (el: HTMLElement) => {
  // 刪除圓dom
  const rippleEle = el.querySelector(`#${RIPPLE_NAME}`);
  if (rippleEle) {
    el.removeChild(rippleEle);
  }
};

export const Ripple: Directive = {
  mounted(el: HTMLElement) {
    // 綁定點(diǎn)擊事件
    el.addEventListener('click', e => {
      removeRipple(el);
      createRipple(el, e);
    });
  },
  unmounted(el: HTMLElement) {
    // 組件卸載時(shí)記得刪了
    removeRipple(el);
  },
};

結(jié)語

我是林三心

  • 一個(gè)待過小型toG型外包公司、大型外包公司、小公司、潛力型創(chuàng)業(yè)公司、大公司的作死型前端選手;
  • 一個(gè)偏前端的全干工程師;
  • 一個(gè)不正經(jīng)的掘金作者;
  • 逗比的B站up主;
  • 不帥的小紅書博主;
  • 喜歡打鐵的籃球菜鳥;
  • 喜歡歷史的乏味少年;
  • 喜歡rap的五音不全弱雞如果你想一起學(xué)習(xí)前端,一起摸魚,一起研究簡歷優(yōu)化,一起研究面試進(jìn)步,一起交流歷史音樂籃球rap,可以來俺的摸魚學(xué)習(xí)群哈哈,點(diǎn)這個(gè),有7000多名前端小伙伴在等著一起學(xué)習(xí)哦 -->

當(dāng)前文章:點(diǎn)擊產(chǎn)生水波紋效果,Vue自定義指令20行代碼搞定~
分享路徑:http://www.5511xx.com/article/djsgipe.html