Ver Mensaje Individual
  #2 (permalink)  
Antiguo 20/01/2010, 07:55
quimfv
Colaborador
 
Fecha de Ingreso: marzo-2008
Ubicación: Sabadell
Mensajes: 4.897
Antigüedad: 17 años, 1 mes
Puntos: 574
Respuesta: Problema movimiento aleatorio javascript.

Código HTML:
Ver original
  1. <script language="javascript">
  2. function moure_dalt(ID_objecte) {
  3. var objecte_moure = document.getElementById(ID_objecte);
  4. if (parseInt(objecte_moure.style.top) + 10>90){
  5. objecte_moure.style.top = 90;
  6. }else{
  7. objecte_moure.style.top = parseInt(objecte_moure.style.top) + 10;
  8. }
  9. }
  10.  
  11. function moure(){
  12. var aleatori;
  13. aleatori = Math.floor (Math.random()*4)+1;
  14.  
  15. if (aleatori==1) {
  16. moure_dalt('objecte');
  17. }
  18.  
  19. if (aleatori==2) {
  20. moure_dreta('objecte');
  21. }
  22.  
  23. if (aleatori==3) {
  24. moure_arriba('objecte');
  25. }
  26.  
  27. if (aleatori==4) {
  28. moure_esquerra('objecte');
  29. }
  30. window.setTimeout("moure()",1);
  31.  
  32.  
  33. }
  34. function moure_dreta(ID_objecte) {
  35. var objecte2_moure = document.getElementById(ID_objecte);
  36. if (parseInt(objecte2_moure.style.left) + 10>90){
  37. objecte2_moure.style.left = 90;
  38. }else{
  39. objecte2_moure.style.left = parseInt(objecte2_moure.style.left) + 10;
  40. }
  41. }
  42.  
  43.  
  44. function moure_arriba(ID_objecte) {
  45. var objecte3_moure = document.getElementById(ID_objecte);
  46. if (parseInt(objecte3_moure.style.top) - 10<0){
  47. objecte3_moure.style.top = 0;
  48. }else{
  49. objecte3_moure.style.top = parseInt(objecte3_moure.style.top) - 10;
  50. }
  51. }
  52.  
  53. function moure_esquerra(ID_objecte) {
  54. var objecte4_moure = document.getElementById(ID_objecte);
  55. if (parseInt(objecte4_moure.style.left) - 10<0){
  56. objecte4_moure.style.left = 0;
  57. }else{
  58. objecte4_moure.style.left = parseInt(objecte4_moure.style.left) - 10;
  59. }
  60. }
  61.  
  62.  
  63.  
  64.  
  65. </head>
  66. <body onLoad="moure()">
  67. <br>
  68. <br>
  69. <br>
  70. <br>
  71. <div id="cuerpo" style="width:100px; height:100px; background-color: #F5F5DC; border:1px solid #CCCCCC;margin-bottom:10px;z-index:0">
  72. <div id="objecte" style="position: relative; width:10px; height:10px; left:0px; top:0px; background-color: #cccccc; z-index:1">&nbsp;</div>
  73. </div>
  74. </body>
  75. </html>

Analiza lo que he hecho, no es una solución general depende de la medida del div contenedor y del objeto flotante.

Quim

(Sembla una mosca collonera)

Última edición por quimfv; 20/01/2010 a las 08:00