html
<div id="big">
<img src="./imgs/2.jpg" width="100%" alt="">
<div id="small"></div>
</div>
<div id="list">
<ul>
<li><img src="./imgs/2.jpg" alt=""></li>
<li><img src="./imgs/3.jpg" alt=""></li>
<li><img src="./imgs/4.jpg" alt=""></li>
<li><img src="./imgs/5.jpg" alt=""></li>
</ul>
</div>
<div id="show">
<img id="bigPic" src="./imgs/2.jpg" alt="">
</div>
css
<style>
*{
margin:0px;
padding:0px;
}
li{
list-style:none;
}
#big{
width:400px;
height:400px;
margin-top:70px;
margin-left:100px;
position:relative;
border:1px solid #ccc;
}
#small{
width:160px;
height:160px;
background:yellow;
opacity:0.4;
position:absolute;
top:0px;
left:0px;
display:none;
}
#show{
width:400px;
height:400px;
position:absolute;
top:70px;
left:521px;
overflow:hidden;
display:none;
border:1px solid #ccc;
}
#show img{
position:absolute;
left:-500px;
}
#list{
width:400px;
height:100px;
margin-top:5px;
margin-left:100px;
border:1px solid #ccc;
}
#list ul{
}
#list li{
height:100px;
float:left;
}
#list li img{
height:100%;
}
</style>
js
<script>
var small = document.getElementById('small');
var big = document.getElementById('big');
var show = document.getElementById('show');
var bigPic = document.getElementById('bigPic');
var list = document.getElementById('list');
var bL = big.offsetLeft;
var bT = big.offsetTop;
var bW = big.offsetWidth;
var bH = big.offsetHeight;
var imgW = 0;
var imgH = 0;
var sW = 0;
var sH = 0;
big.onmouseover = function(){
small.style.display = 'block';
show.style.display = 'block';
sW = small.offsetWidth;
sH = small.offsetHeight;
imgW = bigPic.offsetWidth;
imgH = bigPic.offsetHeight;
}
big.onmouseout = function(){
small.style.display = 'none';
show.style.display = 'none';
}
big.onmousemove = function(e){
var y = e.clientY;
var x = e.pageX;
var y = e.pageY;
var l = x - bL - sW/2;
var t = y - bT - sH/2;
var maxL = bW - sW -2;
if(l>=maxL){
l = maxL;
}
if(l<=0){
l = 0;
}
if(t<=0){
t = 0;
}
var maxT = bH - sH - 2;
if(t>=maxT){
t = maxT;
}
small.style.left = l+'px';
small.style.top = t+'px';
var bigL = -imgW/bW*l;
var bigT = -imgH/bH*t;
bigPic.style.left = bigL+'px';
bigPic.style.top = bigT+'px';
}
var imgList = list.children[0].children;
var len = imgList.length;
for(var i=0;i<len;i++){
imgList[i].onclick = function(){
big.children[0].src = this.children[0].src;
bigPic.src = this.children[0].src;
}
}
</script>
放大镜完整代码
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>我是放大镜</title>
<style>
*{
margin:0px;
padding:0px;
}
li{
list-style:none;
}
#big{
width:400px;
height:400px;
margin-top:70px;
margin-left:100px;
position:relative;
border:1px solid #ccc;
}
#small{
width:160px;
height:160px;
background:yellow;
opacity:0.4;
position:absolute;
top:0px;
left:0px;
display:none;
}
#show{
width:400px;
height:400px;
position:absolute;
top:70px;
left:521px;
overflow:hidden;
display:none;
border:1px solid #ccc;
}
#show img{
position:absolute;
left:-500px;
}
#list{
width:400px;
height:100px;
margin-top:5px;
margin-left:100px;
border:1px solid #ccc;
}
#list ul{
}
#list li{
height:100px;
float:left;
}
#list li img{
height:100%;
}
</style>
</head>
<body>
<div id="big">
<img src="./imgs/2.jpg" width="100%" alt="">
<div id="small"></div>
</div>
<div id="list">
<ul>
<li><img src="./imgs/2.jpg" alt=""></li>
<li><img src="./imgs/3.jpg" alt=""></li>
<li><img src="./imgs/4.jpg" alt=""></li>
<li><img src="./imgs/5.jpg" alt=""></li>
</ul>
</div>
<div id="show">
<img id="bigPic" src="./imgs/2.jpg" alt="">
</div>
<script>
var small = document.getElementById('small');
var big = document.getElementById('big');
var show = document.getElementById('show');
var bigPic = document.getElementById('bigPic');
var list = document.getElementById('list');
var bL = big.offsetLeft;
var bT = big.offsetTop;
var bW = big.offsetWidth;
var bH = big.offsetHeight;
var imgW = 0;
var imgH = 0;
var sW = 0;
var sH = 0;
big.onmouseover = function(){
small.style.display = 'block';
show.style.display = 'block';
sW = small.offsetWidth;
sH = small.offsetHeight;
imgW = bigPic.offsetWidth;
imgH = bigPic.offsetHeight;
}
big.onmouseout = function(){
small.style.display = 'none';
show.style.display = 'none';
}
big.onmousemove = function(e){
var y = e.clientY;
var x = e.pageX;
var y = e.pageY;
var l = x - bL - sW/2;
var t = y - bT - sH/2;
var maxL = bW - sW -2;
if(l>=maxL){
l = maxL;
}
if(l<=0){
l = 0;
}
if(t<=0){
t = 0;
}
var maxT = bH - sH - 2;
if(t>=maxT){
t = maxT;
}
small.style.left = l+'px';
small.style.top = t+'px';
var bigL = -imgW/bW*l;
var bigT = -imgH/bH*t;
bigPic.style.left = bigL+'px';
bigPic.style.top = bigT+'px';
}
var imgList = list.children[0].children;
var len = imgList.length;
for(var i=0;i<len;i++){
imgList[i].onclick = function(){
big.children[0].src = this.children[0].src;
bigPic.src = this.children[0].src;
}
}
</script>
</body>
</html>