基本信息
源码名称:图片局部放大效果
源码大小:0.87M
文件格式:.rar
开发语言:js
更新时间:2020-10-26
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
<script> | |
// OOP:编程 | |
function Magnifier() { | |
// 1.选元素 | |
this.sBox = document.querySelector(".s_box"); | |
this.span = document.querySelector(".s_box span"); | |
this.bBox = document.querySelector(".b_box"); | |
this.bImg = document.querySelector(".b_box img"); | |
// 2.绑定事件 | |
this.init() | |
} | |
Magnifier.prototype.init = function() { | |
var that = this; | |
// 进入 | |
this.sBox.onmouseover = function() { | |
// 3-1.显示,计算span的宽高 | |
that.show() | |
} | |
// 离开 | |
this.sBox.onmouseout = function() { | |
// 3-2.隐藏 | |
that.hide() | |
} | |
// 移动 | |
this.sBox.onmousemove = function(eve) { | |
var e = eve || window.event; | |
// 5.span跟随鼠标 | |
that.move(e) | |
} | |
} | |
Magnifier.prototype.show = function() { | |
// 显示,计算span的宽高 | |
this.span.style.display = "block"; | |
this.bBox.style.display = "block"; | |
this.span.style.width = this.bBox.offsetWidth / this.bImg.offsetWidth * this.sBox.offsetWidth "px"; | |
this.span.style.height = this.bBox.offsetHeight / this.bImg.offsetHeight * this.sBox.offsetHeight "px"; | |
} | |
Magnifier.prototype.hide = function() { | |
// 隐藏 | |
this.span.style.display = "none"; | |
this.bBox.style.display = "none"; | |
} | |
Magnifier.prototype.move = function(e) { | |
// 计算移动的距离 | |
var l = e.clientX - this.span.offsetWidth / 2; | |
var t = e.clientY - this.span.offsetHeight / 2; | |
console.log(this.sBox.offsetTop); | |
// 边界限定 | |
if (l < 0) l = 0; | |
if (t < 0) t = 0; | |
if (l > this.sBox.offsetWidth - this.span.offsetWidth) { | |
l = this.sBox.offsetWidth - this.span.offsetWidth | |
} | |
if (t > this.sBox.offsetHeight - this.span.offsetHeight) { | |
t = this.sBox.offsetHeight - this.span.offsetHeight | |
} | |
// span跟随鼠标 | |
this.span.style.left = l "px"; | |
this.span.style.top = t "px"; | |
// 计算比例 | |
// 当前值 / 总值,得到的就是比例 | |
var x = l / (this.sBox.offsetWidth - this.span.offsetWidth); | |
var y = t / (this.sBox.offsetHeight - this.span.offsetHeight); | |
// 根据比例计算右边大图应该移动的距离 | |
// 比例 * 总值,得到的就是当前应该移动的距离 | |
this.bImg.style.left = x * (this.bBox.offsetWidth - this.bImg.offsetWidth) "px"; | |
this.bImg.style.top = y * (this.bBox.offsetHeight - this.bImg.offsetHeight) "px"; | |
} | |
new Magnifier(); | |
</script> |