Javascript 如何存储高度值数组并在另一个脚本中使用它们

Javascript 如何存储高度值数组并在另一个脚本中使用它们,javascript,jquery,arrays,jquery-animate,each,Javascript,Jquery,Arrays,Jquery Animate,Each,我有很多东西要学,所以我真的可以使用一些指针: 问题 我尝试在一个条中迭代存储条的高度值 图表 然后我将所有的高度设置为零 然后触发一个条形动画,在该动画中,我向数组中存储的原始值增加高度 问题 在我看来,数组正在被覆盖,而不是 日益充实 我不知道最好的方法是与 我稍后会触发其他脚本 // 在函数调用之外初始化了origHeight,将参数“i”添加到$($theBars)。每个函数(将索引传递给每个调用),并设置MyH:origHeight[i] /* SVG ARRAY \\\\\\\\\

我有很多东西要学,所以我真的可以使用一些指针:

问题

  • 我尝试在一个条中迭代存储条的高度值 图表
  • 然后我将所有的高度设置为零
  • 然后触发一个条形动画,在该动画中,我向数组中存储的原始值增加高度
  • 问题

  • 在我看来,数组正在被覆盖,而不是 日益充实
  • 我不知道最好的方法是与 我稍后会触发其他脚本
  • //

    在函数调用之外初始化了origHeight,将参数“i”添加到$($theBars)。每个函数(将索引传递给每个调用),并设置MyH:origHeight[i]

    /* SVG ARRAY \\\\\\\\\ */
    
    var origHeight = [];
    $(function(){
        var $theBars = $("#v-bars").children();
        var BarsHeight = $theBars.each(function(index, element ) {
            origHeight.push($(this).attr("height"));
            console.log (origHeight);
        });
    
    
        $("#svg-skills-expertise").click(function() {
            $($theBars).each(function(i){
                $(this).css("MyH",$(this).attr("height")).animate(
                {
                    MyH:origHeight[i]
                },
                {
                    step:function(v1){
                        $(this).attr("height",v1)
                    }
                })
            })
            console.log ("Yeap clicked it!");
        });
    });
    

    正在创建
    origHeight
    数组,将1个值推送到该数组,然后在每个循环中重新创建。在函数外部声明数组,现在声明时,范围仅限于该函数内部。
    /* SVG ARRAY \\\\\\\\\ */
    
    var origHeight = [];
    $(function(){
        var $theBars = $("#v-bars").children();
        var BarsHeight = $theBars.each(function(index, element ) {
            origHeight.push($(this).attr("height"));
            console.log (origHeight);
        });
    
    
        $("#svg-skills-expertise").click(function() {
            $($theBars).each(function(i){
                $(this).css("MyH",$(this).attr("height")).animate(
                {
                    MyH:origHeight[i]
                },
                {
                    step:function(v1){
                        $(this).attr("height",v1)
                    }
                })
            })
            console.log ("Yeap clicked it!");
        });
    });