国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

js實現簡單div拖拽功能實例

2024-05-06 16:20:00
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了js實現簡單div拖拽功能的方法,實例分析了javascript針對div層拖拽的實現技巧,需要的朋友可以參考下

本文實例講述了js實現簡單div拖拽功能的方法。分享給大家供大家參考。具體實現方法如下:

 

 
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
  2. <html xmlns="http://www.w3.org/1999/xhtml"
  3. <head> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  5. <title>拖拽div</title> 
  6. <style type="text/css"
  7. div{ 
  8. position:absolute; 
  9. width:150px; 
  10. height:150px; 
  11. background-color:#C8FFFF; 
  12. </style> 
  13. <script type="text/javascript"
  14. <!-- 
  15. function drag(obj) 
  16. if (typeof obj == "string") { 
  17. var obj = document.getElementById(obj); 
  18. obj.orig_index=obj.style.zIndex; 
  19. //設置當前對象永遠顯示在最上層 
  20. obj.onmousedown=function (a){ 
  21. //鼠標按下 
  22. this.style.cursor="move"
  23. //設置鼠標樣式 
  24. this.style.zIndex=1000; 
  25. var d=document; 
  26. if(!a) a=window.event; 
  27. //按下時創建一個事件 
  28. var x=a.clientX-document.body.scrollLeft-obj.offsetLeft; 
  29. //x=鼠標相對于網頁的x坐標-網頁被卷去的寬-待移動對象的左外邊距 
  30. var y=a.clientY-document.body.scrollTop-obj.offsetTop; 
  31. //y=鼠標相對于網頁的y左邊-網頁被卷去的高-待移動對象的左上邊距 
  32. d.onmousemove=function(a){//鼠標移動 
  33. if(!a) a=window.event;//移動時創建一個事件 
  34. obj.style.left=a.clientX+document.body.scrollLeft-x; 
  35. obj.style.top=a.clientY+document.body.scrollTop-y; 
  36. d.onmouseup=function (){//鼠標放開 
  37. document.onmousemove=null
  38. document.onmouseup = null
  39. obj.style.cursor="normal";//設置放開的樣式 
  40. obj.style.zIndex=obj.orig_index; 
  41. }  
  42. --> 
  43. </script> 
  44. </head> 
  45. <body> 
  46. <div id="div1"></div> 
  47. <div id="div2" style="left:170px; background-color:#408080"></div> 
  48. <script type="text/javascript"
  49. <!-- 
  50. drag("div1"); 
  51. drag("div2"); 
  52. --> 
  53. </script> 
  54. </body> 
  55. </html> 

希望本文所述對大家的javascript程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 高唐县| 上高县| 仪征市| 伊春市| 巫山县| 菏泽市| 陆良县| 龙川县| 宜州市| 奇台县| 清水县| 高雄市| 如东县| 永泰县| 台中县| 平江县| 右玉县| 时尚| 卫辉市| 武强县| 连江县| 铅山县| 乐山市| 临夏县| 宁夏| 轮台县| 巨野县| 崇左市| 安陆市| 汝州市| 丰城市| 盐城市| 深水埗区| 景东| 宁津县| 连南| 临安市| 安岳县| 吐鲁番市| 肇源县| 临安市|