代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
*{
margin: 0px;
padding: 0px;
}
#head1 {
width: 2000px;
height:700px;
}
#small{
margin-top: 150px;
margin-left: 350px;
width: 300px;
height: 200px;
border:1px solid #eee;
border-radius: 4px;
position: relative;
}
#small img{
width: 100%;
height: 100%;
}
div {
float: left;
}
#big{
margin-top: 150px;
margin-left: 20px;
width: 300px;
height: 200px;
overflow: hidden;
position: relative;
border:1px solid #eee;
border-radius: 4px;
display: none;
}
#look{
width: 300px;
height: 200px;
position: absolute;
left: 0px;
top:0px;
}
#move{
width: 100px;
height: 100px;
background:#000;
opacity: .5;
position: absolute;
display: none;
left: 0px;
top: 0px;
}
</style>
</head>
<body>
<ul id="head1"></ul>
<script>
(function(){
function Small(width,height,url){
this.width = width;
this.height = height;
this.url = url;
}
Small.prototype.init = function(){
this.createElement();
}
Small.prototype.createElement = function(){
this.smallBox = document.createElement('div');
document.getElementById('head1').appendChild(this.smallBox);
this.smallBox.style.width = this.width;
this.smallBox.style.height = this.height;
this.smallBox.id = 'small';
this.imageBox = document.createElement('img');
this.imageBox.style.width = this.width;
this.imageBox.style.height = this.height;
this.imageBox.src = this.url;
this.smallBox.appendChild(this.imageBox);
this.moveBox = document.createElement('p');
this.moveBox.id = 'move';
this.smallBox.appendChild(this.moveBox);
}
window.Small = Small;
})();
(function(){
function Big(width,height,bigWidth,bigHeight,url){
this.width = width;
this.height = height;
this.bigWidth = bigWidth;
this.bigHeight = bigHeight;
this.url = url;
}
Big.prototype.init = function(){
this.createElement();
}
Big.prototype.createElement = function(){
this.bigBox = document.createElement('div');
document.getElementById('head1').appendChild(this.bigBox);
this.bigBox.style.width = this.width;
this.bigBox.style.height = this.height;
this.bigBox.id = 'big';
this.imageBox = document.createElement('img');
this.imageBox.style.width = this.bigWidth;
this.imageBox.style.height = this.bigHeight;
this.imageBox.id = 'look';
this.imageBox.src = this.url;
this.bigBox.appendChild(this.imageBox);
}
window.Big = Big;
})();
(function(){
function Large(option){
this.width = option.width;
this.height = option.height;
this.bigWidth = option.bigWidth;
this.bigHeight = option.bigHeight;
this.url = option.url;
}
Large.prototype.init = function(){
this.createElement();
this.mouseMove();
}
Large.prototype.createElement = function(){
this.smaller = new Small(this.width,this.height,this.url);
console.log(this.smaller)
this.smaller.init();
this.big = new Big(this.width,this.height,this.bigWidth,this.bigHeight,this.url);
this.big.init();
}
Large.prototype.mouseMove = function(){
this.small = document.getElementById('small');
this.bigger = document.getElementById('big');
this.move = document.getElementById('move');
console.log(move)
this.look = document.getElementById('look');
this.small.onmousemove = function(event){
event = event || window.event;
this.small.style.cursor = 'move';
var move_left = event.clientX - this.small.offsetLeft - this.move.offsetWidth/2;
console.log(move_left)
var move_top = event.clientY - this.small.offsetTop - this.move.offsetHeight/2;
move_left = move_left < 0 ? 0 : move_left;
move_left = move_left > this.small.offsetWidth - this.move.offsetWidth ? this.small.offsetWidth - this.move.offsetWidth : move_left;
move_top = move_top < 0 ? 0 : move_top;
move_top = move_top > this.small.offsetHeight - this.move.offsetHeight ? this.small.offsetHeight - this.move.offsetHeight : move_top;
this.move.style.left = move_left + 'px';
this.move.style.top = move_top + 'px';
var big_x = move_left/(this.small.offsetWidth-this.move.offsetWidth) * (this.look.offsetWidth-this.bigger.offsetWidth);
var big_y = move_top/(this.small.offsetHeight-this.move.offsetHeight) * (this.look.offsetHeight-this.bigger.offsetHeight);
this.look.style.left = -big_x + 'px';
this.look.style.top = -big_y + 'px';
}.bind(this);
this.small.onmouseover = function(){
this.move.style.display = 'block';
this.bigger.style.display = 'block';
}.bind(this);
this.small.onmouseout = function(){
this.move.style.display = 'none';
this.bigger.style.display = 'none';
}.bind(this);
}
window.Large = Large;
})();
var lar = new Large(
{width:'300px',
height:'200px',
bigWidth:'600px',
bigHeight:'400px',
url:'../新建文件夹/3.jpg'});
lar.init();
</script>
</body>
</html>
演示
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/2f5c4ad72c85e6ae1d880069be075081.png)