1、在 Vue2/3 中 dom 拖动,Ctrl + 鼠标滚轮放大缩小 dom,可以使用 @panzoom/panzoom
库实现具体实现
2、npm 下载插件包 npm install @panzoom/panzoom -S
3、在需要的页面按需引入(也可以全局引入), 详细见下文代码块
4、通过 @wheel 事件监听鼠标滚轮,调整 dom 大小(@wheel 是只有当我们滚动鼠标滚轮的时候,才会触发)
# Vue2 实现方式
| <template> |
| |
| <div ref="dom" @wheel="wheel"></div> |
| </template> |
| |
| <script> |
| import Panzoom from "@panzoom/panzoom"; |
| |
| export default { |
| data () { |
| return { |
| panzoom: null |
| } |
| } |
| mounted () { |
| this.getPanzoomElement() |
| }, |
| methods: { |
| |
| getPanzoomElement () { |
| |
| this.panzoom = Panzoom(this.$refs.dom, { |
| |
| maxScale: 2, |
| minScale: 0, |
| overflow: "auto", |
| canvas: true, |
| }); |
| } |
| |
| wheel (event) { |
| |
| if (event.ctrlKey) { |
| const scaleResult = this.panzoom.zoomWithWheel(event); |
| } |
| } |
| } |
| } |
| <script> |
# Vue3 实现方式
| <template> |
| |
| <div ref="wheelDom" @wheel="wheel"></div> |
| </template> |
| |
| <script setup> |
| |
| import { onMounted, reactive, toRefs,ref } from "vue"; |
| import Panzoom from "@panzoom/panzoom"; |
| const wheelDom = ref(null); |
| |
| const state = reactive({ |
| panzoom: null |
| }) |
| |
| const getPanzoomElement = () => { |
| |
| state.panzoom = Panzoom(wheelDom.value, { |
| |
| maxScale: 2, |
| minScale: 0, |
| overflow: "auto", |
| canvas: true, |
| }); |
| } |
| |
| const wheel = (event) => { |
| |
| if (event.ctrlKey) { |
| const scaleResult = this.panzoom.zoomWithWheel(event); |
| } |
| } |
| |
| onMounted(() => { |
| getPanzoomElement() |
| }); |
| <script> |