style样式:
<style>
*{
padding: 0;
margin: 0;
list-style: none;
}
.box{
width: 400px;
height: 500px;
margin-left:100px;
}
.m{
width: 400px;
height: 400px;
position: relative;
}
.small ul li{
margin:0 5px;
border:1px solid #0f0;
}
.small ul li img{
vertical-align: middle;
}
.small ul{
width: 400px;
height: 100px;
display:flex;
align-items: center;
justify-content: flex-start;
}
.small ul li.active{
border-color:#f00;
}
.shade{
width: 100px;
height: 100px;
background:rgba(255,255,0,0.5);
position: absolute;
left:0;
top: 0;
display:none;
}
.big{
width: 400px;
height: 400px;
position:absolute;
left:105%;
top:0;
border:solid;
background-image:url("./images/big1.jpg");
background-size:1600px 1600px;
display:none;
background-repeat:no-repeat;
}
.middle{
width: 400px;
height: 400px;
border:1px solid #000;
position:absolute;
left:0;
top:0;
}
.shade:hover{
cursor:move;
}
</style>
body代码:
<!-- 有导航,有内容,有顶部 -->
<div style="height:300px;background:red;"></div>
<div class="box">
<div class="m">
<div class="middle">
<img src="./images/middle1.jpg" alt="">
<div class="shade"></div>
</div>
<div class="big"></div>
</div>
<div class="small">
<ul>
<li class="active"><a href="#"><img src="./images/small1.jpg" alt=""></a></li>
<li><a href="#"><img src="./images/small2.jpg" alt=""></a></li>
</ul>
</div>
</div>
js代码:
function Enlarge(classname){
this.box = document.querySelector("."+classname);
this.m = this.box.querySelector(".m");
this.middleImg = this.box.querySelector(".m img");
this.middle = this.box.querySelector(".middle");
this.shade = this.box.querySelector(".shade");
this.ulis = this.box.querySelectorAll("ul li");
this.big = this.box.querySelector(".big");
var _this = this;
this.middle.onmouseenter = ()=>{
this.over();
}
this.middle.onmouseleave= ()=>{
this.out();
}
for(var i=0;i<this.ulis.length;i++){
this.ulis[i].onclick = function(){
_this.click(this);
}
}
}
Enlarge.prototype.click = function(ele){
for(var i=0;i<this.ulis.length;i++){
this.ulis[i].style.borderColor = "#0f0"
}
ele.style.borderColor = "#f00";
var smallImg = ele.firstElementChild.firstElementChild;
var smallPath = smallImg.getAttribute("src");
var lastIndex = smallPath.lastIndexOf(".");
var suffix = smallPath.slice(lastIndex-1);
var middlePath = "./images/middle" + suffix;
this.middleImg.setAttribute("src",middlePath);
var bigPath = "./images/big"+suffix;
this.big.style.backgroundImage = "url("+bigPath+")"
}
Enlarge.prototype.out = function(){
this.shade.style.display = "none"
this.big.style.display = "none"
}
Enlarge.prototype.over = function(){
this.shade.style.display = "block"
this.big.style.display = "block"
var _this = this;
this.middle.onmousemove=function(e){
var e = e || window.event;
var x = e.pageX;
var y = e.pageY;
var l = x - _this.box.offsetLeft - this.offsetLeft - _this.shade.offsetWidth/2;
if(l<=0){
l=0;
}
if(l>=this.clientWidth - _this.shade.offsetWidth){
l=this.clientWidth - _this.shade.offsetWidth
}
_this.shade.style.left = l + "px";
var t = y - _this.box.offsetTop - this.offsetTop - _this.shade.offsetHeight/2;
if(t<=0){
t = 0;
}
if(t>=this.clientHeight - _this.shade.offsetHeight){
t=this.clientHeight - _this.shade.offsetHeight
}
_this.shade.style.top = t + "px";
_this.fangda(l,t);
}
}
Enlarge.prototype.fangda = function(l,t){
var w = this.middle.clientWidth;
var percentw = l/w;
var style = window.getComputedStyle(this.big).backgroundSize;
var bigW = parseInt(style.split(" ")[0]);
var bigL = percentw * bigW;
var h = this.middle.clientHeight;
var percenth = t/h;
var bigH = parseInt(style.split(" ")[1]);
var bigT = percenth * bigH;
this.big.style.backgroundPosition = `-${bigL}px -${bigT}px`;
}
var enlarge = new Enlarge("box");