0

The code should explain more what I am trying to do. I have multiple values as javascript variables, which are then processed through an equation to calculate the size of a circle on screen.

Currently I am repeating the equation and numbering all the variables to match the original circle number, is there a way of just running the original variable through this equation without repeating the same code for each circle?

Here is the code...

$( document ).ready(function() {

        circle1 = 914;
        circle2 = 600;

        G1 = circle1/Math.PI;
        H1 = Math.sqrt(G1);
        J1 = Math.round(H1*20);

        J1margin = J1/2;

        $('.circle1').animate({"height":J1, "margin-top":"-"+J1margin, "width":J1, "margin-left":"-"+J1margin});

        G2 = circle2/Math.PI;
        H2 = Math.sqrt(G2);
        J2 = Math.round(H2*20);

        J2margin = J2/2;

        $('.circle2').animate({"height":J2, "margin-top":"-"+J2margin, "width":J2, "margin-left":"-"+J2margin});

});// JavaScript Document

Hopefully you can see what I mean. Its probably a very easy solution but I can't word my question well enough to get an answer through Google. Thanks!

bboybeatle
  • 549
  • 1
  • 8
  • 28

1 Answers1

0

Use a function:

function animateCircle(radius,el) {

        G = radius/Math.PI;
        H = Math.sqrt(G);
        J = Math.round(H*20);

        Jmargin = J/2;

        $(el).animate({"height":J, "margin-top":"-"+Jmargin, "width":J, "margin-left":"-"+Jmargin});
}
 animateCircle(914,'.circle1');
 animateCircle(600,'.circle2');
madalinivascu
  • 32,064
  • 4
  • 39
  • 55