若未使用JQ,请先配置jq环境
http://jquery.com/download/
简易放大镜功能
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
#contain{
position: absolute;
width: 500px;
height: 500px;
border: 2px solid #000000;
}
.dd{
position: absolute;
width: 200px;
height: 200px;
float: left;
border: 1px solid yellow;
}
img{
width: 200px;
height: 200px;
}
#div2{
margin-left: 300px;
margin-top:1px;
}
#div3{
margin-left: 0px;
margin-top:300px;
}
#div4{
margin-left: 300px;
margin-top:300px;
}
#big{
position:absolute;
width: 400px;
height: 400px;
background-color: green;
display: none;
border: 1px solid blue;
}
</style>
</head>
<body id="test">
<div id="contain">
<div class="dd" id="div1"><img src="gou.jpg"></div>
<div class="dd" id="div2"><img src="niu.jpg"></div>
<div class="dd" id="div3"><img src="she.jpg"></div>
<div class="dd" id="div4"><img src="shu.jpg"></div>
</div>
<div id="big"></div>
<script type="text/javascript" src="../node_modules/jquery/dist/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#div1").mousemove(function(e) {
var x = e.originalEvent.x || e.originalEvent.layerX || 0;
var y = e.originalEvent.y || e.originalEvent.layerY || 0;
var xx=x+300;
var yy=y+50;
$('#big').css({'background-image':'url(gou.jpg)','background-size':'100% 100%','display':'block'});
$('#big').css('marginLeft',xx);
$('#big').css('marginTop',yy);
})
$("#div1").mouseout(function() {
$('#big').css({'display':'none'});
})
$("#div2").mousemove(function(e) {
var x = e.originalEvent.x || e.originalEvent.layerX || 0;
var y = e.originalEvent.y || e.originalEvent.layerY || 0;
var xx=x+300;
var yy=y+50;
$('#big').css({'background-image':'url(niu.jpg)','background-size':'100% 100%','display':'block'});
$('#big').css('marginLeft',xx);
$('#big').css('marginTop',yy);
})
$("#div2").mouseout(function() {
$('#big').css({'display':'none'});
})
$("#div3").mousemove(function(e) {
var x = e.originalEvent.x || e.originalEvent.layerX || 0;
var y = e.originalEvent.y || e.originalEvent.layerY || 0;
var xx=x+300;
var yy=y-400;
$('#big').css({'background-image':'url(she.jpg)','background-size':'100% 100%','display':'block'});
$('#big').css('marginLeft',xx);
$('#big').css('marginTop',yy);
})
$("#div3").mouseout(function() {
$('#big').css({'display':'none'});
})
$("#div4").mousemove(function(e) {
var x = e.originalEvent.x || e.originalEvent.layerX || 0;
var y = e.originalEvent.y || e.originalEvent.layerY || 0;
var xx=x+300;
var yy=y-400;
$('#big').css({'background-image':'url(shu.jpg)','background-size':'100% 100%','display':'block'});
$('#big').css('marginLeft',xx);
$('#big').css('marginTop',yy);
})
$("#div4").mouseout(function() {
$('#big').css({'display':'none'});
})
})
</script>
</body>
</html>