欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

如何实现淘宝放大镜实例

程序员文章站 2022-05-29 20:38:01
...
<!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=utf-8" />
<title>放大镜</title>
 <link rel="stylesheet" type="text/css" href="style/css.css">
</head>
<script type="text/javascript">
	
</script>
<body>

<p id="p1">

    <p class="small_pic">
		<span class="mark"></span>
        <span class="float_layer"></span>
        <img src="images/b.jpg" alt="" longdesc="" />
    </p>

    <p class="big_pic">
		<img src="images/a.jpg" alt=""longdesc="" />
	</p>

</p>

</body>
<script src="./js/js.js"></script>	
</html>
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;

}
var op = document.getElementById('p1');
var oMark = getByClass(op,'mark')[0];
var oFloat=getByClass(op,'float_layer')[0];
var oBg=getByClass(op,'big_pic')[0];
var oSm=getByClass(op,'small_pic')[0];
var oImg=oBg.getElementsByTagName('img')[0];
oMark.onmouseover=function(){
	oFloat.style.display='block';
    oBg.style.display='block';
}
oMark.onmouseout=function(){
	oFloat.style.display='none';
	oBg.style.display='none';
}	
oMark.onmousemove=function(ev){
    var oEvent=ev||event;
    var disX=oEvent.clientX-op.offsetLeft-oSm.offsetLeft-oFloat.offsetWidth/2;
    var disY=oEvent.clientY-op.offsetTop-oSm.offsetTop-oFloat.offsetHeight/2;
    var l=disX, t=disY;
    l<0?l=0:l>(oMark.offsetWidth-oFloat.offsetWidth)?l=oMark.offsetWidth-oFloat.offsetWidth:l=disX;
    t<0?t=0:t>(oMark.offsetWidth-oFloat.offsetHeight)?t=oMark.offsetWidth-oFloat.offsetHeight:t=disY;
    // 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.offsetWidth-oFloat.offsetHeight){
    // 	t=oMark.offsetWidth-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=-(oImg.offsetWidth-oBg.offsetWidth)*percentX +'px';
  	oImg.style.top=-(oImg.offsetHeight-oBg.offsetHeight)*percentY +'px';
}

以上就是如何实现淘宝放大镜实例的详细内容,更多请关注其它相关文章!