首页 > 其他分享 >vue图片放大缩小拖拽

vue图片放大缩小拖拽

时间:2023-08-15 12:27:36浏览次数:34  
标签:vue moveStart center px event scale 缩小 translate 拖拽

1.封装可放大缩小拖拽组件
<template>
<div class="drag-outer"
ref="dragWrap"
:style="'width:'+imgWidth"
@mouseenter="isHover = true"
@mouseleave="isHover = isMousedown = false"
@mousemove="dragMousemove">
<div class="drag-inner"
ref="dragElement"
@mousedown="dragMousedown"
@mouseup.stop="isMousedown = false">
<slot></slot>
</div>
</div>
</template>

<script>
export default {
name: "index",
props: {
imgWidth: {
type:String,
default () {
return '400px'
}
},
scaleZoom: {
type: Object,
default () {
return {
max: 5,
min: 0.2
}
}
}
},
data() {
return {
isMousedown: false,
isHover: false,
moveStart: {},
translate: {x: 0, y: 0},
scale: 1
}
},
mounted() {
window.addEventListener('mousewheel',this.handleScroll,false)
},
methods: {
handleScroll(e) {
if (this.isHover) {
let speed = e.wheelDelta / 120
if (e.wheelDelta > 0 && this.scale < this.scaleZoom.max) {
this.scale += 0.2 * speed
this.$refs.dragElement.style.transform = `scale(${this.scale}) translate(${this.translate.x}px, ${this.translate.y}px)`
} else if (e.wheelDelta < 0 && this.scale > this.scaleZoom.min) {
this.scale += 0.2 * speed
this.$refs.dragElement.style.transform = `scale(${this.scale}) translate(${this.translate.x}px, ${this.translate.y}px)`
}
}
},
dragMousedown() {
this.moveStart.x = event.clientX
this.moveStart.y = event.clientY
this.isMousedown = true
},
dragMousemove() {
if (this.isMousedown) {
this.translate.x += (event.clientX - this.moveStart.x) / this.scale
this.translate.y += (event.clientY - this.moveStart.y) / this.scale
this.$refs.dragElement.style.transform = `scale(${this.scale}) translate(${this.translate.x}px, ${this.translate.y}px)`
this.moveStart.x = event.clientX
this.moveStart.y = event.clientY
}
}
}
}
</script>

<style lang="scss" scoped>
.drag-outer {
overflow: hidden;
height:500px; float: left;
display: flex;
background-color:#fff;
justify-content: center;
align-items: center;
.drag-inner {
transform-origin: center center;
display: flex;
justify-content: center;
align-items: center;
cursor: move;
user-select: none;
width:100%;
height:100%;
>* {
-webkit-user-drag: none;
user-drag: none;
}
img{object-fit:contain; width:100%; height:100%}
}
}
</style>

2.引用组件实现效果
<template>
<div>
<inc_imgsvg v-if="srcImg" :imgWidth="'100%'">
<img :src="srcImg" alt="">
</inc_imgsvg>
</div>
</template>

<script>
import inc_imgsvg from "@/components/customImg/index";
export default {
name: "index",
components: {inc_imgsvg},
data(){
return{
srcImg:'https://xxx.png'
}
}
}
</script>
————————————————

原文链接:https://blog.csdn.net/weixin_46054723/article/details/129261541

标签:vue,moveStart,center,px,event,scale,缩小,translate,拖拽
From: https://www.cnblogs.com/sjruxe/p/17631008.html

相关文章

  • Vue+Element导出Excel表格示例
    <template><div@click="exportFn">导出</div></template><script>exportdefault{data(){return{query:{pageIndex:1,//当前页......
  • Vue3 setup的业务逻辑分离功能拆分
    在Vue3开发中,我们可能遇到一个页面或者组件业务逻辑很复杂,代码量达到千行,不利于阅读和维护,因此需要将业务逻辑进行分离首页主界面index.vue//index.vue<script>import{reactive,toRefs}from'vue'importuseOperatefrom'./useOperate.js'importuseConfi......
  • vue + element-ui 的from表单嵌套数组的验证问题
    在vue+element-ui/plus的项目中,有的时候会出现表单自定义增加数组字段,并要对新增加的字段添加相关验证。举个例子//结构data(){return{form:{name:'',Param:[{id:0,label:'',Itemtype:0,},......
  • vue--day64--Vue-resource
    安装npminstallvue-resource//main.js使用importVueResourcefrom"vue-resource"Vue.use(VueResource)安装好Vue-resource之后,在Vue组件中,我们就可以通过this.$http或者使用全局变量Vue.http发起异步请求......
  • 关于Vue的就地更新策略的解析
    在Vue中使用v-for渲染列表时,默认使用就地更新策略。该策略默认是基于索引的,规定在列表绑定的数据元素顺序变化时,不会重新创建整个列表,而只是更新对应DOM元素上的数据。以下代码实现了一个TODO列表的勾选、添加和删除功能:<!DOCTYPEhtml><html><head><title>In-PlaceUpd......
  • Vue computed 计算属性语法
    1.不传参import{ref,computed}from"vue";letcarnoColor=computed(()=>{returnformatterCarnoColor(model.value.carnoColor)}) 2.传参<divv-for="iteminlist"><divv-if='isShow(item)'>是否显示</div......
  • React和Vue的区别,大家怎么看?
    Vue更适合小项目,React更适合大公司大项目;Vue的学习成本较低,很容易上手,但项目质量不能保证......真的是这样吗?借助本篇文章,我们来从一些方面的比较来客观的去看这个问题。 论文档的丰富性从两个方面来看这个问题:社区的支持力度及文档的完善性。 对于任何编程语......
  • vue3 使用 vue-i18n 配置多语言环境
    1.插件地址:VueI18n官方文档GitHub地址2.安装:在Vue之后引入vue-i18n,它会自动安装:<scriptsrc="https://unpkg.com/vue/dist/vue.js"></script><scriptsrc="https://unpkg.com/vue-i18n/dist/vue-i18n.js"></script>NPM:npminstallv......
  • Vuejs装饰器风格开发教程(前言、模板项目、类属性、类方法)
    教程前言AOP切面编程是面向对象开发中的一种经典的编程范式,旨在将横切关注点与核心业务逻辑分离来提高代码的模块性和可维护性。如:日志记录、事务管理等就属于横切关注点。在为H5提供Android原生支持时,曾将插件模块改造为AOP模式,实现插件的自动注册。Java领域的SpringBoo......
  • 使用vue自定义指令实现按钮权限管理
    原文链接:https://www.jianshu.com/p/f7d6b9420cee官网链接:https://v2.cn.vuejs.org/v2/guide/custom-directive.html注册全局指令Vue提供了一个directive方法给我们注册自定义指令,在main.js中注册一个全局的自定义指令。directive方法接收两个参数:指令名称、包含指令钩子函......