一、面向对象-放大镜
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Document</title>
</head>
<style>
.box {
width: 402px;
height: 464px;
border: 2px solid #00f;
position: relative;
margin: 50px;
}
.middle {
width: 400px;
height: 400px;
border: 1px solid #000;
position: relative;
}
.small {
margin-top: 10px;
}
.small img {
border: 1px solid #000;
margin-left: 10px;
}
.middle .shade {
position: absolute;
left: 0;
top: 0;
background-color: rgba(255, 255, 0, 0.4);
width: 200px;
height: 200px;
display: none;
}
.big {
width: 400px;
height: 400px;
border: 1px solid #f00;
position: absolute;
left: 105%;
top: 0;
background-image: url("./images/big1.jpg");
background-size: 800px 800px;
display: none;
}
</style>
<body>
<div style="height: 300px"></div>
<div class="box">
<div class="middle">
<img src="./images/middle1.jpg" alt="" />
<div class="shade"></div>
</div>
<div class="small">
<img src="./images/small1.jpg" alt="" />
<img src="./images/small2.jpg" alt="" />
</div>
<div class="big"></div>
</div>
</body>
<script type="text/javascript">
function Enlarge(classname) {
this.box = document.querySelector("." + classname);
this.smallImgs = this.box.querySelectorAll(".small img");
this.middleImg = this.box.querySelector(".middle>img");
this.big = this.box.querySelector(".big");
this.middle = this.box.querySelector(".middle");
this.shade = this.box.querySelector(".shade");
this.smallImgs[0].style.borderColor = "red";
}
Enlarge.prototype.init = function () {
for (let i = 0; i < this.smallImgs.length; i++) {
this.smallImgs[i].onclick = () => {
this.smallImgClick(i);
};
}
this.middle.onmouseover = () => {
this.shade.style.display = "block";
this.big.style.display = "block";
this.middle.onmousemove = (e) => {
this.move();
};
};
this.middle.onmouseout = () => {
this.shade.style.display = "none";
this.big.style.display = "none";
};
};
Enlarge.prototype.move = function () {
var e = e || window.event;
var x = e.pageX;
var y = e.pageY;
this.setShadeMove(x, y);
this.setBigImgMove();
};
Enlarge.prototype.setBigImgMove = function () {
var xpercent = this.shade.offsetLeft / this.middle.clientWidth;
var ypercent = this.shade.offsetTop / this.middle.clientHeight;
var bigBgSize = getStyle(this.big, "background-size");
var bigBgWidth = parseFloat(bigBgSize.split(" ")[0]);
var bigBgHeight = parseFloat(bigBgSize.split(" ")[1]);
var bigLeft = xpercent * bigBgWidth;
var bigTop = ypercent * bigBgHeight;
this.big.style.backgroundPosition = `-${bigLeft}px -${bigTop}px`;
};
Enlarge.prototype.setShadeMove = function (x, y) {
var l = x - this.box.offsetLeft - this.shade.offsetWidth / 2;
var t = y - this.box.offsetTop - this.shade.offsetHeight / 2;
if (l <= 0) {
l = 0;
}
if (t <= 0) {
t = 0;
}
if (l >= this.middle.clientWidth - this.shade.offsetWidth) {
l = this.middle.clientWidth - this.shade.offsetWidth;
}
if (t >= this.middle.clientHeight - this.shade.offsetHeight) {
t = this.middle.clientHeight - this.shade.offsetHeight;
}
this.shade.style.left = l + "px";
this.shade.style.top = t + "px";
};
Enlarge.prototype.smallImgClick = function (i) {
for (var j = 0; j < this.smallImgs.length; j++) {
this.smallImgs[j].style.borderColor = "#000";
}
this.smallImgs[i].style.borderColor = "red";
var smallImgPath = this.smallImgs[i].getAttribute("src");
var index = smallImgPath.lastIndexOf(".");
var suffix = smallImgPath.slice(index - 1);
var middleImgPath = "./images/middle" + suffix;
this.middleImg.setAttribute("src", middleImgPath);
var bigImgPath = "./images/big" + suffix;
this.big.style.backgroundImage = `url("${bigImgPath}")`;
};
var e = new Enlarge("box");
e.init();
function getStyle(ele, attr) {
if (window.getComputedStyle) {
return window.getComputedStyle(ele)[attr];
} else {
return ele.currentStyle[attr];
}
}
</script>
</html>