-
JavaScript Moving Images
Hi, I’m basically new to everything that is to do with web design – such as HTML, CSS and JavaScript. I’ve started to play around with JavaScript as I have an interest in coding, but I’m used to languages like the original BASIC that was used on Spectrums, though I also know assembly language for the Spectrum.
Yesterday I followed this JavaScript tutorial: Moving Image Inside HTML page: Javascript ( A Small Fun Application ), which is available here: https://technotip.com/269/moving-image-javascript-small-fun-application/
The script allows the user to input x and y coordinates for an image and then moves an image to that location on the screen. The image jumps from the old position to the new position. I’ve tried to amend the script so that the image can be seen moving from the old position to the new position, without jumping, so that you can actually see the image moving across the screen, but I can’t get the image to be displayed every time round the loop. For example, in BASIC you would just print the image onto the screen at the new coordinates every time round the loop, and then just increase or decrease the variables containing the coordinates in the same loop. How can I print the image to the screen every time round the loop in JavaScript? I’d like to learn how to do this without using jQuery. There are other issues with the coding, such as checking whether the y coordinate has moved off the screen, but just ignore that for now and enter zero for both x and y when prompted (I’m just interested in learning how to get the image to be displayed for now).
This is my amended script, though I think the format may get a bit muddled in the forum page:
Move It
#kids { position: relative; top: 400px; left: 400px; }
// These variables will be used to move the image from coordinates 400, 400:
var movx = 400;
var movy = 400;function moveIt()
{
var newx = 0;
while (movx > newx)
{
var newx = document.getElementById(“x”).value;
var newy = document.getElementById(“y”).value;var kids = document.getElementById(“kids”).style;
kids.top = movx + “px”;
kids.left = movy + “px”;// For now I’m assuming the new coordinates are nearer the top left of the screen.
movx–;
movy–;// This next bit is just to slow things down:
var i = 0;
while (i<1000000)
{
i++;
}
}}
X axis:
Y axis:
Any help would be much appreciated!
Thanks,
Simon.