473,395 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to Make a "Ball" move When a Button is Clicked.

1
When the up or down button is clicked I want the "ball" to move up or down. What I have sort of works. When I click up it will move up only ONCE and down only ONCE I want to be able to keep clicking up or down (or left or right) and have it keep moving by 5 pixels. Here's my js code. I have it positioned absolutely in my htm file.

Expand|Select|Wrap|Line Numbers
  1. window.onload = initAll; 
  2.  
  3. function initAll() 
  4. {
  5.     myBallObj=document.getElementById("ball"); 
  6.     myBallObj.style.top = "300px";
  7.     myBallObj.style.left = "300px";
  8.  
  9.     document.getElementById("btnUp").onclick = moveBall;
  10.     document.getElementById("btnDown").onclick = moveBall;
  11.     functions    
  12. }
  13.  
  14. function moveBall()
  15. {
  16.     var y = parseInt(myBallObj.style.left); 
  17.     switch(this.id)
  18.     {
  19.         case "btnUp": y -= 5; break;
  20.         case "btnDown": y += 5; break; 
  21.     }
  22.  
  23.     myBallObj.style.top = y += "px"; 
  24.  
  25. }
  26.  
Jan 12 '12 #1
1 3345
Rabbit
12,516 Expert Mod 8TB
You'll want to use the setInterval() function to call your animation function every so often.
Jan 12 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Wayne Wengert | last post by:
The subject code used to give me a string containing the URL of the calling page. Now I get nothing? How can I get the URL of the calling page? -- ------------------------------------ Wayne...
18
by: day | last post by:
I know I've seen this issue described before, but I can't find it, or the solution now that I need it. I have some css-specified floating divs that contain images or text. The text divs have a...
2
by: Rudolf Ball | last post by:
Hi NG, when trying to drag a UserControl from the toolbox on a Form, this exception rises: The exception was ""null" is not a valid value for the 'stream'.". ? Thank you
3
by: OJ | last post by:
It's easy to control one ball (a graphic) to move and bounce inside a square. But I don't know how to create and control a large number of balls (10 or 100) and count how many times they hit a wall...
13
by: andro | last post by:
Hi everybody! I have several tables from which I want to exract the SAME value (along with other referenced data). All the values are in the same column within the tables. How can I achieve...
13
by: Kobee | last post by:
Hi, I'm having a few issues adapting to new 2.0 "website" project vs. the old 1.1 "web application". One of the major issues I'm having is with the notion of namespaces. Using the old way, I...
15
by: cedmunds | last post by:
Group: We have an application that is calling a stored proc. The stored proc takes anywhere from 15 to 90 minutes to run. In order to keep the GUI responsive, we are using a BackgroundWorker...
10
by: morangolds | last post by:
Hi, I've been having a problem with C++ Windows Forms apps not "ending" when you close the form window. I've searched about this problem all over the place and most searches have lead me to...
11
by: Marcus Otmarsen | last post by:
In my CSS stylesheet I can define e.g. A { FONT-WEIGHT: bold; FONT-SIZE: 16px; } and TD A { FONT-WEIGHT: bold; FONT-SIZE: 12px; } What is the difference for a HTML code like
0
by: =?Utf-8?B?ay1jZXNz?= | last post by:
A nonfunctional icon/box calle "Dialog" appears in taskbar on "Startup" On the left side of this is a blue arrow touching a blue ball. Move & Close are the only optons. What causes this, how do i...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.