当前位置:首页 > VUE

vue实现盖章组件

2026-02-17 06:09:10VUE

vue实现盖章组件的方法

实现一个盖章组件可以通过自定义指令或组件封装完成,以下是两种常见实现方式:

自定义指令实现

通过自定义指令v-stamp动态添加盖章效果:

vue实现盖章组件

Vue.directive('stamp', {
  inserted(el, binding) {
    const stamp = document.createElement('div')
    stamp.className = 'vue-stamp'
    stamp.innerHTML = binding.value || '已审核'
    stamp.style = `
      position: absolute;
      transform: rotate(-15deg);
      color: red;
      font-size: 24px;
      opacity: 0.8;
      pointer-events: none;
      user-select: none;
    `
    el.style.position = 'relative'
    el.appendChild(stamp)
  }
})

使用方式:

<div v-stamp="'通过审核'"></div>

组件化封装

创建可复用的Stamp组件:

vue实现盖章组件

<template>
  <div class="stamp-container" :style="containerStyle">
    <div class="stamp" :style="stampStyle">
      {{ text }}
    </div>
  </div>
</template>

<script>
export default {
  props: {
    text: { type: String, default: '已盖章' },
    color: { type: String, default: '#f00' },
    size: { type: Number, default: 24 },
    rotate: { type: Number, default: -15 },
    opacity: { type: Number, default: 0.7 }
  },
  computed: {
    stampStyle() {
      return {
        transform: `rotate(${this.rotate}deg)`,
        color: this.color,
        fontSize: `${this.size}px`,
        opacity: this.opacity
      }
    },
    containerStyle() {
      return { position: 'relative' }
    }
  }
}
</script>

<style>
.stamp {
  position: absolute;
  pointer-events: none;
  user-select: none;
  z-index: 100;
}
</style>

动态位置控制

添加鼠标点击定位功能:

methods: {
  handleClick(e) {
    const stamp = this.$refs.stamp
    stamp.style.left = `${e.offsetX - stamp.offsetWidth/2}px`
    stamp.style.top = `${e.offsetY - stamp.offsetHeight/2}px`
  }
}

高级效果实现

实现圆形印章效果:

<template>
  <div class="seal" :style="sealStyle">
    <div class="seal-circle"></div>
    <div class="seal-text">{{ text }}</div>
  </div>
</template>

<style>
.seal {
  position: absolute;
  width: 120px;
  height: 120px;
  border-radius: 50%;
  border: 3px solid red;
  display: flex;
  justify-content: center;
  align-items: center;
}
.seal-text {
  transform: rotate(-15deg);
  color: red;
  font-weight: bold;
}
</style>

注意事项

  1. 需要设置pointer-events: none避免遮挡交互
  2. 建议添加z-index控制层级关系
  3. 动态定位时注意容器需设置position: relative
  4. 移动端需考虑触摸事件适配

以上实现可以根据实际需求组合使用,组件化方式更适合复杂场景和复用需求。

标签: 组件vue
分享给朋友:

相关文章

vue实现防抖节流

vue实现防抖节流

Vue 中实现防抖(Debounce) 防抖的核心是延迟执行函数,在连续触发时取消前一次的调用,只执行最后一次。适用于输入框搜索、窗口大小调整等场景。 方法1:使用 Lodash 的 _.debo…

vue实现主题

vue实现主题

Vue 实现主题的方法 使用 CSS 变量动态切换主题 定义全局 CSS 变量,通过修改这些变量实现主题切换。在 Vue 的根组件或全局样式中定义变量: :root { --primary-c…

vue实现app

vue实现app

Vue 实现 App 的常见方法 使用 Vue 开发移动端 App 主要有两种主流方案:混合开发(Hybrid)和原生渲染。以下是具体实现方式和工具链: 混合开发方案(WebView 嵌入) 通…

vue实现全景

vue实现全景

Vue 实现全景效果的方法 使用 Vue 实现全景效果通常需要结合第三方库或框架,以下是几种常见的方法: 使用 Three.js 和 Vue-Three.js Three.js 是一个强大的 3D…

vue实现modal

vue实现modal

Vue 实现 Modal 弹窗组件 使用 Vue 实现 Modal 弹窗组件可以通过多种方式,包括原生 Vue 组件、第三方库或自定义指令。以下是几种常见方法: 原生 Vue 组件实现 创建一个基…

vue实现画图

vue实现画图

Vue 实现画图功能 在 Vue 中实现画图功能通常需要结合 HTML5 的 <canvas> 元素或第三方库。以下是几种常见的方法: 使用原生 Canvas API 通过 Vue 直接…