Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  javascript  >  正文 Javascript图片放大镜的实例分析

Javascript图片放大镜的实例分析

发布时间:2015-02-21   编辑:www.jquerycn.cn
本文分享一例javascript实现的图片放大镜代码,由javascript结合css代码来实现,效果不错,有需要的朋友可以参考学习下。

javascript图片放大镜的实现思路:
“放大镜”后有一幅背景图,它是“放大了”的图的原本。
通过移动“放大镜”时适当调整背景图的位置,使它显示的刚好是需要要放大的部分。

制作步骤:
1,准备两幅内容相同尺寸不同的图片,这里找了一个400×300像素的缩略图small_hill.gif,一个800×600像素的大图big_hill.gif。然后再准备一个“放大镜”的图片。
注意:它中间部分必须是透明的,这里准备了一个绿色的方框 viewer.gif。

2,编写如下的代码:
以下是两幅图的代码,都它们作为层。
第一幅是缩略图,第二幅是“放大镜”,首先将它的背景移到不可见的地方;
其中“ onclick="moveme=!moveme" ”表示每次点击它都改变“moveme”的布尔值。

代码:
 

复制代码 代码示例:
<img src="small_hill.gif" id="bgLayer" style="position:absolute; left:150px; top:50px;">
<img src="viewer.gif" id="myviewer"  onclick="moveme=!moveme" onmousemove="viewit(this)"
style="left:0;top:0;background-repeat:no-repeat; background-position:2000px 2000px;position:absolute;">

JavaScript脚本:
 

复制代码 代码示例:

<script language="JavaScript">
  <!--
  var viewer_bgcolor="#FFFFFF"; //放大镜的背景色,建议设成和网页背景色相同。
  var bigmap="big_hill.gif"; //大图路径

  document.all.myviewer.style.backgroundImage='url('+bigmap+')';
  document.all.myviewer.style.backgroundColor=viewer_bgcolor;
  //因为大图作为背景无法设定和读取它的尺寸,只好把它的一个副本作为实图,但不可见:
  document.write('<img id="getsize" style="position:absolute; left:-2000px; top:-2000px;" src="'+bigmap+'">');

  var moveme=false; //该布尔值决定“放大镜”是否随鼠标移动,初始值为否
  function viewit(obj){
    if (moveme){
      //以下两行控制“放大镜”的移动:
      obj.style.left=event.x+parseInt(document.body.scrollLeft)-parseInt(obj.width)/2;
      obj.style.top=event.y+parseInt(document.body.scrollTop)-parseInt(obj.height)/2;

     //以下几行调整当“放大镜”移动时其背景图的位置,使其中心移到缩略图的某点时,其背景图上相应的点也移动到其中心。
     //其中Nx,Ny指大图宽和高分别是小图的几倍,bgx,bgy是背景图当移到的X,Y坐标。
      Nx=parseInt(document.all.getsize.width)/parseInt(document.all.bgLayer.width);
      bgx=(-1)*(Nx-1)*(event.x-parseInt(document.all.bgLayer.style.left)+parseInt(document.body.scrollLeft))-parseInt(obj.style.left)+parseInt(document.all.bgLayer.style.left);

      Ny=parseInt(document.all.getsize.height)/parseInt(document.all.bgLayer.height);
      bgy=(-1)*(Ny-1)*(event.y-parseInt(document.all.bgLayer.style.top)+parseInt(document.body.scrollTop))-parseInt(obj.style.top)+parseInt(document.all.bgLayer.style.top);

      obj.style.backgroundPosition=bgx+" "+bgy;
    }
  }
  //-->
</script>

完整代码:
 

复制代码 代码示例:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>js图片放大镜-www.jquerycn.cn</title>
</head>
<body bgcolor="#FFFFFF">
<center>
--JS放大镜显示--
 
单击"放大镜"开始浏览,再次单击停止。</center>
<!--缩略图和放大镜图-->
<img src="http://www.www.jquerycn.cn/images/small_hill.gif" id="bgLayer" style="position:absolute; left:160px; top:50px;"> 
<img src="http://www.www.jquerycn.cn/images/viewer.gif" id="myviewer" onclick="moveme=!moveme" onmousemove="viewit(this)" style="background-repeat:no-repeat;background-position:2000px 2000px;position:absolute;">

<script language="JavaScript">
var viewer_bgcolor="#FFFFFF"; //放大镜的背景色,建议设成和网页背景色相同。
var bigmap="http://www.www.jquerycn.cn/images/big_hill.gif";    //大图路径

document.all.myviewer.style.backgroundImage='url('+bigmap+')';
document.all.myviewer.style.backgroundColor=viewer_bgcolor;
document.write('<img id="getsize" style="position:absolute; left:-2000px; top:-2000px;" src="'+bigmap+'">');

var moveme=false;
function viewit(obj){
if (moveme){
obj.style.left=event.x+parseInt(document.body.scrollLeft)-parseInt(obj.width)/2;
obj.style.top=event.y+parseInt(document.body.scrollTop)-parseInt(obj.height)/2;

Nx=parseInt(document.all.getsize.width)/parseInt(document.all.bgLayer.width);
bgx=(-1)*(Nx-1)*(event.x-parseInt(document.all.bgLayer.style.left)+parseInt(document.body.scrollLeft))-parseInt(obj.style.left)+parseInt(document.all.bgLayer.style.left);

Ny=parseInt(document.all.getsize.height)/parseInt(document.all.bgLayer.height);
bgy=(-1)*(Ny-1)*(event.y-parseInt(document.all.bgLayer.style.top)+parseInt(document.body.scrollTop))-parseInt(obj.style.top)+parseInt(document.all.bgLayer.style.top);

obj.style.backgroundPosition=bgx+" "+bgy;
}}
</script>
</body>
</html>

您可能感兴趣的文章:
Javascript图片放大镜的实例分析
jQuery图片放大镜插件 magnifier
JavaScript图片放大镜的简单代码
什么是canvas离屏技术?canvas放大镜效果如何实现?
canvas离屏技术与放大镜实现代码示例
js放大镜效果一例
jQuery 图片放大镜效果插件jQZoom的用法举例
jQuery放大镜插件 AnythingZoomer
photoshop树滤镜使用技巧心得分享
JS放大镜效果(简洁实用的放大镜效果)

关键词: 放大镜特效  图片放大镜   
[关闭]