面向对象实现放大镜
很多电商网站的商品展示都有放大镜的效果,这个效果可以用js面向对象来实现,话不多说直接上代码
html结构
<body>
<div class="box">
<!-- 中等图 -->
<div class="middle">
<img src="./images/tu1.jpg" alt="">
<!-- 遮罩 -->
<div class="shadow"></div>
</div>
<!-- 小图 -->
<div class="small">
<a href="#"><img class="active" src="./images/tu1.jpg" alt=""></a>
<a href="#"><img src="./images/tu2.jpg" alt=""></a>
</div>
<!-- 放大图 -->
<div class="enlarge"></div>
</div>
</body>
css样式
<style>
* {
margin: 0;
padding: 0;
}
.box {
width: 400px;
position: relative;
margin: 50px;
}
.middle {
width: 400px;
height: 400px;
border: 1px solid #000000;
}
.middle img{
width: 100%;
height: 100%;
}
.shadow {
width: 100px;
height: 100px;
background: rgba(255, 255, 0, 0.5);
position: absolute;
left: 0;
top: 0;
display: none;
}
.small {
display: flex;
justify-content: flex-start;
}
.small a{
width: 52px;
height: 52px;
margin: 10px 10px;
}
.small img {
height: 100%;
border: 1px solid green;
}
.enlarge {
width: 400px;
height: 400px;
border: 1px solid #333333;
position: absolute;
top: 0;
left: 105%;
background-image: url(./images/tu1.jpg);
background-position: 0 0;
background-repeat: no-repeat;
background-size: 1600px 1600px;
display: none;
}
.small img.active {
border-color: red;
}
</style>
效果图:
基本布局就是这样
js代码
<script>
// 构造函数
function Enlarge(classname) {
// 获取到需要操作的元素
this.box = document.querySelector("." + classname);
this.middle = this.box.querySelector(".middle");
this.middleImg = this.middle.querySelector(".middle img");
this.shadow = this.middle.querySelector(".middle .shadow");
this.smallImgs = this.box.querySelectorAll(".small img");
this.enlarge = this.box.querySelector(".enlarge");
var _this = this;
// 小图片绑定点击切换事件
for (var i = 0; i < this.smallImgs.length; i++) {
this.smallImgs[i].onclick = function () {
_this.tab(this)
}
}
// 给middle绑定鼠标划入事件
this.middle.onmouseover = function () {
_this.over();
}
// 给middle绑定鼠标划出事件
this.middle.onmouseout = function () {
_this.out();
}
// 给middle绑定鼠标移动事件
this.middle.onmousemove = function () {
_this.move();
}
}
//小图片点击切换方法
Enlarge.prototype.tab = function (ele) {
var active = this.box.querySelector(".small img.active");
active.className = '';
ele.className = 'active';
// 获取图片后缀,通过获取 . 的下标,然后截取后缀
var dIndex = ele.src.lastIndexOf(".");
var suffix = ele.src.slice(dIndex - 1);
// console.log(suffix);
// 改变中等图片
this.middleImg.src = "./images/tu" + suffix;
}
// 鼠标划入显示方法
Enlarge.prototype.over = function () {
this.shadow.style.display = 'block';
var dIndex = this.middleImg.src.lastIndexOf(".");
var suffix = this.middleImg.src.slice(dIndex - 1);
this.enlarge.style.backgroundImage = `url(./images/tu${suffix})`;
this.enlarge.style.display = 'block';
}
// 鼠标划出隐藏方法
Enlarge.prototype.out = function () {
this.shadow.style.display = 'none';
var dIndex = this.middleImg.src.lastIndexOf(".");
var suffix = this.middleImg.src.slice(dIndex - 1);
this.enlarge.style.backgroundImage = `url(./images/tu${suffix})`;
this.enlarge.style.display = 'none';
}
// 鼠标滑动让遮罩和大图对应移动的方法
Enlarge.prototype.move = function (e) {
var e = e || window.event;
// 获取鼠标的坐标
var x = e.clientX;
var y = e.clientY;
// box相对浏览器的偏移值
var boxLeft = this.box.offsetLeft;
var boxTop = this.box.offsetTop;
// 获取遮罩一半的宽高,让光标可以在中间
var boxW = this.shadow.clientWidth / 2;
var boxH = this.shadow.clientHeight /2;
console.log(boxW,boxH);
// 限制左上角移动的边界
if (x < boxLeft + boxW) {
x = boxLeft + boxW;
}
if (y < boxTop + boxH) {
y = boxTop + boxH;
}
// 限制右下角移动的边界值
if (x > boxLeft + this.middle.clientWidth - boxW) {
x = boxLeft + this.middle.clientWidth - boxW;
}
if (y > boxTop + this.middle.clientWidth - boxH) {
y = boxTop + this.middle.clientWidth - boxH;
}
// 用获取到的值设置遮罩的left和top值,让它移动
this.shadow.style.left = x - boxLeft - boxW + 'px';
this.shadow.style.top = y - boxTop - boxH + 'px';
// 让大图移动
// 计算遮罩和中盒子的比例
var xPercent = this.shadow.offsetLeft/ this.middle.clientWidth;
var yPercent = this.shadow.offsetTop / this.middle.clientHeight;
// 获取大图的宽高大小
var enlargeBgWidth = parseInt(window.getComputedStyle(this.enlarge)["background-size"].split(" ")[0]);
var enlargeBgHeight = parseInt(window.getComputedStyle(this.enlarge)["background-size"].split(" ")[1]);
// 计算大图要移动的距离
var xMove = enlargeBgWidth * xPercent;
var yMove = enlargeBgHeight * yPercent;
// 将要移动的距离设置到背景图定位的left和top值上,让大图移动
this.enlarge.style.backgroundPosition = -xMove + 'px ' + -yMove + 'px';
}
// 实例化
var enlarge = new Enlarge("box");
</script>
效果图:
点击切换第二张也是一样的效果