javascript放大镜效果的简单实现
时间:2024-04-27 15:18:53
这个效果并不难,要点是位置和比例设置,
捕获鼠标位置、判断鼠标位置区域、还有onmouseover事件、onmousemove事件、onmouseout事件
设置显示大图的比例,小图上显示的切图比例都要弄准确点,最好是2倍啦,4倍啦。
主要注意宽度,我这里的图片m.jpg是1440X900的....
<!DOCTYPE html PUBLIC "-//W3C//DTD Xhtml 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>放大镜效果</title>
<style type="text/css">
*{margin:0;padding:0;}
#smallimg{width:360px;float:left;position:relative;border:1px solid red;}
#smallimg img{ width:360px;}
#bigimg{float:left;width:400px;height:400px;margin-left:40px;border:1px solid #ccc;display:none;}
#showimg{width:100px;height:100px;background:#fff;cursor:move; position:absolute;border:1px solid #666;opacity:0.5;filter:alpha(opacity=50);display:none;}
</style>
</head>
<body>
<div id="smallimg">
<img src="jq/m.jpg" alt=""/>
<div id="showimg"> </div>
</div>
<div id="bigimg"> </div>
<script type="text/javascript">
var $=function(id){return typeof id=="string"?document.getElementById(id):id}
var smallimg = $("smallimg");
var showimg = $("showimg");//滤镜图片
var bigimg = $("bigimg");
var small_url = smallimg.getElementsByTagName("img")[0].getAttribute("src");
var show_half = maxWidth = maxHeight = 0;
smallimg.onmouseover = function(){
showimg.style.display = "block";
bigimg.style.display = "inline";
show_half = showimg.offsetHeight/2;
maxWidth = smallimg.clientWidth - showimg.offsetWidth;
maxHeight = smallimg.clientHeight - showimg.offsetHeight;
//上面两个变量指明showimg允许活动的区域
};
smallimg.onmousemove = function(e){
var e=window.event?window.event:e;
var num=bigimg.clientWidth/showimg.clientWidth;
var Top = e.clientY - smallimg.offsetTop - show_half;
var Left = e.clientX - smallimg.offsetLeft - show_half;
//获取当前移动的showimg位置 计算方法是 鼠标坐标 - 最外面容器的坐标 - 盒子的宽(高)的/2
Top = Top<0?0:Top>maxHeight?maxHeight:Top;
Left = Left<0?0:Left>maxWidth?maxWidth:Left;
showimg.style.top = Top + "px";
showimg.style.left = Left + "px";
bigimg.style.background = "url("+small_url+") -"+Left*num+"px -"+Top*num+"px no-repeat";
};
smallimg.onmouseout = function(){
showimg.style.display="none";
bigimg.style.background ="";
bigimg.style.display="none"
};
</script>
</body>
</html>
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MySQL8数据库安装及SQL语句详解
十个免费的web前端开发工具详细整理
![](https://img.aspxhome.com/file/2023/3/56013_0s.jpg)
sql语句查询数据库中的表名/列名/主键/自动增长值实例
python PaddleOCR库用法及知识点详解
Perl中的控制结构学习笔记
java如何使用正则表达式限制特殊字符的个数
Python中生成器和迭代器的区别详解
聊聊Python pandas 中loc函数的使用,及跟iloc的区别说明
![](https://img.aspxhome.com/file/2023/8/99268_0s.jpg)
python使用append合并两个数组的方法
Python读取txt文件数据的方法(用于接口自动化参数化数据)
![](https://img.aspxhome.com/file/2023/9/66419_0s.png)
matlab画三维图像的示例代码(附demo)
![](https://img.aspxhome.com/file/2023/9/131869_0s.jpg)
MySql树形结构(多级菜单)查询设计方案
![](https://img.aspxhome.com/file/2023/4/116794_0s.png)
golang 自旋锁的实现
解决vue脚手架项目打包后路由视图不显示的问题
Python import自己的模块报错问题及解决
![](https://img.aspxhome.com/file/2023/6/81466_0s.png)
基于Python制作打地鼠小游戏
Mysql性能优化案例研究-覆盖索引和SQL_NO_CACHE
![](https://img.aspxhome.com/file/2023/3/107893_0s.png)
Python常见数据结构之栈与队列用法示例
javascript实现页面的实时时钟显示示例
![](https://img.aspxhome.com/file/2023/6/136926_0s.png)
Python利用matplotlib绘制约数个数统计图示例
![](https://img.aspxhome.com/file/2023/4/63564_0s.png)