Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [js] wleczenie się za / gonieie kursora ze spowolnieniem
Forum PHP.pl > Forum > Przedszkole
23kulpamens
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
nospor
  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. checker();
  25. if(DELAY > 0){ DELAY--;}
  26. }
  27.  
  28. function checker(){
  29. setTimeout("update()", DELAY);
  30. if( tempX != XX || tempY != YY){
  31.  
  32. }
  33. else{
  34. DELAY = 100;
  35. }
  36. }
  37.  
  38. function getMouseXY(e) {
  39. if (window.IE) { // grab the x-y pos.s if browser is IE
  40. tempX = event.clientX + document.body.scrollLeft
  41. tempY = event.clientY + document.body.scrollTop
  42. } else { // grab the x-y pos.s if browser is NS
  43. tempX = e.pageX
  44. tempY = e.pageY
  45. }
  46. // catch possible negative values in NS4
  47. if (tempX < 0){tempX = 0}
  48. if (tempY < 0){tempY = 0}
  49. return true
  50. }
  51.  
  52. function count(){
  53. //setInterval("checker()", 1);
  54. checker();
  55. if (!IE) document.captureEvents(Event.MOUSEMOVE)
  56. document.onmousemove = getMouseXY;
  57. }
  58.  
  59.  
  60. </head>
  61. <div id="blok" style="background-color: aqua; width: 500px; position: absolute; t">
  62. <input id="xxx" name="strength" value="" />
  63. <input id="yyy" name="strength" value="" />
  64. </div>
  65. <input id="info1" name="strength" value="" />
  66. <input id="info2" name="strength" value="" />
  67. <script type="text/javascript">count();</script>
  68. </body>
  69. </html>
23kulpamens
Dzięki, przydało się winksmiley.jpg
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.