vue中怎么实现图片放大缩小的按键功能并可拖曳
Admin 2022-07-02 群英技术资讯 776 次浏览
vue图片拖拉转放大缩小组件的具体使用方法,供大家参考,具体内容如下
<doc> 图片组件 - 用户放大缩小以及拖拽 </doc> <template> <div style="width: 100%;position: relative;overflow: hidden;text-align: center;border: 1px solid #f1f2f3;"> <el-button size='mini' @click="toBIgChange" icon="el-icon-zoom-in" style="position: absolute;top: 2px ;left: 2px;z-index: 999;"></el-button> <el-button size='mini' @click="toSmallChange" icon="el-icon-zoom-out" style="position: absolute;top: 2px ;left: 40px;z-index: 999;"></el-button> <img id="img" :src="src" alt="" @mousedown.prevent="dropImage" :style="{transform:'scale('+multiples+')'}"> </div> </template> <script> export default { props: ['src'], data() { return { multiples: 1, odiv: null, } }, mounted() { this.dropImage() }, watch: { src(newValue, oldValue) { this.multiples = 1 if (this.odiv !== null) { this.odiv.style.left = '0px'; this.odiv.style.top = '0px'; } }, }, methods: { toBIgChange() { if (this.multiples >= 2) { return; } this.multiples += 0.25; }, // 缩小 toSmallChange() { if (this.multiples <= 1) { return; } this.multiples -= 0.25; }, // 拖拽 dropImage(e) { if (e === null) { return } this.odiv = e.target; //获取目标元素 //算出鼠标相对元素的位置 let disX = e.clientX - this.odiv.offsetLeft; let disY = e.clientY - this.odiv.offsetTop; document.onmousemove = (e) => { //鼠标按下并移动的事件 //用鼠标的位置减去鼠标相对元素的位置,得到元素的位置 let left = e.clientX - disX; let top = e.clientY - disY; //绑定元素位置到positionX和positionY上面 this.positionX = top; this.positionY = left; //移动当前元素 this.odiv.style.left = left + 'px'; this.odiv.style.top = top + 'px'; }; document.onmouseup = (e) => { document.onmousemove = null; document.onmouseup = null; }; }, } } </script> <style scoped> img { width: 100%; position: relative; } </style>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
页面跳转,我们一般都通过路由跳转实现,通常情况下可直接使用router-link标签实现页面跳转,下面这篇文章主要给大家介绍了关于vue如何通过点击事件实现页面跳转的相关资料,需要的朋友可以参考下
这篇文章主要介绍了vue 设置背景颜色及透明度的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
代码分片是webpck打包工具所特有的一项技术,通过这项功能可以把代码按照特定的形式进行拆分,使用户不必一次全部加载,而是按需加载。本文就来详细介绍,感兴趣的可以了解一下
这篇文章主要为大家详细介绍了小程序实现列表倒计时功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
这篇文章给大家分享的是uniapp中全局变量的五种实现方式,其中一些细节知识参考uView官网。下面就跟随小编一起来看看uniapp中全局变量的实现示例吧。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008