这个效果并不难,要点是位置和比例设置, 捕获鼠标位置、判断鼠标位置区域、还有onmouseover事件、onmousemove事件、onmouseout事件 设置显示大图的比例,小图上显示的切图比例都要弄准确点,最好是2倍啦,4倍啦。 主要注意宽度,我这里的图片m.jpg是1440X900的.... 复制代码 代码如下: 放大镜效果 <BR>*{margin:0;padding:0;}<BR>#smallimg{width:360px;float:left;position:relative;border:1px solid red;}<BR>#smallimg img{ width:360px;}<BR>#bigimg{float:left;width:400px;height:400px;margin-left:40px;border:1px solid #ccc;display:none;}<BR>#showimg{width:100px;height:100px;background:#fff;cursor:move; position:absolute;border:1px solid #666;opacity:0.5;filter:alpha(opacity=50);display:none;}<BR> <BR>var $=function(id){return typeof id=="string"?document.getElementById(id):id}<BR>var smallimg = $("smallimg");<BR>var showimg = $("showimg");//滤镜图片<BR>var bigimg = $("bigimg");<BR>var small_url = smallimg.getElementsByTagName("img")[0].getAttribute("src");<BR>var show_half = maxWidth = maxHeight = 0;<BR>smallimg.onmouseover = function(){<BR> showimg.style.display = "block";<BR> bigimg.style.display = "inline";<BR> show_half = showimg.offsetHeight/2;<BR> maxWidth = smallimg.clientWidth - showimg.offsetWidth;<BR> maxHeight = smallimg.clientHeight - showimg.offsetHeight;<BR> //上面两个变量指明showimg允许活动的区域<BR>};<BR>smallimg.onmousemove = function(e){<BR> var e=window.event?window.event:e;<BR> var num=bigimg.clientWidth/showimg.clientWidth;<BR> var Top = e.clientY - smallimg.offsetTop - show_half;<BR> var Left = e.clientX - smallimg.offsetLeft - show_half;<BR> //获取当前移动的showimg位置 计算方法是 鼠标坐标 - 最外面容器的坐标 - 盒子的宽(高)的/2 <BR> Top = Top<0?0:Top>maxHeight?maxHeight:Top;<BR> Left = Left<0?0:Left>maxWidth?maxWidth:Left;<BR> showimg.style.top = Top + "px";<BR> showimg.style.left = Left + "px";<BR> bigimg.style.background = "url("+small_url+") -"+Left*num+"px -"+Top*num+"px no-repeat";<BR>};<BR>smallimg.onmouseout = function(){<BR> showimg.style.display="none";<BR> bigimg.style.background ="";<BR> bigimg.style.display="none"<BR>};<BR>