仿京东的放大镜
<!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;
}
#box{
width: 450px;
height: 450px;
position: relative;
border: 1px solid salmon;
}
</style>
</head>
<body>
<div id="box">
<!-- <div class="small" id="small">
<img src="images/11.jpg" alt="">
<div class="cover" id="cover"></div>
</div>
<div class="big" id="big">
<img src="images/10.jpg" alt="">
</div> -->
</div>
<script>
(function(){
function Small(width,height){
this.width=width|| 450
this.height=height|| 450
}
Small.prototype.init=function(box){
var small = document.createElement('div')
var img = document.createElement('img')
var box = document.getElementById('box')
img.src = 'images/11.jpg'
small.style.width = this.width + 'px'
small.style.height = this.height + 'px'
box.append(small)
small.append(img)
}
window.Small = Small
})();
(function(){
function Big(width,height) {
this.width = width || 540
this.height = height || 540
}
Big.prototype.init = function(box){
var big = document.createElement('div')
var img = document.createElement('img')
var box = document.getElementById('box')
img.src = 'images/10.jpg'
img.style.position = 'absolute'
img.style.left = 0 + 'px'
img.style.top = 0 + 'px'
big.style.width = this.width + 'px'
big.style.height = this.height + 'px'
big.style.position = 'absolute'
big.style.border = '1px solid salmon'
big.style.left = 540 + 'px'
big.style.top = 50 + 'px'
big.style.overflow = 'hidden'
big.style.display = 'none'
box.append(big)
big.append(img)
}
window.Big = Big
})();
(function(){
function Cover(width,height){
this.width=width|| 300
this.height=height|| 300
}
Cover.prototype.init=function(box,small){
var cover = document.createElement('div')
var box = document.getElementById('box')
cover.style.width = this.width + 'px'
cover.style.height = this.height + 'px'
cover.style.position = 'absolute'
cover.style.background = 'rgba(255,246,143,0.5)'
cover.style.display = 'none'
cover.style.left = 0 + 'px'
cover.style.top = 0 + 'px'
box.children[0].append(cover)
}
window.Cover = Cover
})();
(function(){
var small1 = new Small(450,450)
small1.init();
var cover1 = new Cover(300,300)
cover1.init();
var big1 = new Big(540,540)
big1.init();
var box = document.getElementById('box')
var coverBox = box.children[0].children[1]
var bigBox = box.children[1]
var bigImg = box.children[1].children[0]
var small = box.children[0]
box.onmouseover = function (cover,big) {
coverBox.style.display = 'block'
bigBox.style.display = 'block'
}
box.onmouseout = function (cover,big) {
coverBox.style.display = 'none'
bigBox.style.display = 'none'
}
box.onmousemove = function(e){
var e = window.event || e
var coverPositionX = e.clientX - 150
var coverPositionY = e.clientY - 150
if(coverPositionX<=0){
coverPositionX = 0
}
if(coverPositionX>=150){
coverPositionX = 150
}
if(coverPositionY<=0){
coverPositionY=0
}
if(coverPositionY>=150){
coverPositionY=150
}
coverBox.style.left = coverPositionX +'px'
coverBox.style.top = coverPositionY +'px'
var imgMaxDistance = bigImg.offsetWidth- bigBox.offsetWidth
var coverMaxDistance = small.offsetWidth - coverBox.offsetWidth
bigImg.style.left = - imgMaxDistance*coverPositionX /coverMaxDistance + 'px'
bigImg.style.top = - imgMaxDistance*coverPositionY /coverMaxDistance + 'px'
}
})();
</script>
</body>
</html>