Add a script tag at the bottom of the page, where you'll put all your code.
Create a variable to store a reference to the img.
Change the style of the img to have a "left" of "0px", so that it starts at the left hand of the screens.
Create a function called catWalk() that moves the cat 10 pixels to the right of where it started, by
changing the "left" style property.
Call that function every 50 milliseconds. Your cat should now be moving across the screen from left to right. Hurrah!
var img = document.getElementsByTagName('img')[0];
img.style.left = '0px';
function catWalk() {
img.style.left = (parseInt(img.style.left) + 10) + 'px';
}
window.setInterval(catWalk, 50);
Bonus #1: When the cat reaches the right-hand of the screen, restart them at the left hand side ("0px"). So they should keep walking from left to right across the screen, forever and ever.
var img = document.getElementsByTagName('img')[0];
img.style.left = '0px';
function catWalk() {
var currentLeft = parseInt(img.style.left);
img.style.left = (currentLeft + 10) + 'px';
if (currentLeft > (window.innerWidth-img.width)) {
img.style.left = '0px';
}
}
window.setInterval(catWalk, 50);
Bonus #2: When the cat reaches the right-hand of the screen, make them move backwards instead. They should keep walking back and forth forever and ever.
var img = document.getElementsByTagName('img')[0];
img.style.left = '0px';
var walkForwards = true;
function catWalk() {
var currentLeft = parseInt(img.style.left);
if (walkForwards && (currentLeft > (window.innerWidth-img.width))) {
walkForwards = false;
}
if (!walkForwards && (currentLeft <= 0)) {
walkForwards = true;
}
if (walkForwards) {
img.style.left = (currentLeft + 10) + 'px';
} else {
img.style.left = (currentLeft - 10) + 'px';
}
}
window.setInterval(catWalk, 50);
Bonus #3: When the cat reaches the middle of the screen, replace the img with an image of a cat dancing, keep it dancing for 10 seconds, and then replace the img with the original image and have it continue the walk.