练习鼠标跟随!以及拖拽

<!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>
</head>
<script>

另外一种鼠标跟随移动(也可以制作成拖拽)
window.onload=function(){
    var xmouse=0;
    var ymouse=0;
    var odiv=document.getElementById("time");

    odiv.onmousedown=function(ev){
            var ev=ev||window.event;
        
    
        xmouse=ev.clientX-odiv.offsetLeft ;
        ymouse=ev.clientY-odiv.offsetTop ;
    
    document.onmousemove=function(ev){
        var ev=ev||window.event;    

        
        odiv.style.left=ev.clientX-xmouse+"px";
            
        odiv.style.top=ev.clientY-ymouse+"px";
        if(odiv.style.left<=0){
            odiv.style.left=0;
            }
                if(odiv.style.top<=0){
            odiv.style.top=0;
            }
       }
    document.onmouseup=function(ev){
        var ev=ev||window.event;    
        
        document.onmousemove=null;
        document.onmousedown=null;
     }
    }
    
    
}
    

 
    

</script>
<body>
<div style="position:relative; left:300px;top:300px; width: 100px;height: 100px;" id="time">老子要显示时间</div>

</body>
</html>

posted @ 2016-10-17 13:29  你走!你不走,那我走  阅读(156)  评论(0编辑  收藏  举报