How can I make an image fall off the screen?

Viewed 319

I have this code to allow me to place a random image next to the user's cursor using javascript and styled using CSS. I would like images to fall down off the page after a few seconds, my first thought was to animate position but apparently that's not possible?

How could I achieve this? Here is my javascript and CSS code

Javascript

<script>

var myPix = new Array("/img/portfolio/30day.jpg", "/img/portfolio/animationposter.jpg","/img/portfolio/beetle.jpg","/img/portfolio/board.jpg","/img/portfolio/cyanotype.jpg","/img/portfolio/dissent.jpg")
document.addEventListener("click", showCoords);

function showCoords(event)
{

var randomNum = Math.floor(Math.random() * myPix.length);
var yourImage = document.createElement("img");
yourImage.src = myPix[randomNum] ;
yourImage.classList.add("mystyle");
yourImage.style.cssText = " width:360px;height:auto;position:fixed;top:" + event.clientY + "px;left:" + event.clientX + "px;";

document.body.appendChild(yourImage);
}
jQuery.fn.reverse = [].reverse;
</script>

CSS

.mystyle {
border-radius: 20px;
box-shadow: 0px 0px 10px 0 rgba(0, 0, 0, 0.1);
z-index: -2;
width: 360px;
height: auto;
position: fixed;


}
1 Answers

First create an images array so you can easily access all your images. Whenever you create an image, push it to the array:

var images = [];
function showCoords(event)
{

var randomNum = Math.floor(Math.random() * myPix.length);
var yourImage = document.createElement("img");
yourImage.src = myPix[randomNum] ;
yourImage.classList.add("mystyle");
yourImage.style.cssText = " width:360px;height:auto;position:fixed;top:" + event.clientY + "px;left:" + event.clientX + "px;";
images.push([yourImage,0,0]); // this line is where we add the image. 
//In the same sub-array, put a number, 0, to store the image's age, and a velocity, 0, to make the physics look good. These will be used later.
document.body.appendChild(yourImage);
}

In order to animate your images, you need to set up some kind of animate loop and call it in a setInterval:

animate = function(){}
setInterval(animate,5); // choose whatever interval you want. Here, it is called every 5 milliseconds

Inside the animate function, we need to add the logic to change every image's position:

animate = function(){
   for(image of images){ // loop over all elements of our array
      image[1] += 1; //increase the age
      if(image[1] > 400){ //if old enough, fall
         image[2] += 0.1; //accelerate, tweak this value to change how strong gravity is
         currentTop = parseFloat(image[0].style.top.replace("px","")); // get the current y position
         currentTop += image[2]; //move
         newTop = String(currentTop) + "px"; //change back to string
         image[0].style.top = newTop; //set the attribute

         if(newTop > document.documentElement.clientHeight){ //if off-screen, remove it
         document.body.removeChild(image[0]);
         images.splice(images.indexOf(image),1); //remove from array
         }
      }
   }
}

Now you should be good to go. I tested this out in Chrome and it worked for the simple case where I just had one image on screen; hopefully I haven't made any typos writing it up here. To change the speed, either change the acceleration value or change the time in the setInterval. Hope this helps!

Edit: here is a working jsFiddle. I had to use spans instead of images because I don't have your exact image files, but everything else is the same.

Related