功能分三個子產品:
1-滑鼠跟随
2-處理越界
3-方大
效果:

學習交流群:970353786
第一部分代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
#box{
width: 400px;
height: 400px;
border: 1px solid black;
margin: 100px auto;
position: relative;
}
#glass{
width: 50px;
height: 50px;
background-color: green;
left: 100px;
top: 50px;
position: absolute;
}
</style>
</head>
<body>
<div id="box">
<div id="glass">
</div>
</div>
</body>
<script type="text/javascript">
var box = document.querySelector('#box');
var glass = document.querySelector('#glass');
// box.onclick = function(e){
box.onmousemove = function(e){
var x = e.pageX - box.offsetLeft;
var y = e.pageY - box.offsetTop;
x -= parseInt(getComputedStyle(glass).width)/2
y -= parseInt(getComputedStyle(glass).height)/2
glass.style.left = x + "px";
glass.style.top = y + "px";
}
</script>
</html>
第二部分代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
#box{
width: 400px;
height: 400px;
border: 1px solid black;
margin: 100px auto;
position: relative;
}
#glass{
width: 50px;
height: 50px;
background-color: green;
left: 100px;
top: 50px;
position: absolute;
display: none;
}
</style>
</head>
<body>
<div id="box">
<div id="glass">
</div>
</div>
</body>
<script type="text/javascript">
var box = document.querySelector('#box');
var glass = document.querySelector('#glass');
// box.onclick = function(e){
box.onmousemove = function(e){
var x = e.pageX - box.offsetLeft;
var y = e.pageY - box.offsetTop;
x -= parseInt(getComputedStyle(glass).width)/2
y -= parseInt(getComputedStyle(glass).height)/2
//越界處理
//左越界處理
if (x < 0) {
x = 0;
}
//上越界處理
if (y < 0) {
y = 0;
}
//右越界處理
var maxLeft = parseInt(getComputedStyle(this).width) - parseInt(getComputedStyle(glass).width)
if (x > maxLeft ) {
x = maxLeft;
}
//下越界處理
var maxHeight = parseInt(getComputedStyle(this).height) - parseInt(getComputedStyle(glass).height)
if (y > maxHeight ) {
y = maxHeight;
}
console.log(x,y);
glass.style.left = x + "px";
glass.style.top = y + "px";
}
box.onmouseover = function(){
glass.style.display = "block"
}
box.onmouseout = function(){
glass.style.display = "none"
}
</script>
</html>
第三部分代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
*{
padding: 0;
margin: 0;
}
.box{
width: 800px;
margin: 50px auto;
}
.left{
width: 250px;
height: 250px;
border: 1px solid black;
float: left;
margin-right:30px ;
position: relative;
}
.left img{
width: 100%;
}
.right{
width: 500px;
height: 500px;
border: 1px solid black;
overflow: hidden;
float: left;
display: none;
position: relative;
}
.right img{
position: absolute;
}
.glass{
/* 如何計算放大鏡寬高?? */
/*
已知:小div寬高 = 小圖寬高 = 250*250
大div寬高 = 500*500
大圖寬高 = 800*800
計算公式:
放大鏡寬 / 小div寬 = 大div寬 / 大圖寬
==>放大鏡寬 = 大div寬 / 大圖寬 * 小div寬
= 500/800*250
=1250/8 = 156.125
*/
width:157px;
height:157px;
background-color: blue;
position: absolute;
left: 0;
top: 0;
opacity: 0.3;
display: none;
}
</style>
</head>
<body>
<div class="box">
<div class="left">
<img src="1.jpeg" >
<div class="glass"></div>
</div>
<div class="right">
<img src="2.jpeg" >
</div>
</div>
</body>
<script type="text/javascript">
var leftDiv = document.querySelector('.left');
var glass = document.querySelector('.glass');
var rightDiv = document.querySelector('.right');
var bigImg = document.querySelector('.right img');
leftDiv.onmousemove = function(e){
var x = e.pageX - this.offsetLeft;
var y = e.pageY - this.offsetTop;
x -= parseInt(getComputedStyle(glass).width)/2
y -= parseInt(getComputedStyle(glass).height)/2
//越界處理
//左越界處理
if (x < 0) {
x = 0;
}
//上越界處理
if (y < 0) {
y = 0;
}
//右越界處理
var maxLeft = parseInt(getComputedStyle(this).width) - parseInt(getComputedStyle(glass).width)
if (x > maxLeft ) {
x = maxLeft;
}
//下越界處理
var maxHeight = parseInt(getComputedStyle(this).height) - parseInt(getComputedStyle(glass).height)
if (y > maxHeight ) {
y = maxHeight;
}
console.log(x,y);
glass.style.left = x + "px";
glass.style.top = y + "px";
//大圖移動,需要計算移動的距離
/* 如何計算大圖移動的距離?? */
/*
已知:小div寬高 = 小圖寬高 = 250*250
大div寬高 = 500*500
大圖寬高 = 800*800
放大鏡寬高= 157*157
放大鏡移動的x和y
計算大圖移動的距離left和top????
計算公式:
放大鏡移動x / 大圖移動的x = 小圖寬 / 大圖寬
===>大圖移動的x = 大圖寬 / 小圖寬 * 放大鏡移動x
*/
var left = -x * 800/250;
var top = -y * 800/250;
bigImg.style.left = left + "px";
bigImg.style.top = top + "px";
}
leftDiv.onmouseover = function(){
glass.style.display = "block";
rightDiv.style.display = "block";
}
leftDiv.onmouseout = function(){
glass.style.display = "none";
rightDiv.style.display = "none";
}
</script>
</html>