将相同的数学方程应用于多个jQuery值

时间:2015-05-14 11:18:07

标签: javascript jquery css equations

代码应该更多地解释我想要做的事情。我有多个值作为javascript变量,然后通过等式处理以计算屏幕上圆圈的大小。

目前我正在重复方程并对所有变量进行编号以匹配原始的圆数,是否有办法通过此等式运行原始变量而不重复每个圆的相同代码?

这是代码......

$( 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

希望你能明白我的意思。它可能是一个非常简单的解决方案,但我不能很好地说出我的问题,以便通过Google获得答案。谢谢!

1 个答案:

答案 0 :(得分:0)

使用功能:

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');