JavaScript图片放大镜效果代码一例

发布时间:2020-04-16编辑:脚本学堂
本文分享一例javascript实现的图片放大镜代码,效果不错,值得借鉴,有需要的朋友可以看看。

javascript图片放大镜效果,图示如下:
js图片放大镜
代码:
 

复制代码 代码示例:
<!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=iso-8859-1" />
<title>图片放大镜效果-www.jb200.com</title>
  <style>
 #zoomdiv {
  position: absolute;
  background: #ffffff;
  border:1px solid #CCCCCC;
  display:none;
  text-align: center;
  overflow: hidden;
    }
 #zoomup{
  position:absolute;
  background:#c0c0c0;
  display:none;
  filter:alpha(opacity=50);
  opacity:.5;
  cursor:move;
 }
 .zoom{ 
    position:relative;
    }
  </style>
</head>
<body>
<div style="position:absolute;border:1px solid #c0c0c0;left:400px;"><img  src="/article/UploadPic/200910/19/shoe2_small-14.jpg" class="zoom" big="/article/UploadPic/200910/19/shoe2_big-80.jpg" /></div>
</body>
</html>
<script>
    var zoom = function(o){
  var d = document,db=document.body,timgs=d.getElementsByTagName('img'),ie = !+'v1',isStrict = d.compatMode == "CSS1Compat",
   opt = {
    width:200,
    height:200,
    offset:20,
    float:'rigth',
    minWidth:100
   },
   div = d.createElement('div'),divup = d.createElement('div'),
   getXY = function(el){
    var r={t:0,l:0},
     ua = navigator.userAgent.toLowerCase(),
     add = function(t,l){r.l+=l,r.t+=t},
     p = el;
    if(el&&el!=db){
     if(el.getBoundingClientRect){
      var b = el.getBoundingClientRect();
      add(b.top + Math.max(d.body.scrollTop,d.documentElement.scrollTop),
 b.left+Math.max(d.body.scrollLeft,d.documentElement.scrollLeft));
      isStrict?add(-d.documentElement.clientTop,-d.documentElement.clientLeft):add(-1,-1)
     }else{
      var dv = d.defaultView;
      while(p){
 add(p.offsetTop,p.offsetLeft);
 p = p.offsetParent;
      }
      p = el.parentNode;
      while (p && p != db) {
 add(-p.scrollTop,-p.scrollLeft);
 p = p.parentNode;
      }
     }
    }
    return r;
   },
   extend = function(t,s){
    for(var p in s){
     t[p] = s[p];
    };
   };
   div.id='zoomdiv',divup.id = 'zoomup';
   div.innerHTML = '<img id="bigimg" src="" />';
   db.appendChild(div);
   extend(opt,o);
   function leave(){
    div.style.display = divup.style.display = 'none';
    db.onmousemove = null;
   };
   for(var i=0,ci;ci=timgs[i++];){
    if(ci.className=='zoom'){
     ci.onmouseover = function(e){
      this.parentNode.appendChild(divup);
      var bimg=d.getElementById('bigimg'),bwidth,bheight,sx,sy,
 width = this.offsetWidth,height = this.height,top=getXY(this).t,left = getXY(this).l,tWidth,tLeft,sWidth;
      bimg.onload = function(){
    div.style.display = 'block';
    bwidth = bimg.width,bheight = bimg.height,
 sx = bwidth/width,sy = bheight/height;
    tLeft =  opt.float=='rigth'?opt.offset+width+left:left-opt.offset-width*opt.width*sx,
    sWidth = window.innerWidth||(isStrict?d.documentElement.clientWidth:db.clientWidth);
    if(tLeft+opt.width+5>sWidth){
        tWidth = sWidth - 5 - tLeft;
        tWidth<opt.minWidth&&(tLeft = left-opt.offset-opt.width,tWidth=0);
    };
    extend(div.style,{
  left:tLeft +'px',
  top:top+'px',
  width:(tWidth||opt.width)+'px',
  height:opt.height+'px'
 });
 extend(divup.style,{
  width:(tWidth||opt.width)/sx+'px',
  height:opt.height/sy+'px',
        display:'block'
 })
 db.onmousemove = function(e){
  var e = e || event,x=e.pageX||(e.clientX+(d.documentElement.scrollLeft||db.scrollLeft)),y=e.pageY||(e.clientY+(d.documentElement.scrollTop||db.scrollTop)),
   scrolly = y - divup.offsetHeight/2 - top,
   scrollx = x - divup.offsetWidth/2 - left;
  scrolly = y - divup.offsetHeight/2 < top ? 0 : y + divup.offsetHeight/2>height+top ? height - divup.offsetHeight  : scrolly;
  scrollx = x - divup.offsetWidth/2 < left ? 0 : x + divup.offsetWidth/2>width+left ? width - divup.offsetWidth  : scrollx;
  div.scrollTop = scrolly*sy;
  div.scrollLeft =  scrollx*sx;
  extend(divup.style,{top:scrolly+'px',left:scrollx+'px'});
 }
      }
      bimg.src = this.getAttribute('big');
     };
     ci.parentNode[ie?'onmouseleave':'onmouseout'] = ie?leave:function(e){
      !(this===e.relatedTarget||(this.contains?this.contains(e.relatedTarget):this.compareDocumentPosition(e.relatedTarget)==20))&&leave();
     }
    }
   };   
    }
 zoom({width:300,height:300});
</script>