JavaScript实现图片放大镜效果:参数详解与代码示例

作者:纸飞机

时间:2026-10-25

浏览:291

本文介绍如何使用JavaScript实现简单的图片放大效果。详细解析了berviary、expand、clip和opacity等关键参数的配置方法,提供了两种不同模式的完整JS调用代码及对应的HTML结构,帮助开发者快速集成图片局部放大功能。

文章目录

图片放大效果实现概述

实现一个基础的图片局部放大功能:


核心参数配置详解

核心参数配置说明:


1. berviary:字符串类型,指定需要被放大的目标图片元素的ID。


2. expand:对象类型,定义放大显示区域的配置。若未设置此参数或其ID未指定,系统将默认启用光标跟随模式,并自动创建一个用于展示放大内容的div元素。


3. clip:对象类型,设定裁剪区域的大小,即当鼠标悬停在图片上时,高亮显示需要放大的具体区域范围。


4. opacity:浮点数类型,控制放大时遮罩区域的透明度。


案例一:带自定义放大区域的实现

演示案例一:



JavaScript调用代码:


具体代码如下:

 
$E({berviary:"currentPic",expand:{id:"expand",style:{}},clip:{width:"50px",height:"50px"},opacity:"0.2"});

HTML结构代码:


具体代码如下:

 
<div>
<div class="img_warp" id="img_warp">
<img src="1.jpg" alt="" class="current" id="currentPic" />
</div>
<div class="expand" id="expand" style="border: 1px solid #555; width:400px; height:400px;margin:-200px auto 0 auto; overflow:hidden;"></div>
</div>

效果预览:


 <style type="text/css"> .img_warp {width:318px; _height:199px; _overflow:hidden; border:1px solid #ccc;padding:0px;margin-left:0px; margin-top:0px; position:absolute;top:0px; left:0px;} .img_warp{position:relative;top:0px; left:0px;/*cursor:crosshair*/} .img_warp .current{ width:318px;height:199px} </style>  <div id="img_warp" class="img_warp"><img src="/uploads/20261025/5e47cfca894590c38c714324aef179e36cb3c5b389baa217aef780645fc9c85a.jpg" /> </div> <div style="border-bottom: #555 1px solid; border-left: #555 1px solid; margin: -200px 0px 0px 400px; width: 400px; height: 400px; overflow: hidden; border-top: #555 1px solid; border-right: #555 1px solid" id="expand" class="expand"></div> </div>  

[Ctrl+A 全选 注:引入外部Js需再刷新一下页面才能执行]


案例二:默认光标跟随模式的实现

演示案例二:



JavaScript调用代码:


具体代码如下:

 
$E({berviary:"currentPic",clip:{width:"100px",height:"100px"},opacity:"0.4"});

HTML结构代码:


具体代码如下:

 
<div>
<div class="img_warp" id="img_warp">
<img src="1.jpg" alt="" class="current" id="currentPic" />
</div>
</div>

效果预览:


 <style type="text/css"> .img_warp {width:318px; _height:199px; _overflow:hidden; border:1px solid #ccc;padding:0px;margin-left:0px; margin-top:0px; position:absolute;top:0px; left:0px;} .img_warp{position:relative;top:0px; left:0px;/*cursor:crosshair*/} .img_warp .current{ width:318px;height:199px} </style>  <div id="img_warp" class="img_warp"><img src="/uploads/20261025/5e47cfca894590c38c714324aef179e36cb3c5b389baa217aef780645fc9c85a.jpg" /> </div> </div>  

[Ctrl+A 全选 注:引入外部Js需再刷新一下页面才能执行]

实用工具推荐

电子包浆

绿化、压缩、噪点和虚拟水印,自由调节做旧程度。

使用 ›
图片压缩

选择画质和输出格式,预览压缩前后的大小。

使用 ›
相关文章