Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [JS] zaznaczanie pozycji kursora nad warstwą, np. do określania pozycji na mapie
phpion
post
Post #1





Grupa: Moderatorzy
Postów: 6 072
Pomógł: 861
Dołączył: 10.12.2003
Skąd: Dąbrowa Górnicza




Witam,
napisałem odpowiednie funkcyjki JS - może komuś się przyda. Zamiast div'a można dać np. mapkę i wskazać gdzie znajduje się dany obiekt na mapie (współrzędne zrzucone do inputów).
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3.  
  4. <title>Wspolrzedne na warstwie</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <style type="text/css">
  7. #area {
  8. background-color: #CCC;
  9. height: 200px;
  10. width: 300px;
  11. }
  12. #point {
  13. background-color: #000;
  14. height: 3px;
  15. position: relative;
  16. width: 3px;
  17. }
  18. </style>
  19. <script type="text/javascript">
  20. function getXY(e) {
  21. var posx = 0;
  22. var posy = 0;
  23. if (!e) var e = window.event;
  24. if (e.pageX || e.pageY) {
  25. posx = e.pageX;
  26. posy = e.pageY;
  27. }
  28. else if (e.clientX || e.clientY) {
  29. posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
  30. posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
  31. }
  32.  
  33. posx = posx - parseInt(document.getElementById('area').offsetLeft);
  34. posy = posy - parseInt(document.getElementById('area').offsetTop);
  35.  
  36. document.getElementById('x').value = posx;
  37. document.getElementById('y').value = posy;
  38.  
  39. movePoint(posx, posy);
  40. }
  41.  
  42. function movePoint(posx, posy) {
  43. document.getElementById('point').style.left = posx + 'px';
  44. document.getElementById('point').style.top = posy + 'px';
  45. }
  46. </head>
  47.  
  48. <div id="area" onclick="getXY(event);">
  49. <div id="point"></div>
  50. </div>
  51.  
  52. <input id="x" />
  53. <input id="y" />
  54.  
  55. </body>
  56. </html>
Go to the top of the page
+Quote Post

Posty w temacie


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 Aktualny czas: 20.08.2025 - 04:53