vue自定义指令--实现div和图片的拖拽,放大,缩小

vue自定义指令–实现div和图片的拖拽,放大,缩小

在开发可视化大屏的过程中碰到甲方只能提供一张图片的地图无法提供经纬度,需要在这张图片实现类似百度地图功能的标点。所以就想实现对图片的拖拽移动然后加上放大和缩小功能。这样来模拟百度地图的小部分功能,然后通过在图片上放小div进行标点,来进行坐标定位,如下图
在这里插入图片描述
代码已经封装成组件,复制可以。最后求点赞💖🤞😍

<template>
  <div class="app1">
    <div v-drag class="qwe" style="width: 1920px; height: 1080px">
      <div class="div1" @click="getData"></div>
      <div class="div2" @click="getData"></div>
      <div class="div3" @click="getData"></div>
      <div class="div4" @click="getData"></div>
      <div class="div5" @click="getData"></div>
      <div class="div6" @click="getData"></div>
    </div>
  </div>
</template>

<script>
export default {
  // 组件名称
  name: 'mapPage',
  data() {
    return {}
  },
  // 自定义指令 实现可拖动 drag(el, bindings)
  directives: {
    drag(el) {
      el.onmousedown = function (e) {
        let disx = e.pageX - el.offsetLeft
        let disy = e.pageY - el.offsetTop
        document.onmousemove = function (e) {
          // el.style.left = e.pageX - disx + 'px'
          // el.style.top = e.pageY - disy + 'px'

          if (e.pageX - disx < 53 && -1100 < e.pageX - disx) {
            // if (e.pageY - disy < 40 && -271 < e.pageY - disy) {
            el.style.left = e.pageX - disx + 'px'
            el.style.top = e.pageY - disy + 'px'
            // console.log('左', el.style.left, el.style.top, '上')
            // }
          }
        }
        document.onmouseup = function () {
          document.onmousemove = document.onmouseup = null
        }
      }
      //对div盒子的大小改变
      let scale = 1
      el.onmousewheel = document.onmousewheel = (e) => {
        if (e.wheelDelta < 0) {
          // console.log('后滚放大')
          if (scale < 1.2) {
            scale += 0.05
            el.style.transform = `scale(${scale})`
            el.style.transformOrigin = '200 200'
            // console.log(scale, '放大')
          }
        } else if (e.wheelDelta > 0) {
          // 前滚缩小
          if (scale > 0.4) {
            scale -= 0.05
            el.style.transform = `scale(${scale})`
            el.style.transformOrigin = '200 200'
            // console.log(scale, '缩小')
          }
        }
      }
    },
  },
  //计算属性
  computed: {},
  // 组件方法
  methods: {
    getData() {
      console.log('可以触发点击事件')
    },
  },
  //挂载后钩子函数
  mounted() {},
}
</script>

<style scoped lang="scss">
.app1 {
  position: relative;
  .map {
    width: 45%;
    height: 100%;
  }
  .qwe {
    z-index: 1;
    background: url('../../assets/indexImg/bg.webp') center center no-repeat;
    background-size: 100% 100%;
    // background-color: rgb(185, 76, 13);
    position: absolute;
    top: 0%;
    left: 0%;
    img {
      width: 100%;
      height: 100%;
    }
    .div1 {
      width: 20px;
      height: 30px;
      background-color: red;
      position: absolute;
      top: 48%;
      left: 12%;
    }
    .div2 {
      @extend .div1;
      position: absolute;
      top: 66%;
      left: 12%;
    }
    .div3 {
      @extend .div1;
      position: absolute;
      top: 55%;
      left: 12%;
    }
    .div4 {
      @extend .div1;
      position: absolute;
      top: 71%;
      left: 33%;
    }
    .div5 {
      @extend .div1;
      position: absolute;
      top: 46%;
      left: 66%;
    }
    .div6 {
      @extend .div1;
      position: absolute;
      top: 32%;
      left: 82%;
    }
  }
}
</style>

最近也开发了自己的公众号,会在上面分享前端方面的知识,有兴趣的话,走个关注吧!!!
在这里插入图片描述

  • 8
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
可以通过自定义 Quill 的 toolbar 实现图片的拉伸、放大缩小。 首先,你需要在 `vue-quill-editor` 的配置中添加一个自定义的 toolbar。例如: ``` <quill-editor v-model="content" :options="editorOption" > <div v-if="editorOption.toolbar"> <span class="ql-formats"> <select class="ql-size"> <option value="small"></option> <option selected></option> <option value="large"></option> <option value="huge"></option> </select> </span> <span class="ql-formats"> <button class="ql-stretch"></button> <button class="ql-zoom-in"></button> <button class="ql-zoom-out"></button> </span> <span class="ql-formats"> <button class="ql-image"></button> </span> </div> </quill-editor> ``` 然后,在 `mounted()` 钩子函数中获取 Quill 实例,并添加自定义的 toolbar 按钮的逻辑。例如: ``` mounted() { this.$nextTick(() => { const quill = this.$refs.myQuillEditor.quill const stretchButton = quill.getModule('toolbar').container.querySelector('.ql-stretch') const zoomInButton = quill.getModule('toolbar').container.querySelector('.ql-zoom-in') const zoomOutButton = quill.getModule('toolbar').container.querySelector('.ql-zoom-out') stretchButton.addEventListener('click', function() { // todo: 进行图片拉伸的逻辑 }) zoomInButton.addEventListener('click', function() { // todo: 进行图片放大的逻辑 }) zoomOutButton.addEventListener('click', function() { // todo: 进行图片缩小的逻辑 }) }) } ``` 其中,`quill.getModule('toolbar').container` 获取到的是 Quill 的 toolbar 容器,可以通过 `querySelector()` 方法获取到自定义按钮的 DOM 元素并添加事件监听器。 接下来,你需要实现图片拉伸、放大缩小的逻辑。可以通过 Quill 的 Delta API 获取到当前选中的图片,并对其属性进行修改。例如: ``` stretchButton.addEventListener('click', function() { const range = quill.getSelection() if (!range) return const [image] = quill.getSemanticHTML(range.index, range.length).match(/<img.*?>/g) if (image) { quill.updateContents(new Delta().retain(range.index).delete(range.length).insert({ image: { ...Quill.Attributor.AttributeList.fromHTML(image), width: '200px', height: 'auto' } })) } }) ``` 上面的代码中,首先获取到当前选中的图片(即 `<img>` 标签),然后通过 Quill 的 `updateContents()` 方法修改图片的属性,例如将其宽度设置为 200px,高度设置为自适应。 类似地,你可以实现图片放大缩小。例如: ``` zoomInButton.addEventListener('click', function() { const range = quill.getSelection() if (!range) return const [image] = quill.getSemanticHTML(range.index, range.length).match(/<img.*?>/g) if (image) { const attr = Quill.Attributor.AttributeList.fromHTML(image) const width = parseInt(attr.width.replace('px', '')) const height = parseInt(attr.height.replace('px', '')) quill.updateContents(new Delta().retain(range.index).delete(range.length).insert({ image: { ...attr, width: `${width + 10}px`, height: `${height + 10}px` } })) } }) zoomOutButton.addEventListener('click', function() { const range = quill.getSelection() if (!range) return const [image] = quill.getSemanticHTML(range.index, range.length).match(/<img.*?>/g) if (image) { const attr = Quill.Attributor.AttributeList.fromHTML(image) const width = parseInt(attr.width.replace('px', '')) const height = parseInt(attr.height.replace('px', '')) if (width > 10 && height > 10) { quill.updateContents(new Delta().retain(range.index).delete(range.length).insert({ image: { ...attr, width: `${width - 10}px`, height: `${height - 10}px` } })) } } }) ``` 上面的代码中,首先获取到当前选中的图片的宽度和高度,然后将其分别增加或减少 10px。需要注意的是,当图片的宽度或高度小于等于 10px 时,不能再继续缩小了。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

低级前端

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

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

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

打赏作者

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

抵扣说明:

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

余额充值