开发者

javascript menu

I'm trying to make a menu's elements show and disappear, but i stops at a certain level, why?

<script language="javascript">
tre = [1, 0, 0, 0, 0];
function tree(e) {
    elm = document.getElementsByClassName("sub")[e];
    if (e == 1) {
        n = 73;
    }
    else if (e == 3 || e == 4) {
        n = 168;
    }
    else {
        n = 144;
    }
    ex = e;
    if (tre[ex] == 0) {
        i = 0;
        tmp1 = setInterval(function() {
            if (i <= 100 && i >= 0) {
                elm.style.height = (i / 100) * n + "px";
                document.getElementById('wait').innerHTML += i + ' ';
                i++;
            } else {
                clearInterval(tmp1);
                tre[ex] = 1;
            }
        }, 2);
    }
    else {
        j = 100;
        tmp2 = setInterval(function() {
            if (j >= 0) {
                elm.style.height = (j / 100) * n + "px";
                j--;
            } else {
                    clearInterval(tmp2);
                tre[ex] = 0;
            }
    }, 2);
    }
}
</script>
<style>
div {overflow: hidden;}
</style>
<span onClick="tree(0);"&g开发者_如何学Got;Toogle 0</span><br />
<div class="sub">test 0<br />test 0<br />test 0<br />test 0<br />test 0<br />test 0</div>
<span onClick="tree(1);">Toogle 1</span><br />
<div class="sub">test 1<br />test 1<br />test 1</div>
<span onClick="tree(2);">Toogle 2</span><br />
<div class="sub">test 2<br />test 2<br />test 2<br />test 2<br />test 2<br />test 2</div>
<span onClick="tree(3);">Toogle 3</span><br />
<div class="sub">test 3<br />test 3<br />test 3<br />test 3<br />test 3<br />test 3<br />test 3</div>
<span onClick="tree(4);">Toogle 4</span><br />
<div class="sub">test 4<br />test 4<br />test 4<br />test 4<br />test 4<br />test 4<br />test 4</div>


According to your logic the code will work only, if you declare tre array as tre = [1, 1, 1, 1, 1]; This slidedown and slideup effects for menus can easily be implemented using Jqueries without going for these types of static calculations that you have done in your code..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜