html静态网页:放大镜
【摘要】
功能分三个模块: 1-鼠标跟随 2-处理越界 3-方大 效果:
文件架构: 学习交流群:970353786 第一部分代码:
<!DOCTYPE html>
<html>
...
功能分三个模块:
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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
第二部分代码:
<!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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
第三部分代码:
<!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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
文章来源: chuanchuan.blog.csdn.net,作者:川川菜鸟,版权归原作者所有,如需转载,请联系作者。
原文链接:chuanchuan.blog.csdn.net/article/details/119286537
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)