利用JS实现图片放大效果

      最近有个朋友询问我有关图片放大的效果的设计思路,我刚开始也觉得这个方法已经很多网站都用到了,尤其是淘宝网,因为小弟的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>图片放大镜</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="img_small.jpg" class="zoom" big="img_big.jpg" /></div>
</body>
</html>
<script type="text/javascript">
    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>

阅读更多
个人分类: JavaScript
上一篇document.getElementsByClassName的理想实现
下一篇gb2312和UTF8的解码
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭