//这个综合示例解决问题比较多
1:图片预加载
var img = new Image();
2:img.complete 返回浏览器是否已完成对图像的加载。
3:鼠标绝对位置
event.clienX event.clientY
4:元素的位置
元素.offsetLeft offsetTop offsetWidth
文档的宽度documentElement.offsetWidth
<!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=gbk"> <title>跟随鼠标移动(大图展示)</title> <style type="text/css"> html,body,ul,li{margin:0;padding:0;} html,body{overflow:hidden;} #box ul{width:768px;height:172px;list-style-type:none;margin:10px auto;} #box li{float:left;width:170px;height:170px;cursor:pointer;display:inline;border:1px solid #ddd;margin:0 10px;} #box li.active{border:1px solid #a10000;} #box li img{width:170px;height:170px;vertical-align:top;} /*显示大图*/ #big{position:absolute;width:400px;height:400px;border:2px solid #ddd;display:none;} /*没有加载成功之前显示loading.gif 图片*/ #big div{position:absolute;top:0;left:0;width:400px;height:400px;opacity:0.5; filter:alpha(opacity=50);background:#fff url(img/loading.gif) 50% 50% no-repeat;} </style> </head> <body> <div id="box"> <ul> <li class=""><img src="06/shirt_1.jpg" /></li> <li class=""><img src="06/shirt_2.jpg" /></li> <li class=""><img src="06/shirt_3.jpg" /></li> <li><img src="06/shirt_4.jpg" /></li> </ul> </div> <div style="display:none;top:201px;left:384px;" id="big"> <div style="display:none;"> </div> </div> <h1 id="msg"></h1> <script type="text/javascript"> //这个综合示例解决问题比较多 //1:图片预加载 // var img = new Image(); //2:img.complete 返回浏览器是否已完成对图像的加载。 //3:鼠标绝对位置 //event.clienX event.clientY //4:元素的位置 // 元素.offsetLeft offsetTop offsetWidth // 文档的宽度documentElement.offsetWidth window.onload = function(){ var aLi = document.getElementsByTagName("li"); var oBig = document.getElementById("big"); var oLoading = oBig.getElementsByTagName("div")[0]; var i=0; var size = aLi.length; for(i=0;i<size;i++){ aLi[i].index = i; //鼠标划过,预加载图片插入容器并显示 aLi[i].onmouseover = function(){ //负责显示图片对象.. var oImg = document.createElement("img"); //图片预加载 var img = new Image(); // img.src = oImg.src = aLi[this.index].getElementsByTagName("img")[0].src.replace(".jpg","_big.jpg"); //插入大图片 oBig.appendChild(oImg); //鼠标移过样式 oBig.style.display = oLoading.style.display = "block"; //判断大图是否加载成功 //图片预加载成功 img.complete ? oLoading.style.display="none":(oImg.onload = function(){oLoading.style.display="none";}); }; //鼠标移动,大图容器跟随鼠标移动 aLi[i].onmousemove = function(event){ var event = event || window.event; //文档的宽度.. document.documentElement.offsetWidth 元素的宽度 //鼠标绝对位置 ..event.clientX //document.getElementById("msg").innerHTML = document.documentElement.offsetWidth +":"+event.clientX+":"+oBig.offsetWidth; var iWidth = document.documentElement.offsetWidth - event.clientX; //设置big的top值 oBig.style.top = event.clientY + 20 + "px"; //设置big的left值,如果右侧显示区域不够,大图将在鼠标左侧显示 oBig.style.left = (iWidth < oBig.offsetWidth+10 ? event.clientX-oBig.offsetWidth-10:event.clientX+10)+"px"; }; //鼠标离开,删除大图并隐藏大图容器 aLi[i].onmouseout = function(){ this.className = ""; oBig.style.display = "none"; //移除大图片 oBig.removeChild(oBig.lastChild); } } } </script> </body> </html>