Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [JS] Metoda obiektu do obsługi eventu
DarK MaN
post 28.06.2007, 15:16:31
Post #1





Grupa: Zarejestrowani
Postów: 23
Pomógł: 0
Dołączył: 4.10.2004

Ostrzeżenie: (0%)
-----


Mam prosty skrypt do obsługi drag & drop. Nie działa jeśli funkcja move odwołuje się do swojego obiektu poprzez "this". Jeśli robi to bezpośrednio za pomocą nazwy wszystko jest OK (wykomentowany kod). Co może być tego przyczyną?

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <STYLE type="text/css">
  3. .window_style
  4. {
  5. background-color: #66CCCC;
  6. border: 1px #000000 solid;
  7. position: absolute;
  8. width:100px;
  9. height:100px;
  10. }
  11. </STYLE>
  12.  
  13. <script LANGUAGE="JavaScript">
  14. <!--
  15.  
  16. function Draging()
  17. {
  18. //pozycja początkowa okna
  19. this.coordX = 0;
  20. this.coordY = 0;
  21.  
  22. //pozycja początkowa kursora
  23. this.offsetX = 0;
  24. this.offsetY = 0;
  25. //okno
  26. this.target = null;
  27.  
  28. //pobiera dane początkowe o oknie i kursorze
  29. this.drag = function drag(e, obj)
  30. {
  31. e = (e) ? e : window.event;
  32. this.target = obj;
  33. this.offsetX = e.clientX;
  34. this.offsetY = e.clientY;
  35. this.coordX = parseInt(this.target.style.left);
  36. this.coordY = parseInt(this.target.style.top);
  37. this.target.onmousemove = this.move;
  38. }
  39.  
  40. //przesuwa okno
  41. this.move = function move(e)
  42. {
  43.  
  44. e = (e) ? e : window.event;
  45.  
  46. this.target.style.left = this.coordX + e.clientX - this.offsetX + 'px';
  47. this.target.style.top = this.coordY + e.clientY - this.offsetY + 'px';
  48.  
  49. /*
  50. //odwołanie przez winDrag, this nie działa
  51. winDrag.target.style.left = winDrag.coordX + e.clientX - winDrag.offsetX + 'px';
  52. winDrag.target.style.top = winDrag.coordY + e.clientY - winDrag.offsetY + 'px';
  53. */
  54.  
  55. return false;
  56. }
  57.  
  58. //zaprzestaje przesuwania okna, czyści dane o obiekcie
  59. this.drop = function drop(e)
  60. {
  61. e = (e) ? e : window.event;
  62. this.target.onmousemove = null;
  63. this.target = null;
  64. this.offsetX = 0;
  65. this.offsetY = 0;
  66. this.coordX = 0;
  67. this.coordY = 0;
  68.  
  69. }
  70. }
  71.  
  72.  
  73. var winDrag = new Draging;
  74. //-->
  75. </SCRIPT>
  76. </HEAD>
  77.  
  78. <div id="win1" class="window_style" style="top:10px; left:50px" onMouseDown="winDrag.drag(event, this);" onMouseUp = "winDrag.drop()">Win1</div>
  79. </BODY>
  80.  
  81. </HTML>
Go to the top of the page
+Quote Post
Wiktor.
post 5.07.2007, 09:25:47
Post #2





Grupa: Zarejestrowani
Postów: 2
Pomógł: 0
Dołączył: 4.07.2007

Ostrzeżenie: (0%)
-----


Cześć,
przyjrzałem się temu co napisałeś i sądzę, że problem to przypisanie funkcji move - zauważ, że przypisujesz ją jako eventHandler dla obiektu this.TARGET a nie this.

pozdrawiam
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 24.07.2025 - 19:56