创建一个数组并在javascript / jquery中循环遍历它

时间:2014-06-11 20:58:32

标签: javascript jquery arrays loops

我创建了一个数组。我想把东西推进阵列,当我点击" pic2"让它运行" runShadow" function ...我希望函数使任何已被推入数组的对象可见:

<script type="text/javascript">
$(document).ready(function () {
    var shadowArray = [];

    function runShadow() { // jquery- ".each"? look this up in stackoverflow!
        console.log("functionWorking!");
        for (var i = 0; i < shadowArray.length; i++) {
            shadowArray[i].animate({
                opacity: 1
            }, 1500, "linear");
        }
    }
    $("#pic1a").click(function () {
        $('#pic2').fadeIn(1500);
        $('#pic1').animate({
            top: -100,
            left: -1000
        }, 1500, "linear");
        $('#pic1a').animate({
            opacity: 0
        }, 1500, "linear");
        $('#pic1b').animate({
            opacity: 0
        }, 1500, "linear");
        $('#pic1c').animate({
            opacity: 0
        }, 1500, "linear");
        $('#pic1d').animate({
            opacity: 0
        }, 1500, "linear");
        $('#pic1e').animate({
            opacity: 0
        }, 1500, "linear");
        $('#pic1f').animate({
            opacity: 0
        }, 1500, "linear");

        shadowArray.push("pic1a");
        console.log(shadowArray);
        console.log("WORKING!");

    });
    $("#pic2").click(function () {
        $('#pic2').fadeOut(1500);
        $('#pic1').animate({
            top: -770,
            left: -800
        }, 1500, "linear");
        runShadow();
    });
});
</script>

1 个答案:

答案 0 :(得分:0)

尝试

$("#"+shadowArray[i]).animate(...)