原生js写的放大镜效果

时间:2024-04-30 08:51:13 

我的大体思路是:时时监听鼠标的坐标,当鼠标移动时,透明层随着鼠标移动,大图片相对透明层的移动而移动。不废话了,看代码。


<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>放大镜</title>
<meta name="Keywords" content="">
<meta name="Description" content="">
<style type="text/css">
/*重置{*/
html{color:#000;background:#fff;}
body,div{padding:0;margin:0;}
img{border:none;}
/*}重置*/
.outer{width:200px;height:150px;position:relative;margin:20px auto;}
.inner{width:80px;height:60px;background:#f55;position:absolute;opacity:0.5;filter:alpha(opacity=50);left:0;top:0;cursor:pointer;}
.aa{width:320px;height:240px;position:relative;border:1px red solid;margin:20px auto;overflow:hidden;}
.imgs{position:absolute;}
.outer img{width:200px;height:150px;}
</style>
</head>
<body>
<div>
<div class="outer" id="outer">
<img src="images/pobabyb.gif" alt="pobaby小图"/>
<div class="inner" id="inner"></div>
</div>
<div class="aa" id="aa">
<div class="imgs" id="imgs" ><img src="images/pobabyb.gif" alt="pobaby大图"/></div>
</div>
</div>
<script type="text/javascript">
var outer=document.getElementById("outer");
var inner=document.getElementById("inner");
var aa=document.getElementById("aa");
var imgs=document.getElementById("imgs");
var x,y,n=false;
inner.onmousedown=test1;//如果把inner改为document,鼠标在窗口任意位置点击,图片都会跟随
document.onmousemove=test2;//document如果改为outer,鼠标在outer内才起作用
document.onmouseup=test3;
function test1(event){//鼠标按下时方法
var event=event || window.event;//调试兼容,各个浏览器认识event有差别.
n=true;//当n=true(n的值可随便设定)时,假定为鼠标按下的事件
x=event.clientX-inner.offsetLeft;//鼠标在透明层的相对横坐标=鼠标坐标-方块左边距
y=event.clientY-inner.offsetTop;//鼠标在透明层的相对纵坐标=鼠标坐标-方块上边距
}
function test2(event){//鼠标移动时方法
var event=event || window.event;
if(n==true){
////////鼠标移动范围
inner.style.left=event.clientX-x+"px";
inner.style.top=event.clientY-y+"px";
////////图片移动范围
imgs.style.left=-4*parseInt(inner.style.left)+"px";
imgs.style.top=-4*parseInt(inner.style.top)+"px";
////////////////////////////限定鼠标移动的范围
if(parseInt(inner.style.left)<0){
inner.style.left=0+"px";
}
if(parseInt(inner.style.top)<0){
inner.style.top=0+"px";
}
if(parseInt(inner.style.left)>outer.clientWidth-inner.clientWidth){
inner.style.left=outer.clientWidth-inner.clientWidth+"px";
}
if(parseInt(inner.style.top)>outer.clientHeight-inner.clientHeight){
inner.style.top=outer.clientHeight-inner.clientHeight+"px";
}
//////////////////////////////限定图片移动的范围
if(parseInt(imgs.style.left)>0){
imgs.style.left=0+"px";
}
if(parseInt(imgs.style.top)>0){
imgs.style.top=0+"px";
}
if(parseInt(imgs.style.left)<-4*(outer.clientWidth-inner.clientWidth)){
imgs.style.left=-4*parseInt(outer.clientWidth-inner.clientWidth)+"px";
}
if(parseInt(imgs.style.top)<-4*(outer.clientHeight-inner.clientHeight)){
imgs.style.top=-4*parseInt(outer.clientHeight-inner.clientHeight)+"px";
}
}
}
function test3(){//鼠标松开时方法
n=false;
}
</script>
</body>
</html>


原生js写的放大镜效果

标签:放大镜
0
投稿

猜你喜欢

  • Python做文本按行去重的实现方法

    2021-12-31 17:41:07
  • python使用openpyxl库读写Excel表格的方法(增删改查操作)

    2021-11-29 01:22:43
  • Python简单实现TCP包发送十六进制数据的方法

    2021-04-26 06:00:04
  • Python读取csv文件分隔符设置方法

    2021-10-22 16:59:47
  • win10 安装mysql 8.0.18-winx64的步骤详解

    2024-01-24 06:54:04
  • Go之interface的具体使用

    2024-02-08 18:58:41
  • python四种出行路线规划的实现

    2022-10-01 18:46:09
  • Python StringIO模块实现在内存缓冲区中读写数据

    2021-12-22 08:23:13
  • 解决新django中的path不能使用正则表达式的问题

    2021-06-09 18:45:21
  • asp如何编写sql语句来查询|搜索数据记录

    2008-10-09 12:35:00
  • Python递归函数特点及原理解析

    2023-12-02 19:40:40
  • PyQt QCombobox设置行高的方法

    2021-10-15 19:50:20
  • django中间键重定向实例方法

    2021-04-17 12:34:02
  • Python Pytorch深度学习之Tensors张量

    2023-05-04 12:34:54
  • python3+selenium自动化测试框架详解

    2022-01-29 18:26:01
  • Go语言带缓冲的通道实现

    2024-02-08 18:34:13
  • Z-Blog实现摘要图文混排效果的方法

    2009-02-23 13:54:00
  • MySQL多表操作的外键约束教程

    2024-01-26 01:58:56
  • python的numpy模块实现逻辑回归模型

    2022-10-01 07:05:59
  • python如何正确的操作字符串

    2023-12-28 02:46:30
  • asp之家 网络编程 m.aspxhome.com