使用class定义函数
<script>
class Fun {
constructor(a) {
this.name = a;
console.log(this);
}
init() {}
move() {}
}
new Fun("aa");
</script>
放大镜案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
body {
height: 2000px;
}
.box {
width: 450px;
margin: 50px;
display: flex;
flex-direction: column;
position: relative;
}
.box>.show {
width: 420px;
height: 430px;
border: 1px solid #333;
position: relative;
text-align: center;
}
.box>.show>img {
width: 100%;
height: 100%;
}
.box>.show>.mask {
width: 150px;
height: 150px;
background-color: yellow;
opacity: 0.4;
position: absolute;
top: 0px;
left: 0px;
display: none;
}
.box>.list {
width: 100%;
flex: 1;
display: flex;
justify-content: flex-start;
align-items: center;
box-sizing: border-box;
padding: 20px;
}
.box>.list>p {
width: 60px;
height: 60px;
border: 2px solid #333;
margin-right: 10px;
text-align: center;
}
.box>.list>p.active {
border-color: red;
}
.box>.enlarge {
width: 500px;
height: 500px;
position: absolute;
top: 0px;
left: 450px;
background-size: 750px 1000px;
background-position: 0 0;
background-repeat: no-repeat;
border: 1px solid #ccc;
background-image: url(./images/img1_750x1000.jpg);
display: none;
}
</style>
<script src="./enlarge.js"></script>
</head>
<body>
<div class="box">
<div class="show">
<img src="./images/img1_430x430.jpg" />
<div class="mask"></div>
</div>
<div class="list">
<p class="active">
<img midelImg="./images/img1_430x430.jpg" bigImg="./images/img1_750x1000.jpg"
src="./images/img1_60x60.jpg" />
</p>
<p>
<img midelImg="./images/img2_430x430.jpg" bigImg="./images/img2_750x1000.jpg"
src="./images/img2_60x60.jpg" />
</p>
<p>
<img midelImg="./images/img3_430x430.jpg" bigImg="./images/img3_750x1000.jpg"
src="./images/img3_60x60.jpg" />
</p>
<p>
<img midelImg="./images/img4_430x430.jpg" bigImg="./images/img4_750x1000.jpg"
src="./images/img4_60x60.jpg" />
</p>
<p>
<img midelImg="./images/img5_430x430.jpg" bigImg="./images/img5_750x1000.jpg"
src="./images/img5_60x60.jpg" />
</p>
</div>
<div class="enlarge"></div>
</div>
<script>
new Enlarge(".box");
</script>
</body>
</html>
class Enlarge{
constructor(ele){
this.ele=document.querySelector(ele);
this.init();
}
init(){
this.show=this.ele.querySelector(".show");
this.showImg = this.show.querySelector("img");
this.mask = this.show.querySelector(".mask");
this.list = this.ele.querySelector(".list");
this.p = this.list.querySelectorAll("p");
this.enlarge = this.ele.querySelector(".enlarge");
this.show.onmousemove=()=>{
this.mask.style.display="block";
this.enlarge.style.display="block";
this.setStyle();
}
this.show.onmouseout=()=>{
this.mask.style.display="none";
this.enlarge.style.display="none";
};
this.show.onmousemove=()=>{
let e=window.event;
this.move(e);
}
this.p.forEach((item,index)=>{
item.onclick=()=>{
this.changeImg(item);
}
})
},
setStyle(){
let style=window.getComputedStyle(this.enlarge).backgroundSize;
let bgx=parseInt(style.split("")[0]);
let bgy=parseInt(style.split("")[1]);
let maskW = this.mask.offsetWidth;
let maskH = this.mask.offsetHeight;
let showW = this.show.offsetWidth;
let showH = this.show.offsetHeight;
let width = (bgx * maskW) / showW;
let height = (bgy * maskH) / showH;
this.enlarge.style.width = width + "px";
this.enlarge.style.height = height + "px";
}
move(e){
let x=e.pageX-this.ele.offsetLeft-this.mask.offsetWidth/2;
let y=e.pageY-this.ele.offsetTop-this.mask.offsetHeight/2;
if(x<=0){
x=0;
}
if(y<=0){
y=0;
}
if (x >= this.show.offsetWidth - this.mask.offsetWidth) {
x = this.show.offsetWidth - this.mask.offsetWidth;
}
if (y >= this.show.offsetHeight - this.mask.offsetHeight) {
y = this.show.offsetHeight - this.mask.offsetHeight;
}
this.mask.style.left = x + "px";
this.mask.style.top = y + "px";
let style = window.getComputedStyle(this.enlarge).backgroundSize;
let bgx = parseInt(style.split(" ")[0]);
let bgy = parseInt(style.split(" ")[1]);
let bpx = (bgx * x) / this.show.offsetWidth;
let bpy = (bgy * y) / this.show.offsetHeight;
this.enlarge.style.backgroundPosition = `${-bpx}px ${-bpy}px`;
}
changeImg(ele){
for(let i=0;i<this.p.length;i++){
this.p[i].classList.remove("active");
}
ele.classList.add("active");
let img=ele.querySelector("img");
this.showImg.setAttribute("src",img.getAttribute("mideling"));
this.enlarge.style.backgroundImage=`url(${img.getAttribute("bigImg")})`;
}
}