Solved

jquery animation advice

Posted on 2014-02-24
3
650 Views
Last Modified: 2014-02-28
Hi all,

I want to create a small semi-interactive effect on my site.

Basically I have a number of balloon pngs.

I want to create instance off the bottom of the screen and animate them up across until the are out of bounds at the top where I destroy them.

I would like to have them animate some what randomly and want to try and avoid them being spawned in the same place?

I'm just after some avice on how to do this.
0
Comment
Question by:flynny
  • 2
3 Comments
 
LVL 54

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 39882541
Here is something I knocked together. Animation is achieved with one line of chained JQuery calls.

<!doctype html>
<html>
<head>
<title>Test</title>
<script src="http://code.jquery.com/jquery.js"></script>
<script type="text/javascript">
var startheight = 0;
var startwidth = 0;
$(function() {
  startheight = $(window).height() + 100;
  startwidth = $(window).width() - 100;
  setTimeout('balloon()', 500);
});

function randomMinMax(min,max)
{
    return Math.floor(Math.random()*(max-min+1)+min);
}

function balloon()
{
  var timeout = randomMinMax(250,750);
  var animationspeed = randomMinMax(2500,10000);
  var leftspace = randomMinMax(100, startwidth);
  var pos = '0px -' + (randomMinMax(0,4) * 100) + 'px';
  $('#balloons')
    .append( 
      $('<div/>')
        .addClass('balloon')
        .css({top: startheight + 'px', left: leftspace + 'px', backgroundPosition: pos})
        .animate(
          {top : '-100px'}, 
          animationspeed,
          function() {
            $(this).remove();
          }
        )
    );
  
  setTimeout('balloon()', timeout);
}

</script>
<style type="text/css">
body, html, #balloons {
  height: 100%;
  margin: 0;
  padding: 0;
  overflow: hidden;
}
div.balloon {
  width: 100px;
  height: 75px;
  background-image: url(images/balloons.png);
  background-position: 0 0;
  background-repeat: no-repeat;
  position: absolute;
  top: -9999px;
  left: 90px;
}
</style>
</head>
<body>
<div id="balloons">
</div>
</body>
</html>

Open in new window

Working sample here
balloons.png
0
 

Author Closing Comment

by:flynny
ID: 39894269
many thanks didnt expect code, it really helped.
0
 
LVL 54

Expert Comment

by:Julian Hansen
ID: 39894423
You are welcome - thanks for the points.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question