Solved

jquery animation advice

Posted on 2014-02-24
3
664 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 55

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 55

Expert Comment

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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Asp in script 6 35
forgetful about js objects 8 46
jQuery dialog box help 2 17
How  can  I extract  Id  from a  URL  using  Javascript? 12 29
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…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

766 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