建站学 - 轻松建站从此开始!

建站学-个人建站指南,网页制作,网站设计,网站制作教程

原生JavaScript图片放大镜效果

时间:2012-05-14 20:55来源: 作者: 点击:
好记性,不如烂笔头;今天又写了一个简单的JS效果,放上来跟大家分享下,顺便也记录下自己的代码。废话不多说,想看的朋友请点下面的链接查看。   效果预览: http://www.seejs.com/enlarge/ <!DOCTYPE HTML> <html> <head> <meta charset="utf
好记性,不如烂笔头;今天又写了一个简单的JS效果,放上来跟大家分享下,顺便也记录下自己的代码。废话不多说,想看的朋友请点下面的链接查看。

  效果预览: http://www.seejs.com/enlarge/

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>JS另类放大镜</title>
<style>
ul,li{margin:0;padding:0;}
ul{list-style:none;}
.w960{
 margin:0 auto;
 width:960px;
 border:1px solid #ccc;
 border-right:none;
}
.pic-list{
 width:960px;
 overflow:hidden;
}
.pic-list .item{
 float:left;
 position:relative;
 margin-left:-1px;
 margin-bottom:-1px;
 width:240px;
 height:320px;
 border-right:1px solid #ccc;
 border-bottom:1px solid #ccc;
 overflow:hidden;
}
.pic-list .item img{
 position:absolute;
 cursor:pointer;
}
</style>
</head>
<body>
<div class="w960">
 <ul class="pic-list" id="pic-list">
  <li class="item">
         <img src="imgs/img_01.jpg" width="240" height="320">
        </li>
        <li class="item">
         <img src="imgs/img_02.jpg" width="240" height="320">
        </li>
        <li class="item">
         <img src="imgs/img_03.jpg" width="240" height="320">
        </li>
        <li class="item">
         <img src="imgs/img_04.jpg" width="240" height="320">
        </li>
        <li class="item">
         <img src="imgs/img_05.jpg" width="240" height="320">
        </li>
        <li class="item">
         <img src="imgs/img_06.jpg" width="240" height="320">
        </li>
        <li class="item">
         <img src="imgs/img_07.jpg" width="240" height="320">
        </li>
        <li class="item">
         <img src="imgs/img_08.jpg" width="240" height="320">
        </li>
 </ul>
</div>
<script>
(function(){
 var d = document.getElementById("pic-list"),
     c = d.getElementsByTagName("li"),
  l = c.length,
  scale = 2;
 function stopEvent(e){
  if(e && e.stopPropagation){
   e.stopPropagation();    
  }else{
   e.cancelBubble = true;
  }
 }
 for(var i = 0; i < l; i++){
  c[i].left = c[i].getBoundingClientRect().left;
  c[i].top = c[i].getBoundingClientRect().top;
  (function(i){
   c[i].onmouseover = function(e){
    var e = e || window.event;
    var img = this.getElementsByTagName("img")[0];
    img.style.width = img.offsetWidth * scale + "px";
    img.style.height = img.offsetHeight * scale + "px";
    img.style.left = (this.offsetWidth - img.offsetWidth) / 2 + "px";
    img.style.top = (this.offsetHeight - img.offsetHeight) / 2 + "px";
    this.dsx = Math.abs(parseInt(img.style.left));
    this.dsy = Math.abs(parseInt(img.style.top));
    this.cdx = this.offsetWidth / 2;
    this.cdy = this.offsetHeight / 2;
    this.left = (this.offsetWidth - img.offsetWidth) / 2
    this.top = (this.offsetHeight - img.offsetHeight) / 2
    stopEvent(e);
   };
   c[i].onmouseout = function(e){
    var e = e || window.event;
    var img = this.getElementsByTagName("img")[0];
    img.style.width = img.offsetWidth / scale + "px";
    img.style.height = img.offsetHeight / scale + "px";
    img.style.left = Math.floor((this.offsetWidth - img.offsetWidth) / 2) + "px";
    img.style.top = Math.floor((this.offsetHeight - img.offsetHeight) / 2) + "px";
    stopEvent(e);
   };
   c[i].onmousemove = function(e){
    var e = e || window.event;
    var img = this.getElementsByTagName("img")[0];
    var center = {"x":this.getBoundingClientRect().left + this.offsetWidth / 2,"y":this.getBoundingClientRect().top + this.offsetHeight / 2};
    var mPos = {"x":e.clientX,"y":e.clientY};
    var deltax = center.x - mPos.x,deltay = center.y - mPos.y;
    var dleft = deltax / this.cdx * this.dsx;
    var dtop = deltay / this.cdy * this.dsy;
    img.style.left = Math.floor(this.left + dleft) + "px";
    img.style.top = Math.floor(this.top + dtop) + "px";
    stopEvent(e);
   }
  })(i);
 }
})();
</script>
</body>
</html>

(责任编辑:admin)
织梦二维码生成器
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片