jquery写的两种放大镜效果,没有使用到插件。调理和思路清晰。不是使用面向对象方式写的,初学者较容易看懂。废话不多说,看代码。图片这里就不上传了,大家自己找下。最好是找到比例的,这样效果比较好。
<body>
<div id="father">
<div id="container">
<img src="/UploadFiles/2021-04-02/400_1.jpg">
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="/UploadFiles/2021-04-02/400_1.jpg">
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>
标签:
jquery,放大镜
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“实现jquery放大镜的两种方法”评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新动态
2025年11月11日
2025年11月11日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]