用JS怎样写鼠标移入放大效果,方法是什么
Admin 2022-08-09 群英技术资讯 739 次浏览
使用原生js编写一个鼠标移入图片放大效果,供大家参考,具体内容如下
给图片添加鼠标移动放大方法效果,移到哪里放大哪里
先看看效果是不是你想要的,再看代码
移入前
移入后
html
<!-- css看着写 --> <div class="Box" style="width:200px;height:200px;border:1px solid #f00;position: relative;top:0;left:0;overflow: hidden;"> <Img src="../image/lingtai.jpg" alt="" style="width:200px;height:200px;position:absolute;left:0;top:0;"> </div>
javascript
// 图片放大镜 // @params Class 目标class string // @params Scale 放大倍数 number function ScaleImg(Class, Scale){ this.Box = document.querySelector(Class); this.Img = this.Box.querySelector('img'); this.scale = Scale || 3 ; // 盒子中心点 this.BoxX = this.Box.offsetWidth / 2; this.BoxY = this.Box.offsetHeight / 2; // 获取盒子初始定位 this.Left = parseFloat( this.Box.offsetLeft ); this.Top = parseFloat(this.Box.offsetTop ); this.Init(); } ScaleImg.prototype = { // 鼠标移入 Mouseover: function(e){ var e = e || window.event; // 放大图片 this.Img.style.width = this.Img.offsetWidth * this.scale + "px"; this.Img.style.height = this.Img.offsetHeight * this.scale + "px"; // 设置放大后的图片定位 this.Img.style.left = (this.Box.offsetWidth - this.Img.offsetWidth) / 2 + "px"; this.Img.style.top = (this.Box.offsetHeight - this.Img.offsetHeight) / 2 + "px"; // 获取图片放大后定位值 this.ImgLeft = parseFloat(this.Img.style.left); this.ImgTop = parseFloat(this.Img.style.top); this.Box.left = (this.Box.offsetWidth - this.Img.offsetWidth) / 2; this.Box.top = (this.Box.offsetHeight - this.Img.offsetHeight) / 2; // 阻止默认事件 return ; }, // 鼠标移除 Mouseout: function(e){ var e = e || window.event; // 重置css this.Img.style.width = this.Img.offsetWidth / this.scale + "px"; this.Img.style.height =this.Img.offsetHeight / this.scale + "px"; this.Img.style.left = Math.floor((this.Box.offsetWidth - this.Img.offsetWidth) / 2) + "px"; this.Img.style.top = Math.floor((this.Box.offsetHeight - this.Img.offsetHeight) / 2) + "px"; return ; }, // 鼠标移动 Mousemove: function(e){ var e = e || window.event; // 图片鼠标位置 var ImgXY = {"x": this.Left + this.BoxX, "y": this.Top + this.BoxY}; // 获取偏移量 var left = (ImgXY.x - e.clientX ) / this.BoxX * this.ImgLeft , top = (ImgXY.y - e.clientY) / this.BoxY * this.ImgTop ; this.Img.style.left = Math.floor(this.Box.left - left) + "px"; this.Img.style.top = Math.floor(this.Box.top - top) + "px"; return ; }, // 初始化 Init: function(e){ var that = this; this.Box.onmouseover = function(e){ that.Mouseover(e); } this.Box.onmouseout = function(e){ that.Mouseout(e); } this.Box.onmousemove = function(e){ that.Mousemove(e); } } } // 实例一个对象 new ScaleImg('.Box');
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
jQuery中eq有何作用?有一些新手对于jQuery中eq是什么可能不是很清楚,其实eq的用处有表示eq()选择器和eq()遍历,那么具体的用法是什么呢?文中示例代码介绍的很详细,感兴趣的朋友接下来一起跟随小编看看吧。
不少朋友应该都有玩过贪吃蛇小游戏吧,贪吃蛇小游戏可谓是经典游戏了,那么我们如果使用原生JS怎么写一个贪吃蛇小游戏呢?下面就给大家分享使用原生js实现贪吃蛇游戏代码,感兴趣的朋友可以参考学习。
each()函数封装了十分强大的遍历功能,使用也很方便,它可以遍历一维数组、多维数组、DOM, JSON 等等,这篇文章我们来了解jQuery中each的基本用法,下文有示例供大家参考,有需要的朋友可以看一看,接下来就跟随小编来一起学习一下吧!
比如jQuery的插件扩展原理其实就是在类的原型上扩展方法。其实有一些方法可能很实用,但是它没有提供,这时候就是需要我们自己去做一下扩展了。
今天给大家分享的是JS代码编译器的内容,但是JavaScript的代码编译器有很多,这篇文章就主要介绍编译器Monaco的安装和使用,下文有详细的介绍,感兴趣的朋友可以参考。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008