vue实现简单放大镜效果
程序员文章站
2022-04-24 11:18:15
本文实例为大家分享了vue实现简单放大镜效果的具体代码,供大家参考,具体内容如下
本文实例为大家分享了vue实现简单放大镜效果的具体代码,供大家参考,具体内容如下
<template> <div> <div class="imgmerror"> <div class="smalldiv" @mousemove="move($event)" @mouseenter="enter()" @mouseleave="leave()"> <img src="/image/2017/07/26/0d6069de4916471b92da66b0da8f0ec9.jpg" alt=""> <div class="imgmask"></div> </div> <div class="bigdiv"> <img src="/image/2017/07/26/0d6069de4916471b92da66b0da8f0ec9.jpg" alt="" class="bigimg"> </div> </div> </div> </template> <script> export default { data() { return { } }, methods: { enter(){ let imgmaskdom = document.queryselector('.imgmask'); let bigdivdom = document.queryselector('.bigdiv'); imgmaskdom.style.display = 'block'; bigdivdom.style.display = 'block'; }, leave(){ let imgmaskdom = document.queryselector('.imgmask'); let bigdivdom = document.queryselector('.bigdiv'); imgmaskdom.style.display = 'none'; bigdivdom.style.display = 'none'; }, move(e){ let smalldivdom = document.queryselector('.smalldiv'); let imgmaskdom = document.queryselector('.imgmask'); let bigdivdom = document.queryselector('.bigdiv'); let bigimgkdom = document.queryselector('.bigimg'); let ev = e || window.event; let left = ev.clientx - smalldivdom.offsetleft - imgmaskdom.offsetwidth/2; let top = ev.clienty - smalldivdom.offsettop - imgmaskdom.offsetheight/2; if(left < 0) left=0; if(left > smalldivdom.offsetwidth - imgmaskdom.offsetwidth){ left = smalldivdom.offsetwidth - imgmaskdom.offsetwidth; } if(top < 0) top=0; if(top > smalldivdom.offsetheight - imgmaskdom.offsetheight){ top = smalldivdom.offsetheight - imgmaskdom.offsetheight; } imgmaskdom.style.left = left + 'px'; imgmaskdom.style.top = top + 'px'; //移动比例 let precentx = left / (smalldivdom.offsetwidth-imgmaskdom.offsetwidth); let precenty = top / (smalldivdom.offsetheight-imgmaskdom.offsetheight); bigimgkdom.style.left = precentx * (bigdivdom.offsetwidth - bigimgkdom.offsetwidth) + 'px'; bigimgkdom.style.top = precenty * (bigdivdom.offsetheight - bigimgkdom.offsetheight) + 'px'; } }, } </script> <style lang="scss"> *{ margin: 0; padding: 0; } .imgmerror{ position: relative; padding: 50px; .smalldiv{ border: 1px solid #ccc; width: 360px; height: 360px; position: relative; left: 0; top: 0; img{ width: 100%; height: 100%; } .imgmask{ width: 240px; height: 240px; background: #00ff98; opacity: 0.5; cursor: move; position: absolute; left:0; top: 0; display: none; } } .bigdiv{ border: 1px solid #ccc; width: 540px; height: 540px; position: relative; left: 380px; top: -360px; overflow: hidden; display: none; img{ width: 600px; height: 600px; position: absolute; left: 0; top: 0; } } } </style>
效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。