实现jquery放大镜的两种方法

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

实现jquery放⼤镜的两种⽅法
jquery写的两种放⼤镜效果,没有使⽤到插件。

调理和思路清晰。

不是使⽤⾯向对象⽅式写的,初学者较容易看懂。

废话不多说,看代码。

图⽚这⾥就不上传了,⼤家⾃⼰找下。

最好是找到⽐例的,这样效果⽐较好。

<body>
<div id="father">
<div id="container">
<img src="img/400_1.jpg" style="display: block;">
<img src="img/400_2.jpg" >
<div class="shade"></div>
</div>
<div class="small first"><img src="img/50_1.jpg"></div>
<div class="small second"><img src="img/50_2.jpg"></div>
</div>
<div class="big">
<img src="img/800_1.jpg" style="display: block;">
<img src="img/800_2.jpg">
</div>
</body>
css代码
*{padding: 0; margin: 0;}
#father .small{width: 50px; height: 50px; border: 2px solid #ccc; bottom: 0; position: absolute;}
#father .second{left: 70px;}
.third{left: 140px;}
#father{position: relative; top: 100px; left: 50px; height: 460px;}
#container{position: absolute; width: 400px; height: 400px;}
#container img{position: absolute; display: none;}
.shade{width: 200px; height: 200px; position: absolute; background: #000; opacity: 0.4; top: 0;
left: 0; display: none;}
.big{width: 400px; height: 400px; position: absolute; top: 100px; overflow: hidden; left: 500px; display: none;}
.big img{width: 800px; height: 800px; position: absolute; display: none;}
js代码
<script type="text/javascript" src='js/jquery-1.12.4.min.js'></script>
<script type="text/javascript">
$(function () {
changePic('.first',0);
changePic('.second',1);
var shadeWidth = $('.shade').width(),//阴影的宽度
shadeHeight = $('.shade').height(),//阴影的⾼度
middleWidth = $('#container').width(),//容器的宽度
middleHeight = $('#container').height(),//容器的⾼度
bigWidth = $('.big').width(),//放⼤图⽚盒⼦的宽度
bigHeight = $('.big').height(),//放⼤图⽚盒⼦的⾼度
rateX = bigWidth / shadeWidth,//放⼤区和遮罩层的宽度⽐例
rateY = bigHeight / shadeHeight;//放⼤区和遮罩层的⾼度⽐例
//当⿏标移⼊与移出时阴影与放⼤去显现/消失
$('#container').hover(function() {
$('.shade').show();
$('.big').show();
}, function() {
$('.shade').hide();
$('.big').hide();
}).mousemove(function(e) {//当⿏标移动时,阴影和放⼤区图⽚进⾏移动
//记录下光标距离页⾯的距离
var x = e.pageX,
y = e.pageY;
//设置遮罩层的位置
$('.shade').offset({
top: y-shadeHeight/2,
left: x-shadeWidth/2
});
//获取遮罩层相对⽗元素的位置
var cur = $('.shade').position(),
_top = cur.top,
_left = cur.left,
hdiffer = middleHeight - shadeHeight,
wdiffer = middleWidth - shadeWidth;
if (_top < 0) _top = 0;
else if (_top > hdiffer) _top = hdiffer;
if (_left < 0) _left = 0;
else if (_left > wdiffer) _left =wdiffer;
//判断完成后设置遮罩层的范围
$('.shade').css({
top: _top,
left: _left
});
//设置放⼤区图⽚移动
$('.big img').css({
top: - rateY*_top,
left: - rateX*_left
});
});;
//封装的改变图⽚显⽰的函数
function changePic (element,index) {
$(element).click(function() {
$('#container img').eq(index).css('display', 'block').siblings().css('display', 'none');
$('.big img').eq(index).css('display', 'block').siblings().css('display', 'none');
});
}
});
以上是常⽤的,下⾯这个是在原图基础上放⼤的
htm
<body>
<div id="father">
<div id="container">
<img src="img/400_1.jpg" style="display: block;">
<img src="img/400_2.jpg" >
<img src="img/400_3.jpg" >
<div class="shade">
<img src="img/800_1.jpg" style="display: block;">
<img src="img/800_2.jpg">
<img src="img/800_3.jpg">
</div>
</div>
<div class="small first"><img src="img/50_1.jpg"></div>
<div class="small second"><img src="img/50_2.jpg"></div>
<div class="small third"><img src="img/50_3.jpg"></div>
</div>
</body>
css代码
*{padding: 0; margin: 0;}
#father .small{width: 50px; height: 50px; border: 2px solid #ccc; bottom: 0; position: absolute;}
#father .second{left: 70px;}
.third{left: 140px;}
#father{position: relative; top: 100px; left: 50px; height: 460px;}
#container{position: absolute; width: 400px; height: 400px;}
#container img{position: absolute; display: none;}
.shade{width: 200px; height: 200px; position: absolute; top: 0;left: 0; display: none; border-radius: 50%; overflow: hidden; background: #000;} .shade img{display: none; width: 800px; height: 800px; position: absolute;}
js代码
<span style="white-space:pre"> </span><script type="text/javascript" src='js/jquery-1.12.4.min.js'></script>
<script type="text/javascript">
$(function () {
changePic('.first',0);
changePic('.second',1);
changePic('.third',2);
var shadeWidth = $('.shade').width(),//阴影的宽度
shadeHeight = $('.shade').height(),//阴影的⾼度
middleWidth = $('#container').width(),//容器的宽度
middleHeight = $('#container').height(),//容器的⾼度
bigImgWidth = $('.shade img').width(),//放⼤图⽚盒⼦的宽度
bigImgHeight = $('.shade img').height(),//放⼤图⽚盒⼦的⾼度
rateX = bigImgWidth / middleWidth,//放⼤区和遮罩层的宽度⽐例2
rateY = bigImgHeight / middleHeight;//放⼤区和遮罩层的⾼度⽐例2
//当⿏标移⼊与移出时阴影与放⼤去显现/消失
$('#container').hover(function() {
$('.shade').show();
$('.big').show();
}, function() {
$('.shade').hide();
$('.big').hide();
}).mousemove(function(e) {//当⿏标移动时,阴影和放⼤区图⽚进⾏移动
//记录下光标距离页⾯的距离
var x = e.pageX,
y = e.pageY;
//设置遮罩层的位置
$('.shade').offset({
top: y-shadeHeight/2,
left: x-shadeWidth/2
});
//获取遮罩层相对⽗元素的位置
var cur = $('.shade').position(),
_top = cur.top,
_left = cur.left,
hdiffer = middleHeight - shadeHeight,
wdiffer = middleWidth - shadeWidth;
if (_top < 0) _top = 0;
else if (_top > hdiffer) _top = hdiffer;
if (_left < 0) _left = 0;
else if (_left > wdiffer) _left =wdiffer;
//判断完成后设置遮罩层的范围
$('.shade').css({
top: _top,
left: _left
});
//设置放⼤区图⽚移动
$('.shade img').css({
top: - _top*rateY*3/2,
left: - _left*rateX*3/2
});
});;
//封装的改变图⽚显⽰的函数
function changePic (element,index) {
$(element).click(function() {
$('#container img').eq(index).css('display', 'block').siblings().css('display', 'none'); $('.shade img').eq(index).css('display', 'block').siblings().css('display', 'none');
});
}
});
<span style="white-space:pre"> </span></script>。

相关文档
最新文档