淘宝详情放大镜效果
作者:互联网
let dataArr = [{
small: "./images/imgA_1.jpg",
middle: "./images/imgA_2.jpg",
big: "./images/imgA_3.jpg"
}, {
small: "./images/imgB_1.jpg",
middle: "./images/imgB_2.jpg",
big: "./images/imgB_3.jpg"
}, {
small: "./images/imgC_1.jpg",
middle: "./images/imgC_2.jpg",
big: "./images/imgC_3.jpg"
}];
let artEle = document.querySelector("article");
//拼接article的内容
let str = "";
//初始化页面
function renderImg() {
str += `<section>
<div class="box">
<img class="middleimg" src="${dataArr[0].middle}" alt="${0}">
</div>
<div class="bigimg"></div>
</section>`;
str += `<section>
<input type="button" value="-">`;
//添加小图片(并添加下标)
dataArr.map((item, index) => {
str += `<img src="${item.small}" alt="${index}">`
})
str += `<input type="button" value="+">
</section>`;
artEle.innerHTML = str;
//先生成在获取节点
let bigEle = document.getElementsByClassName("bigimg")[0];
bigEle.style.backgroundImage = `url('${dataArr[0].big}')`;
}
renderImg();
//获取小图父节点、中图、大图的节点
let bigEle = document.getElementsByClassName("bigimg")[0];
bigEle.style.display = "none";
let middleimg = document.getElementsByClassName("middleimg")[0];
let secEle = document.getElementsByTagName("section");
let boxEle = document.getElementsByClassName("box")[0];
//生成蒙板
function meng() {
//生成蒙板节点
let asiEle = document.createElement("aside");
asiEle.className = "meng";
asiEle.style.width = (430 / 800) * 430 + "px";
asiEle.style.height = (430 / 800) * 430 + "px";
asiEle.style.backgroundImage = `url("./images/bg.png")`;
asiEle.style.position = "absolute";
asiEle.style.left = 0;
asiEle.style.top = 0;
asiEle.style.display = "none";
//添加蒙板到节点
boxEle.appendChild(asiEle);
}
meng();
//点击小图片,中图和大图进行更换
secEle[1].addEventListener("click", e => {
let event = e || window.event;
//获取被点击下图的下标
if (event.target.localName == "img") {
bigEle.style.backgroundImage = `url(${dataArr[event.target.alt].big})`;
middleimg.src = dataArr[event.target.alt].middle;
//点击加号或减号时判断中途的下标
middleimg.alt = event.target.alt;
}
if (event.target.value == "+") {
//获取当前图片下标
let index = ++middleimg.alt;
//判断是不是最后一张,如果是则停止
index = index >= dataArr.length - 1 ? dataArr.length - 1 : index;
bigEle.style.backgroundImage = `url(${dataArr[index].big})`;
middleimg.src = dataArr[index].middle;
}
if (event.target.value == "-") {
//获取当前图片下标
let index = --middleimg.alt;
//判断是不是最后一张,如果是则停止
index = index <= 0 ? 0 : index;
bigEle.style.backgroundImage = `url(${dataArr[index].big})`;
middleimg.src = dataArr[index].middle;
}
});
//蒙板的显示、隐藏、移动、大图的移动
boxEle.addEventListener("mouseover", e => {
let mengEle = document.getElementsByClassName("meng")[0];
mengEle.style.display = "block";
bigEle.style.display = "block";
boxEle.addEventListener("mousemove", e => {
let event = e || window.event;
mengEle.style.left = event.clientX - boxEle.offsetLeft - mengEle.clientWidth / 2 + "px";
mengEle.style.top = event.clientY - boxEle.offsetTop - mengEle.clientHeight / 2 + "px";
if (parseInt(mengEle.style.left) <= 0) {
mengEle.style.left = 0;
}
if (parseInt(mengEle.style.top) <= 0) {
mengEle.style.top = 0;
}
if (parseInt(mengEle.style.left) >= boxEle.clientWidth - mengEle.clientWidth) {
mengEle.style.left = boxEle.clientWidth - mengEle.clientWidth + "px";
}
if (parseInt(mengEle.style.top) >= boxEle.clientHeight - mengEle.clientHeight) {
mengEle.style.top = boxEle.clientHeight - mengEle.clientHeight + "px";
}
bigEle.style.backgroundPositionX = -((800 / 430) * parseInt(mengEle.style.left)) + "px";
bigEle.style.backgroundPositionY = -((800 / 430) * parseInt(mengEle.style.top)) + "px";
})
boxEle.addEventListener("mouseout", e => {
mengEle.style.display = "none";
bigEle.style.display = "none";
})
})
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>放大镜</title>
<link rel="stylesheet" href="./index.css">
</head>
<body>
<article>
</article>
<input type="button" value="加入购物车">
<script src="./index.js"></script>
</body>
</html>
标签:index,style,dataArr,放大镜,mengEle,let,详情,淘宝,event 来源: https://blog.csdn.net/V________/article/details/112483182