vue3 常用指令 更新中

本文介绍了Vue3中的四个实用指令:v-waterMarker实现文字水印效果,v-dragable提供自定义拖拽功能,v-copy用于复制文本到剪贴板,v-longpress实现长按时触发事件,以及v-debounce防止频繁点击带来的性能问题。

vue3 功能大全

v-waterMarker 水印指令

directive/waterMarker/index .ts

/**
 * v-waterMarker可接收参数,均为非必填
 * { text: 'vue-admin-box', font: '16px Microsoft JhengHei', textColor: '#000' }
 */
import {
   
    Color, FontFamilyProperty, FontProperty } from 'csstype'
import type {
   
    Directive, DirectiveBinding } from 'vue'

const directive: Directive = {
   
   
  mounted(el: HTMLElement, binding: DirectiveBinding) {
   
   
    binding.value ? binding.value : binding.value = {
   
   }
    addWaterMarker(binding.value.text, el, binding.value.font, binding.value.textColor, )
  },
}

function addWaterMarker(str: string, parentNode: HTMLElement, font: FontProperty, textColor: Color) {
   
   
  // 水印文字,父元素,字体,文字颜色
  var can = document.createElement('canvas') as HTMLCanvasElement
  parentNode.appendChild(can)
  can.width = 200
  can.height = 150
  can.style.display = 'none'
  var cans = can.getContext('2d') as CanvasRenderingContext2D
  cans.rotate((-20 * Math.PI) / 180)
  cans.font = font || '16px Microsoft JhengHei'
  cans.fillStyle = textColor || 'rgba(180, 180, 180, 0.3)'
  cans.textAlign = 'left'
  cans.textBaseline = 'middle'
  cans.fillText(str ||'He word' , can.width / 10, can.height / 2)
  parentNode.style.backgroundImage = 'url(' + can.toDataURL('image/png') + ')'
}

export default directive
<template>
  <div class="layout-container">
    <div class="layout-container-table" v-waterMarker>
      我是一个水印页面
    </div>
  </div>
</template>
<script setup lang="ts">
import WaterMarker from '@/directive/waterMarker'
</script>
<style lang="scss" scoped>
</style>

v-dragable 拖拽指令

directive/dragable/index .ts

/**
 * 支持父级,自定义父级,以及window作为父级
 * 使用示例:
 * 1. v-dragable
 * 2. v-dragable="'father'" // 使用父级作为父级
 * 3. v-dragable="'body'" // 使用body对象作为父级
 * 4. v-dragable="'#app'" // 使用id作为父级
 * 5. v-dragable="'.list'" // 使用class名作为父级
 * 3-5代表所有可被document.querySelector()解析的参数值
 **/
import type {
   
    Directive, DirectiveBinding } from 'vue'
interface Position {
   
   
  x: number,
  y: number
}
interface Mouse {
   
   
  down: Position,
  move: Position
}
interface ElType extends HTMLElement {
   
   
  __mouseDown__: any,
  __mouseUp__: any,
  __mouseMove__: any,
  __parentDom__: HTMLElement,
  __position__: Position
}
const directive: Directive = {
   
   
  mounted: (el: ElType, binding: DirectiveBinding) => {
   
   
    setParentDom(el, binding, false)
    // 子级元素位置处理
    // 1. 获取父子元素当前位置
    let parentDomRect: DOMRect
    let elDomRect: DOMRect
    let mouseData: Mouse = {
   
   
      down: {
   
    x: 0, y: 0},
      move: {
   
    x: 0, y: 0 }
    }
    let mouseDown: boolean
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Vue1024

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值