用JS怎样写鼠标移入放大效果,方法是什么
Admin 2022-08-09 群英技术资讯 1360 次浏览
这篇文章主要介绍“用JS怎样写鼠标移入放大效果,方法是什么”的相关知识,下面会通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“用JS怎样写鼠标移入放大效果,方法是什么”文章能帮助大家解决问题。使用原生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进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
目录vue-cli3.0修改打包后的文件地址和文件名问题描述修改文件名报错解决vue文件夹名称修改导致错误vue-cli3.0修改打包后的文件地址和文件名问题描述最近开发一个web端vue项目时使用了vue-cli3.0搭建项目目录,开发过程中一切顺利,没有遇到什么问题,开发完毕打包上线时出现了问题,打包后的文件默认在
本文给大家介绍的是JS事件对象的内容,那JS中“事件对象”,到底是什么意思的?其实JS事件对象并不难理解,接下来我们就详细的了解一下JS事件对象,及事件对象中的属性和方法,感兴趣的朋友就继续往下看吧。
这篇文章给大家分享的是jQuery中怎么样删除元素内容的方法,文中给大家分享了两个方法,分别是利用children()和remove()方法、empty()方法,文中的示例代码介绍得很详细,有需要的朋友可以参考,接下来就跟随小编一起了解看看吧。
本文主要介绍了Vue实现动态路由导航的示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
这篇文章主要为大家详细介绍了javascript实现随机抽奖功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008