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

首頁 > 語言 > JavaScript > 正文

javascript實現淡藍色的鼠標拖動選擇框實例

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

這篇文章主要介紹了javascript實現淡藍色的鼠標拖動選擇框,可實現鼠標拖動出現淡藍色選擇框的效果,涉及javascript鼠標事件及樣式的操作技巧,需要的朋友可以參考下

本文實例講述了javascript實現淡藍色的鼠標拖動選擇框。分享給大家供大家參考。具體實現方法如下:

 

 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
  3. <html xmlns="http://www.w3.org/1999/xhtml"
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  5. <style type="text/css"
  6. #rectBox 
  7. background:#CCFFFF; 
  8. border:2px solid #0099FF; 
  9. filter:alpha(opacity=30); 
  10. opacity:0.3; 
  11. position:absolute; 
  12. </style> 
  13. <head> 
  14. <title>一個鼠標選擇框</title> 
  15. <script type="text/javascript"
  16. function Rect() 
  17. this.doc = document.documentElement; 
  18. if(!this.doc) return
  19. this.startX = ''
  20. this.startY = ''
  21. this.rect = null
  22. rectSelf = this
  23. Rect.prototype.down = function(e) 
  24. var e = e?e:window.event; 
  25. rectSelf.startX = e.clientX?e.clientX + document.body.scrollLeft:e.pageX; 
  26. rectSelf.startY = e.clientY?e.clientY + document.body.scrollTop:e.pageY; 
  27. rectSelf.showRect(true); 
  28. Rect.prototype.up = function(e) 
  29. rectSelf.rectBox.style.width = '0px'
  30. rectSelf.rectBox.style.height = '0px'
  31. rectSelf.showRect(false); 
  32. Rect.prototype.move = function(e) 
  33. if(rectSelf.rectBox) 
  34. var currentX = e.clientX?e.clientX + rectSelf.doc.scrollLeft:e.pageX; 
  35. var currentY = e.clientY?e.clientY + rectSelf.doc.scrollTop:e.pageY; 
  36. rectSelf.rectBox.style.width = Math.abs(currentX - rectSelf.startX) + 'px'
  37. rectSelf.rectBox.style.height = Math.abs(currentY - rectSelf.startY) + 'px'
  38. if(currentX - rectSelf.startX < 0) 
  39. rectSelf.rectBox.style.left = currentX + 'px'
  40. if(currentY - rectSelf.startY < 0) 
  41. rectSelf.rectBox.style.top = currentY + 'px'
  42. //document.title = "left:"+currentX + 'px '+"top:"+currentY + 'px '; 
  43. Rect.prototype.showRect = function(bool) 
  44. if(bool) 
  45. if(!this.rectBox) 
  46. this.rectBox = document.createElement("div"); 
  47. this.rectBox.id = "rectBox"
  48. document.body.appendChild(this.rectBox); 
  49. this.rectBox.style.display = "block"
  50. this.rectBox.style.left = this.startX + 'px'
  51. this.rectBox.style.top = this.startY + 'px';  
  52. this.addEventListener(this.doc , 'mousemove' , this.move); 
  53. else 
  54. if(this.rectBox) 
  55. this.rectBox.style.display = "none"
  56. this.removeEventListener(this.doc , 'mousemove' , this.move); 
  57. Rect.prototype.addEventListener = function(o,e,l)  
  58. if (o.addEventListener) { 
  59. o.addEventListener(e,l,false); 
  60. else if (o.attachEvent) { 
  61. o.attachEvent('on'+e,function() { 
  62. l(window.event); 
  63. }); 
  64. Rect.prototype.removeEventListener = function(o,e,l)  
  65. if (o.removeEventListener) { 
  66. o.removeEventListener(e,l,false); 
  67. else if (o.detachEvent) { 
  68. o.detachEvent('on'+e,function() { 
  69. l(window.event); 
  70. }); 
  71. window.onload = function() 
  72. var oRect = new Rect(); 
  73. oRect.addEventListener(oRect.doc , 'mousedown' , oRect.down); 
  74. oRect.addEventListener(oRect.doc , 'mouseup' , oRect.up); 
  75. </script> 
  76. </head> 
  77. <body> 
  78. <h1>拖動你的鼠標就會出現選擇框</h1> 
  79. </body> 
  80. </html> 

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

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

圖片精選

主站蜘蛛池模板: 怀集县| 泾川县| 南江县| 咸宁市| 葵青区| 巴林左旗| 板桥市| 双鸭山市| 长宁县| 蕉岭县| 普宁市| 承德市| 吉林市| 凌海市| 华宁县| 藁城市| 会泽县| 敦化市| 韩城市| 舟山市| 仁化县| 石台县| 衡山县| 宁化县| 晋城| 屯留县| 易门县| 成都市| 滨州市| 霍州市| 云南省| 河东区| 子长县| 曲松县| 谢通门县| 广水市| 保亭| 林西县| 河间市| 平山县| 错那县|