天天看点

CSS放大镜特效

一、CSS代码

  1. #divcss5 { width: 120px; height: 90px; padding: 5px;

     border: 1px solid #ccc; position: relative; } 

  2. #divcss5 .small_pic { width: 120px; height: 90px; background: #eee;

     position: relative; } 

  3. #divcss5 .float_layer { width: 50px; height: 50px; border: 1px solid #000;

     background: #fff; filter: alpha(opacity: 30); opacity: 0.3;

     position: absolute; top: 0; left: 0; display:none; } 

  4. #divcss5 .mark {width:100%; height:100%; position:absolute; z-index:2;

     left:0px; top:0px; background:red; filter:alpha(opacity:0); opacity:0;} 

  5. #divcss5 .big_pic { position: absolute; top: -1px; left: 215px; 

    width:250px; height:250px; overflow:hidden; border:2px solid #CCC; display:none; } 

  6. #divcss5 .big_pic img { position:absolute; top: -30px; left: -80px; } 

二、JS代码

  1. function getByClass(oParent, sClass) 
  2.  var aEle=oParent.getElementsByTagName('*'); 
  3.  var aTmp=[]; 
  4.  var i=0; 
  5.  for(i=0;i<aEle.length;i++) 
  6.  { 
  7.   if(aEle[i].className==sClass) 
  8.   { 
  9.    aTmp.push(aEle[i]); 
  10.   } 
  11.  } 
  12.  return aTmp; 
  13. window.onload=function () 
  14.  var oDiv=document.getElementById('divcss5'); 
  15.  var oMark=getByClass(oDiv, 'mark')[0]; 
  16.  var oFloat=getByClass(oDiv, 'float_layer')[0]; 
  17.  var oBig=getByClass(oDiv, 'big_pic')[0]; 
  18.  var oSmall=getByClass(oDiv, 'small_pic')[0]; 
  19.  var oImg=oBig.getElementsByTagName('img')[0]; 
  20.  oMark.onmouseover=function () 
  21.  { 
  22.   oFloat.style.display='block'; 
  23.   oBig.style.display='block'; 
  24.  }; 
  25.  oMark.onmouseout=function () 
  26.  { 
  27.   oFloat.style.display='none'; 
  28.   oBig.style.display='none'; 
  29.  }; 
  30.  oMark.onmousemove=function (ev) 
  31.  { 
  32.   var oEvent=ev||event; 
  33.   var l=oEvent.clientX-oDiv.offsetLeft-oSmall.offsetLeft-oFloat.offsetWidth/2; 
  34.   var t=oEvent.clientY-oDiv.offsetTop-oSmall.offsetTop-oFloat.offsetHeight/2; 
  35.   if(l<0) 
  36.   { 
  37.    l=0; 
  38.   } 
  39.   else if(l>oMark.offsetWidth-oFloat.offsetWidth) 
  40.   { 
  41.    l=oMark.offsetWidth-oFloat.offsetWidth; 
  42.   } 
  43.   if(t<0) 
  44.   { 
  45.    t=0; 
  46.   } 
  47.   else if(t>oMark.offsetHeight-oFloat.offsetHeight) 
  48.   { 
  49.    t=oMark.offsetHeight-oFloat.offsetHeight; 
  50.   } 
  51.   oFloat.style.left=l+'px'; 
  52.   oFloat.style.top=t+'px'; 
  53.   var percentX=l/(oMark.offsetWidth-oFloat.offsetWidth); 
  54.   var percentY=t/(oMark.offsetHeight-oFloat.offsetHeight); 
  55.   oImg.style.left=-percentX*(oImg.offsetWidth-oBig.offsetWidth)+'px'; 
  56.   oImg.style.top=-percentY*(oImg.offsetHeight-oBig.offsetHeight)+'px'; 
  57.  }; 
  58. }; 

三、HTML代码

  1. <div id="divcss5"> 
  2. <div class="small_pic"> 
  3. <span class="mark"></span> 
  4.     <span class="float_layer"></span> 
  5.     <img src="small.jpg" width=120px height=90px /></div> 
  6.     <div class="big_pic"><img src="big.jpg" /></div> 
  7. </div>