跟随鼠标移动
<script>
function getPageScroll()
{
var pos = {x:0,y:0};
if(window.pageXOffset)//ns
pos.x = window.pageXOffset;
else if(document.documentElement && document.documentElement.scrollLeft)// Explorer 6 Strict
pos.x = document.documentElement.scrollLeft;
else if(document.body)// all other Explorers
pos.x = document.body.scrollLeft;
if(window.pageYOffset)//ns
pos.y = window.pageYOffset;
else if(document.documentElement && document.documentElement.scrollTop)// Explorer 6 Strict
pos.y = document.documentElement.scrollTop;
else if(document.body)// all other Explorers
pos.y = document.body.scrollTop;
return pos;
}
function divMove(id,state,evt)
{
var e=window.event||evt;
var divObj=document.getElementById(id);
var scroll=getPageScroll();
if(state)
{
divObj.style.display='';
divObj.style.left=e.clientX+10+'px';
divObj.style.top=e.clientY-160+scroll.y+'px';
}
else
{
divObj.style.display='none';
}
}
</script>
<br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br>
<img width="100" id="test" height="100" style="display:none; position:absolute;" />
<div onmousemove="divMove('test','1',event)" style="width:500px; height:500px; background:#ff0">
</div>