当前位置:首页 > VUE

vue实现盖章组件

2026-03-29 05:28:13VUE

实现思路

在Vue中实现盖章组件,可以通过动态渲染图片或SVG元素,结合CSS动画和交互逻辑完成。核心是定位、旋转、缩放等样式的控制。

基本实现步骤

创建Vue组件文件Stamp.vue,定义基本模板和样式:

<template>
  <div class="stamp-container" @click="handleStamp">
    <img 
      v-if="isStamped" 
      :src="stampImage" 
      class="stamp" 
      :style="stampStyle"
    />
  </div>
</template>

<script>
export default {
  props: {
    stampImage: {
      type: String,
      default: 'stamp.png'
    }
  },
  data() {
    return {
      isStamped: false,
      position: { x: 0, y: 0 },
      rotation: 0
    }
  },
  computed: {
    stampStyle() {
      return {
        left: `${this.position.x}px`,
        top: `${this.position.y}px`,
        transform: `rotate(${this.randomRotation()}deg)`
      }
    }
  },
  methods: {
    handleStamp(e) {
      const rect = e.target.getBoundingClientRect()
      this.position = {
        x: e.clientX - rect.left - 50,
        y: e.clientY - rect.top - 50
      }
      this.isStamped = true
    },
    randomRotation() {
      return Math.floor(Math.random() * 30) - 15
    }
  }
}
</script>

<style scoped>
.stamp-container {
  position: relative;
  width: 100%;
  height: 500px;
  border: 1px dashed #ccc;
  cursor: pointer;
}

.stamp {
  position: absolute;
  width: 100px;
  height: 100px;
  pointer-events: none;
  animation: stampEffect 0.3s ease-out;
}

@keyframes stampEffect {
  0% { transform: scale(3); opacity: 0; }
  100% { transform: scale(1); opacity: 1; }
}
</style>

高级功能扩展

添加按压动画效果和自定义参数:

<script>
export default {
  props: {
    stampImage: String,
    size: {
      type: Number,
      default: 100
    },
    maxRotation: {
      type: Number,
      default: 15
    }
  },
  methods: {
    randomRotation() {
      return Math.floor(Math.random() * this.maxRotation*2) - this.maxRotation
    }
  }
}
</script>

使用方法

在父组件中引入并使用:

vue实现盖章组件

<template>
  <Stamp 
    stampImage="/path/to/stamp.png" 
    :size="120"
    :maxRotation="20"
  />
</template>

<script>
import Stamp from './Stamp.vue'

export default {
  components: { Stamp }
}
</script>

注意事项

  1. 准备透明背景的PNG印章图片
  2. 调整stampEffect动画参数可获得不同按压效果
  3. 移动端需要添加touchstart事件支持
  4. 需要控制点击频率避免重复盖章

通过调整CSS动画参数和组件props,可以灵活控制印章的大小、旋转幅度和动画效果。

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

相关文章

vue里实现增删改查

vue里实现增删改查

在Vue中实现增删改查 数据初始化 在Vue组件的data中定义初始数据和表单绑定的变量: data() { return { items: [ { id: 1, name:…

vue实现slidetoggle

vue实现slidetoggle

Vue 实现 SlideToggle 效果 SlideToggle 是一种常见的交互效果,元素以滑动方式展开或收起。以下是几种实现方法: 使用 CSS Transition 和 v-show…

vue实现验证

vue实现验证

Vue 表单验证实现方法 在Vue中实现表单验证可以通过多种方式完成,以下是常见的几种方法: 使用Vuelidate库 Vuelidate是一个轻量级的Vue表单验证库,安装后可以通过简单的配置实现…

vue 实现原理

vue 实现原理

Vue 实现原理 Vue.js 是一个渐进式 JavaScript 框架,其核心实现原理主要包括响应式系统、虚拟 DOM、模板编译和组件化机制。以下是 Vue 实现原理的核心要点: 响应式系统 Vu…

vue 实现单点登录

vue 实现单点登录

单点登录(SSO)实现原理 单点登录允许用户通过一次身份验证访问多个系统。核心原理是用户首次登录后,认证中心颁发令牌(如Token),其他系统通过验证令牌实现免登录。 Vue中实现SSO的方案 基于…

vue实现拦截登录

vue实现拦截登录

Vue 实现登录拦截的方法 使用路由守卫进行拦截 Vue Router 提供了全局前置守卫 beforeEach,可以在路由跳转前进行验证。在路由配置文件中添加以下代码: import router…