Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [js] wleczenie się za / gonieie kursora ze spowolnieniem
23kulpamens
post 20.11.2007, 15:25:07
Post #1





Grupa: Zarejestrowani
Postów: 57
Pomógł: 1
Dołączył: 11.10.2007

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


Chcę uzyskać efekt spowolnienia przemiszczania się bloku za kursorem. Brakuje w JavaScript funkcji delay, więc użyłem setInterval i setTimeout, ale nie dają podążanego efektu. Kiedy zmienna DELAY osiągnie "0", blok nie pdąża płynnie za kursorem i nawet na początki nie widać przyspieszenia tylko blok podąża cały czas z tą samą prędkością. Próbowałem kilku sposobów, zaimplementowałem nawet własną funkcję pause(), ale nic nie działało. To jest najbliższe pożądanemu:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  3. <script type="text/javascript">
  4. var IE = document.all?true:false
  5. var tempX = 0
  6. var tempY = 0
  7. var XX = 0
  8. var YY = 0
  9. var YYY = 0
  10. var DELAY = 100;
  11.  
  12. function update(){
  13. if ( tempX > XX ){XX++;}else if( tempX < XX ){XX--;}
  14. if ( tempY > YY ){YY++;}else if( tempY < YY ){YY--;}
  15. document.getElementById('xxx').value = "X:" + XX;
  16. document.getElementById('yyy').value = "Y:" + YY;
  17. document.getElementById('blok').style.width = XX + "px";
  18. document.getElementById('blok').style.height = YY + "px";
  19. document.getElementById('blok').style.left = XX + "px";
  20. document.getElementById('blok').style.top = YY + "px";
  21. window.YYY = document.getElementById('xxx').value.toString();
  22. document.getElementById('info1').value = "D" + DELAY;
  23. document.getElementById('info2').value = tempX;
  24.  
  25. if(DELAY > 0){ DELAY--;}
  26. }
  27.  
  28. function checker(){
  29. if( tempX != XX || tempY != YY){
  30. setTimeout("update()", DELAY);
  31. }
  32. else{
  33. DELAY = 100;
  34. }
  35. }
  36.  
  37. function getMouseXY(e) {
  38. if (window.IE) { // grab the x-y pos.s if browser is IE
  39. tempX = event.clientX + document.body.scrollLeft
  40. tempY = event.clientY + document.body.scrollTop
  41. } else { // grab the x-y pos.s if browser is NS
  42. tempX = e.pageX
  43. tempY = e.pageY
  44. }
  45. // catch possible negative values in NS4
  46. if (tempX < 0){tempX = 0}
  47. if (tempY < 0){tempY = 0}
  48. return true
  49. }
  50.  
  51. function count(){
  52. setInterval("checker()", 1);
  53. if (!IE) document.captureEvents(Event.MOUSEMOVE)
  54. document.onmousemove = getMouseXY;
  55. }
  56.  
  57.  
  58. </head>
  59. <div id="blok" style="background-color: aqua; width: 500px; position: absolute; t">
  60. <input id="xxx" name="strength" value="" />
  61. <input id="yyy" name="strength" value="" />
  62. </div>
  63. <input id="info1" name="strength" value="" />
  64. <input id="info2" name="strength" value="" />
  65. <script type="text/javascript">count();</script>
  66. </body>
  67. </html>


Czy ktoś miałby pomysł jak to zrobić questionmark.gif

Ten post edytował 23kulpamens 20.11.2007, 15:28:00


--------------------
Ta sygnaturka to lekkie przegięcie. To poważne forum. Pomijam już fakt naruszenia regulaminu. Usuwam /~nospor/ szkoda :(
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 Wersja Lo-Fi Aktualny czas: 24.07.2025 - 19:57