-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path产品图片放大镜代码.html
217 lines (211 loc) · 5.79 KB
/
产品图片放大镜代码.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery产品图片放大镜代码</title>
<style>
@charset "utf-8";
body{
margin: 0;
padding: 0;
}
ul{
list-style: none;
padding: 0;
}
#outBox{
width: 1002px;
height: 540px;
margin: 2px auto;
}
.w{
width: 450px;
height: 450px;
}
.leftBox{
width: 450px;
height: 540px;
float: left;
}
.normalBox{
overflow: hidden;
position: relative;
border: 1px solid #f2f2f2;
}
.normalBox .w{
display: none;
}
.botBox{
width: 100%;
height: 90px;
background: white;
overflow: hidden;
}
.botBox ul li{
float: left;
width: 54px;
height: 54px;
border: 2px solid #fff;
border-radius: 5px;
margin-left: 26.5px;
opacity: 0.5;
}
.bord{
border-color: #e4393c !important;
opacity: 1 !important;
}
.moveBox{
width: 200px;
height: 200px;
background: rgba(222,0,0,0.4);
position: absolute;
top: 0;
left: 0;
display: none;
cursor: move;
}
.magBox{
width: 540px;
height: 540px;
border: 1px solid #f2f2f2;
position: relative;
float: left;
overflow: hidden;
display: none;
}
.magBox ul li{
display: none;
position: absolute;
top: 0;
left: 0;
}
#n,.m{
display: block !important;
}
.box{
width: 110px;
margin: auto;
}
</style>
<script src="js/jquery.min.js"></script>
</head>
<body>
<div class="box">
<div>   放大<span class="multiple">1</span>倍</div>
<button style="width: 50px;height: 30px;background: skyblue;outline: none;" class="btn1">+</button>
<button style="width: 50px;height: 30px;background: skyblue;outline: none;" class="btn2">-</button>
</div>
<div id="outBox">
<div class="leftBox">
<div class="normalBox w">
<div class="w" id="n"><img src="img/normal1.jpg"></div>
<div class="w"><img src="img/normal2.jpg"></div>
<div class="w"><img src="img/normal3.jpg"></div>
<div class="w"><img src="img/normal4.jpg"></div>
<div class="w"><img src="img/normal5.jpg"></div>
<div class="moveBox"></div>
</div>
<div class="botBox">
<ul>
<li class="bord"><img src="img/small1.jpg"></li>
<li><img src="img/small2.jpg"></li>
<li><img src="img/small3.jpg"></li>
<li><img src="img/small4.jpg"></li>
<li><img src="img/small5.jpg"></li>
</ul>
</div>
</div>
<div class="magBox">
<ul>
<li class="m"><img src="img/normal1.jpg"></li>
<li><img src="img/normal2.jpg"></li>
<li><img src="img/normal3.jpg"></li>
<li><img src="img/normal4.jpg"></li>
<li><img src="img/normal5.jpg"></li>
</ul>
</div>
</div>
<script>
$(function () {
var sdBoxW = $('.moveBox').css('width');
sdBoxW = parseInt(sdBoxW);//移动层的宽度
var magBoxW = $('.magBox').css('width');
magBoxW = parseInt(magBoxW);//图片放大层的宽度
var normalBoxW = $('.normalBox').css('width');
normalBoxW = parseInt(normalBoxW);//事件绑定层的宽度
var num = 0;//存放下标
//找出放大图片的比例(核心)
var scale = magBoxW/sdBoxW;
//移入normalBox盒子
$('.normalBox').hover(function () {
$('.moveBox').css('display','block');
$('.magBox').css('display','block');
},function () {
$('.moveBox').css('display','none');
$('.magBox').css('display','none');
});
//3、移入leftBox层
$('.leftBox').mouseover(function () {
//给放大的图片和图片层设置宽度;
$('.magBox ul li img').css('width',scale*normalBoxW+'px');
$('.magBox ul li').css({'width':scale*normalBoxW+'px','height':scale*normalBoxW+'px'})
});
//4、设置放大倍数
var n = 1;
function sty() {
$('.moveBox').css({'width':200/n+'px','height':200/n+'px'});
$('.multiple').html(n);
scale = magBoxW/(sdBoxW/n);
}
$('.btn1').click(function () {
n ++;
sty()
});
$('.btn2').click(function () {
if (n==1){
return;
} else {
n --;
sty()
}
}) ;
//1、移入缩小图关联
$('.botBox ul li').attr('index',function (i,e) {
return i;
});
$('.botBox ul li').mouseover(function () {
if ($(this).attr('class')=='bord'){
return;//跳过第一个
}else{
$(this).attr('class','bord').siblings().removeAttr('class');
var index = $(this).attr('index');
//联动normal和magBox中的图片
$('.normalBox .w').eq(index).attr('id','n').siblings().removeAttr('id');
$('.magBox ul li').eq(index).attr('class','m').siblings().removeAttr('class');
num = index;
}
});
//2、鼠标在移动层移动
$('.normalBox').mousemove(function (e) {
var offset = $(this).offset();
var X = e.pageX-offset.left-$('.moveBox').width()/2;
var Y = e.pageY-offset.top-$('.moveBox').height()/2;
if (X<=0){
X=0;
}else if(X>$(this).width()-$('.moveBox').width()){
X = $(this).width()-$('.moveBox').width();
}
if (Y<=0){
Y=0;
}else if(Y>$(this).height()-$('.moveBox').height()){
Y = $(this).height()-$('.moveBox').height();
}
$('.moveBox').css('left',X+'px');
$('.moveBox').css('top',Y+'px');
$('.magBox ul li').eq(num).css('left',-X*scale+'px');
$('.magBox ul li').eq(num).css('top',-Y*scale+'px');
});
})
</script>
</body>
</html>