CSS放大鏡特效
阿新 • • 發佈:2019-02-07
一、CSS程式碼
-
#divcss5 { width: 120px; height: 90px; padding: 5px;
border: 1px solid #ccc; position: relative; } -
#divcss5 .small_pic { width: 120px; height: 90px; background: #eee;
position: relative; } -
#divcss5 .float_layer { width: 50px; height: 50px; border: 1px solid #000;
background: #fff; filter: alpha(opacity: 30); opacity: 0.3;
position: absolute; -
#divcss5 .mark {width:100%; height:100%; position:absolute; z-index:2;
left:0px; top:0px; background:red; filter:alpha(opacity:0); opacity:0;} -
#divcss5 .big_pic { position: absolute; top: -1px; left: 215px;
width:250px; height:250px; overflow:hidden; border:2px solid #CCC; display:none; } - #divcss5 .big_pic img { position:absolute; top: -30px; left: -80px; }
- function getByClass(oParent, sClass)
- {
- var aEle=oParent.getElementsByTagName('*');
- var aTmp=[];
- var i=0;
- for(i=0;i<aEle.length;i++)
- {
- if(aEle[i].className==sClass)
- {
- aTmp.push(aEle[i]);
- }
- }
-
return aTmp;
- }
- window.onload=function ()
- {
- var oDiv=document.getElementById('divcss5');
- var oMark=getByClass(oDiv, 'mark')[0];
- var oFloat=getByClass(oDiv, 'float_layer')[0];
- var oBig=getByClass(oDiv, 'big_pic')[0];
- var oSmall=getByClass(oDiv, 'small_pic')[0];
- var oImg=oBig.getElementsByTagName('img')[0];
- oMark.onmouseover=function ()
- {
- oFloat.style.display='block';
- oBig.style.display='block';
- };
- oMark.onmouseout=function ()
- {
- oFloat.style.display='none';
- oBig.style.display='none';
- };
- oMark.onmousemove=function (ev)
- {
- var oEvent=ev||event;
- var l=oEvent.clientX-oDiv.offsetLeft-oSmall.offsetLeft-oFloat.offsetWidth/2;
- var t=oEvent.clientY-oDiv.offsetTop-oSmall.offsetTop-oFloat.offsetHeight/2;
- if(l<0)
- {
- l=0;
- }
- else if(l>oMark.offsetWidth-oFloat.offsetWidth)
- {
- l=oMark.offsetWidth-oFloat.offsetWidth;
- }
- if(t<0)
- {
- t=0;
- }
- else if(t>oMark.offsetHeight-oFloat.offsetHeight)
- {
- t=oMark.offsetHeight-oFloat.offsetHeight;
- }
- oFloat.style.left=l+'px';
- oFloat.style.top=t+'px';
- var percentX=l/(oMark.offsetWidth-oFloat.offsetWidth);
- var percentY=t/(oMark.offsetHeight-oFloat.offsetHeight);
- oImg.style.left=-percentX*(oImg.offsetWidth-oBig.offsetWidth)+'px';
- oImg.style.top=-percentY*(oImg.offsetHeight-oBig.offsetHeight)+'px';
- };
- };
三、HTML程式碼
- <divid="divcss5">
- <divclass="small_pic">
- <spanclass="mark"></span>
- <spanclass="float_layer"></span>
- <imgsrc="small.jpg"width=120pxheight=90px/></div>
- <divclass="big_pic"><imgsrc="big.jpg"/></div>
- </div>