免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1530 | 回复: 0
打印 上一主题 下一主题

js-图片局部放大 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-20 09:48 |只看该作者 |倒序浏览
<SCRIPT language=javascript>
   window.onload = function(){
   var pbi = new qsoft.PopBigImage("orig",10,-2);  
   pbi.render();        
 }
if(typeof(qsoft) == "undefined") 
       qsoft = {}; 
 qsoft.PopBigImage = function (origImageId,dx,dy) 
   { 
       this.oim = document.getElementById(origImageId); 
       this.oim.style.cursor = "crosshair";   
       this.ow = this.oim.width; 
       this.oh = this.oim.height; 
       this.detaX = dx?dx : 30; 
       this.detaY = dy?dy : 0; 
       this.getAbsSize = function (obj) 
       { 
           return obj.getBoundingClientRect(); 
       }; 
       var rect = this.getAbsSize(this.oim); 
       this.ol = rect.left + this.detaX + this.ow; 
       this.ot = rect.top + this.detaY; 
       this.src = this.oim.src; 
       this.getImageSize = function (img) 
       { 
           var im = new Image(); 
           im.src = img.src;   
           var size = {}; 
           size.width = im.width; 
           size.height = im.height; 
           im = null; 
           delete im; 
           return size; 
       }; 
       var rsize = this.getImageSize(this.oim); 
       this.w = rsize.width; 
       this.h = rsize.height; 
       var qObj = this; 
       this.createMask = function () 
       { 
           if(typeof(this.mask) == "undefined") 
           { 
               this.mask = document.createElement("div"); 
               this.mask.id = this.oim.id + "_mask"; 
               this.mask.style.position  = "absolute"; 
               this.mask.style.width = this.ow; 
               this.mask.style.height = this.oh; 
               this.mask.style.left = this.ol; 
               this.mask.style.top = this.ot; 
               this.mask.style.backgroundImage  = "url("+this.src+")"; 
               this.mask.style.backgroundRepeat = "no-repeat";      
               this.mask.style.display ="none";     
               document.body.appendChild(this.mask);       
               //this.mask.style = "position: absolute; ";//left: "+this.ol+"; top: "+this.ot+";width: " + this.ow + "; height: " + this.oh + "; 
               ///*display: none;*/ background: url(" + this.src + ") left top no-repeat;"; 
           } 
       }; 
       this.regEvent = function () 
       { 
           this.oim.onmousemove = function () 
           { 
               var x = Math.ceil(event.offsetX * qObj.w/qObj.ow) - qObj.ow/2; 
               var y = Math.ceil(event.offsetY * qObj.h/qObj.oh) - qObj.oh/2; 
              if(x<0) x = 0; 
              if(y<0) y = 0;   
              var maxx = Math.ceil((qObj.w-qObj.ow)); 
              var maxy = Math.ceil((qObj.h-qObj.oh));   
              if(x>maxx) x = maxx; 
              if(y>maxy) y = maxy; 
              qObj.mask.style.backgroundPositionX = -x; 
              qObj.mask.style.backgroundPositionY = -y;  
           }; 
           this.oim.onmouseout = function (e) 
           { 
               qObj.mask.style.display = "none"; 
           }; 
           this.oim.onmouseenter = function (e) 
           { 
               qObj.mask.style.display = "block"; 
           }; 
       }; 
       this.render = function () 
       { 
           this.createMask(); 
           this.regEvent(); 
       };  
   }; 
   qsoft.version = 0.1;
window.onload = function (){ 
        var pbi = new qsoft.PopBigImage("orig",24,-2);   
        pbi.render();   
        var pbi1 = new qsoft.PopBigImage("win7",24,-2);   
        pbi1.render();      
    }
</SCRIPT>
<img width="150"  height="200" id="orig" src="http_imgloadCALSZ39K.jpg" alt="" />
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP