Chinaunix首页 | 论坛 | 博客
  • 博客访问: 239615
  • 博文数量: 108
  • 博客积分: 3045
  • 博客等级: 中校
  • 技术积分: 1162
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-19 18:26
文章分类

全部博文(108)

分类: JavaScript

2013-11-27 16:44:02


点击(此处)折叠或打开

  1. <!--拖拽框-->
  2. <style>
  3.     .one1{
  4.         position:absolute;
  5.         background:green;
  6.     }
  7. </style>
  8. <body>
  9. <!--
  10.     <div id="one" style="position:absolute;left:100px;top:100px;width:200px;height:30px;background:red;"></div>
  11.     -->
  12.     <input type="button" onclick="crea()" value="add">
  13.     <script>
  14.         var one=null;
  15.         function crea(){
  16.             if(one!=null)
  17.             return;
  18.             one=document.createElement("div");
  19.             //要写在外面,否则获取不到style
  20.             one.style.top="100";
  21.             one.style.left="100";
  22.             one.style.width="200px";
  23.             one.style.height="30px";

  24.             one.className="one1";

  25.             document.body.appendChild(one);

  26.             one.onmousedown=function(e){
  27.             var ev=e || window.event;
  28.             dx=ev.clientX;
  29.             dy=ev.clientY;
  30.             sx=parseInt(one.style.left);
  31.             sy=parseInt(one.style.top);
  32.             if(!down)
  33.             down=true;
  34.         }
  35.         document.onmouseup=function(){
  36.             down=false;
  37.         }

  38.         }

  39. //    var one=document.getElementById("one");
  40.     var down=false;
  41.     var dx=0;
  42.     var dy=0;
  43.     var sx=0;
  44.     var sy=0;
  45.     document.onmousemove=function(e){
  46.         var ev=e || window.event;
  47.         if(down){

  48.         one.style.top=ev.clientY-(dy-sy);
  49.         one.style.left=ev.clientX-(dx-sx);
  50.     
  51.         }
  52.     }
  53. </script>
  54. </body>


点击(此处)折叠或打开

  1. <!--跟随mouse移动的图片-->

  2. <body>
  3.     <img id="tu" style="position:absolute;left:-400px;top:-200px;" src="logo.png" >
  4. <script>
  5.     var tu=document.getElementById("tu");
  6.     document.onmousemove=function(e){
  7.         var ev=e || window.event;

  8.         var x=ev.clientX;
  9.         var y=ev.clientY;

  10.         tu.style.top=y+20;
  11.         tu.style.left=x+20;

  12.     }
  13. </script>
  14. </body>


点击(此处)折叠或打开

  1. <!--兼容性问题 mouse移动 显示坐标-->

  2. <body>
  3.     <div id="one">
  4.     </div>
  5. <script>
  6.     var one=document.getElementById("one");
  7.     document.onmousemove=function(e){
  8.         var ev=e || window.event;

  9.         var cx=ev.clientX;
  10.         var cy=ev.clientY;
  11.         var sx=ev.screenX;
  12.         var sy=ev.screenY;

  13.         var str="clientX="+cx+",clientY="+cy+",screenX="+sx+",screenY="+sy;

  14.         one.innerText=str;
  15.         window.status=str;
  16.         window.document.title=str;
  17.     }
  18. </script>
  19. </body>


点击(此处)折叠或打开

  1. <!--mouse移动 显示坐标-->

  2. <body onmousemove="test()">
  3.     <div id="one">
  4.     </div>
  5. <script>
  6.     var one=document.getElementById("one");
  7.     function test(){
  8.         var cx=event.clientX;
  9.         var cy=event.clientY;
  10.         var sx=event.screenX;
  11.         var sy=event.screenY;

  12.         var str="clientX="+cx+",clientY="+cy+",screenX="+sx+",screenY="+sy;

  13.         one.innerText=str;
  14.         window.status=str;
  15.         window.document.title=str;
  16.     }
  17. </script>
  18. </body>

阅读(424) | 评论(0) | 转发(0) |
0

上一篇:js_事件对象_1

下一篇:js_window对象

给主人留下些什么吧!~~