在指定区域通过拖拽控制小图相对于指定区域的位置及小图的大小

该文章展示了如何利用interactjsJavaScript库在一个Vue组件中实现二维码图片的拖动和自定义大小调整功能。代码示例详细说明了如何监听拖动和缩放事件,并在结束时获取二维码的位置和大小参数。背景图和二维码图片可以自由移动和调整大小,最小尺寸限制为50x50像素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果

         使用的一款软件将视频转gif有水印,将就着看看吧,通过上面的控制,可以获取到二维码相对于区域的位置,及二维码大小的参数,这其中用的的一个js库interactjs 

具体实现代码

        Interact.vue组件

<template>
  <div
    id="payconfig-drag-area"
    :style="{ backgroundImage: `url(${bgImageUrl})` }"
  >
    <img
      id="payconfig-drag-element"
      :src="qrCodeImg"
      style="width: 60px; height: 60px"
    />
  </div>
</template>

<script lang="ts" setup>
import interact from 'interactjs'
// 引入二维码图片
import qrCodeImg from '../assets/img/qrCode.png'
import { computed } from 'vue'

const props = defineProps<{
  bgImage?: string
}>()

const bgImageUrl = computed(() => props.bgImage)

const emits = defineEmits<{
  (
    e: 'getPositionAndSize',
    data: {
      x: number | undefined
      y: number | undefined
      width: number
      height: number
    }
  ): void
}>()

// 定义一个 Interact.js 事件监听器
interact('#payconfig-drag-element')
  .draggable({
    modifiers: [
      interact.modifiers.restrictRect({
        restriction: 'parent',
        endOnly: true,
      }),
    ],
    listeners: {
      move(event) {
        const target = event.target
        const x = (parseFloat(target.getAttribute('data-x')) || 0) + event.dx
        const y = (parseFloat(target.getAttribute('data-y')) || 0) + event.dy

        target.style.transform = `translate(${x}px, ${y}px)`
        target.setAttribute('data-x', x)
        target.setAttribute('data-y', y)
      },
      end() {
        getPositionAndSize()
      },
    },
  })
  .resizable({
    edges: { left: true, right: true, bottom: true, top: true },
    listeners: {
      move(event) {
        const target = event.target
        const x = parseFloat(target.getAttribute('data-x')) || 0
        const y = parseFloat(target.getAttribute('data-y')) || 0

        target.style.width = `${event.rect.width}px`
        target.style.height = `${event.rect.height}px`
        target.style.transform = `translate(${x}px, ${y}px)`
      },
      end() {
        getPositionAndSize()
      },
    },
    modifiers: [
      interact.modifiers.restrictSize({
        min: { width: 50, height: 50 },
      }),
    ],
  })

function getPositionAndSize() {
  const target = document.getElementById('payconfig-drag-element')
  // 获取目标元素的rect属性
  const targetRect = target?.getBoundingClientRect()
  // 获取目标元素父元素的rect属性
  const parentRect = target?.parentNode?.getBoundingClientRect()
  const x = targetRect && targetRect.left - parentRect.left
  const y = targetRect && targetRect.top - parentRect.top
  const width = targetRect?.width || 50
  const height = targetRect?.height || 50

  emits('getPositionAndSize', { x, y, width, height })
}
</script>

<style scoped lang="scss">
#payconfig-drag-area {
  width: 500px;
  height: 300px;
  border: 1px solid #ccc;
  background-repeat: no-repeat;
  background-size: contain;
}
</style>

使用的地方

        interactjs里面还有好多拖拽相关的操作的使用,有兴趣可以了解一下,不过都是英文的文档,暂时没找到中文文档,看得我头皮发麻 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ZL随心

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

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

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

打赏作者

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

抵扣说明:

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

余额充值