「balloon」という名前の要素が画面内をジグザグしながら行ったり来たりするスクリプトが動かないのですが、原因が分かりません。
ご教授よろしくお願いいたします。
javascript
1function Moving(){ 2var base_x; 3var base_y; 4var max_speed = 3; 5var index_x = 0; 6var index_y = 0; 7var vx1 = 1; 8var vx2 = 1; 9var vy1 = 1; 10var vy2 = 1; 11vx1 = Math.ceil(Math.random() * max_speed); 12 13if(Math.round(Math.random)) vx2 *= -1; 14vy1 = Math.ceil(Math.random() * max_speed); 15 16if(Math.round(Math.random)) vy2 *= -1; 17base_x = $("#balloon").css("left"); 18base_y = $("#balloon").css("top"); 19 20setInterval(function(){ 21 index_x += (vx1 * vx2); 22 index_y += (vy1 * vy2); 23 24 if((index_x > 100) || (index_x < -100)){ 25 if(index_x > 100){ 26 index_x = 100; 27 }else{ 28 index_x = -100; 29 } 30 31 vx1 = Math.ceil(Math.random() * max_speed); 32 vx2 *= -1; 33 vy1 = Math.ceil(Math.random() * max_speed); 34 } 35 36 if((index_y > 20) || (index_y < -20)){ 37 if(index_y > 20){ 38 index_y = 20; 39 }else{ 40 index_y = -20; 41 } 42 43 vx1 = Math.ceil(Math.random() * max_speed); 44 vy1 = Math.ceil(Math.random() * max_speed); 45 vy2 *= -1; 46 } 47 48 $("#balloon").css('left', base_x + index_x ); 49 $("#balloon").css('top', base_y + index_y ); 50},200); 51} 52Moving();
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。