为什么导航菜单只能点击一次?

时间:2016-09-09 07:13:22

标签: javascript css zurb-foundation classiejs

我正在尝试使用一个教程构建一个全屏导航菜单,但在最后我无法理解为什么我的导航菜单只能点击一次。所以你点击它一次,菜单显示,然后你可以关闭它,但如果我想再次打开它,我必须刷新页面,以便能够再次使用它?任何想法为什么代码不能正常工作?

以下是JSFIDDLE

的示例
    <!-- Navigation Menu -->
              <p><button id="trigger-overlay" type="button">menu</button></p>
             <div class="overlay overlay-slidedown">
              <button type="button" class="overlay-close"></button>
              <nav>
                <ul>
                  <li><a href="#">Home</a></li>
                  <li><a href="#">About us</a></li>
                  <li><a href="#">Categories</a></li>
                  <li><a href="#">Gallery</a></li>
                  <li><a href="#">Events</a></li>
                  <li><a href="#">Blog</a></li>
                  <li><a href="#">Contact us</a></li>
                </ul>
              </nav>
            </div>

    /* Overlay closing cross */
    .overlay .overlay-close {
        width: 80px;
        height: 80px;
        position: absolute;
        right: 20px;
        top: 20px;
        overflow: hidden;
        border: none;
        background: url(../img/cross.png) no-repeat center center;
        text-indent: 200%;
        color: transparent;
        outline: none;
        z-index: 100;
    }

    /* Menu style */

    .overlay nav {
        text-align: center;
        position: relative;
        top: 50%;
        height: 60%;
        -webkit-transform: translateY(-50%);
        transform: translateY(-50%);
        letter-spacing: 10px;
    }

    .overlay ul {
        text-transform: uppercase;
        list-style: none;
        padding: 0;
        margin: 0 auto;
        display: inline-block;
        height: 50%;
        position: relative;
    }

    .overlay ul li {
        display: block;
        height: 30%;
        height: calc(100% / 5);
        min-height: 54px;
        -webkit-backface-visibility: hidden;
        backface-visibility: hidden;
    }

    .overlay ul li a {
        font-size: 3rem;
        font-weight: 300;
        display: block;
        color: #626264;
        -webkit-transition: color 0.2s;
        transition: color 0.2s;
        margin-top: -30px;
        float: left;
        font-weight: 700;
    }

    .overlay ul li a:hover,
    .overlay ul li a:focus {
        color: #FFF;
        text-decoration: underline;
    }

    /* Effects */
    .overlay-slidedown {
        visibility: hidden;
        -webkit-transform: translateY(-100%);
        transform: translateY(-100%);
        -webkit-transition: -webkit-transform 0.4s ease-in-out, visibility 0s 0.4s;
        transition: transform 0.4s ease-in-out, visibility 0s 0.4s;
    }

    .overlay-slidedown.open {
        visibility: visible;
        -webkit-transform: translateY(0%);
        transform: translateY(0%);
        -webkit-transition: -webkit-transform 0.4s ease-in-out;
        transition: transform 0.4s ease-in-out;
    }

    #trigger-overlay{
        margin-top: 50px;
        padding: 5px 0 5px 0;
        float: right;
        color: #FFF;
        text-transform: uppercase;
        border-top: 2px solid #FFF;
        border-bottom: 2px solid #FFF;
    }

    @media screen and (max-height: 30.5em) {
        .overlay nav {
            height: 70%;
            font-size: 34px;
        }
        .overlay ul li {
            min-height: 34px;
        }
    }


function shuffle(array) {
    var currentIndex = array.length
    , temporaryValue
    , randomIndex
    ;

    // While there remain elements to shuffle...
    while (0 !== currentIndex) {
        // Pick a remaining element...
        randomIndex = Math.floor(Math.random() * currentIndex);
        currentIndex -= 1;
        // And swap it with the current element.
        temporaryValue = array[currentIndex];
        array[currentIndex] = array[randomIndex];
        array[randomIndex] = temporaryValue;
    }
    return array;
}

var triggerBttn = document.getElementById( 'trigger-overlay' ),
    overlay = document.querySelector( 'div.overlay' ),
    closeBttn = overlay.querySelector( 'button.overlay-close' ),
    paths = [].slice.call( overlay.querySelectorAll( 'svg > path' ) ),
    pathsTotal = paths.length;

function toggleOverlay() {
    var cnt = 0;

    shuffle( paths );

    if( classie.has( overlay, 'open' ) ) {
        classie.remove( overlay, 'open' );
        classie.add( overlay, 'close' );

        paths.forEach( function( p, i ) {
            setTimeout( function() {
                ++cnt;
                p.style.display = 'none';
                if( cnt === pathsTotal ) {
                    classie.remove( overlay, 'close' );
                }
            }, i * 30 );
        });
    }
    else if( !classie.has( overlay, 'close' ) ) {
        classie.add( overlay, 'open' );
        paths.forEach( function( p, i ) {
            setTimeout( function() {
                p.style.display = 'block';
            }, i * 30 );
        });
    }
}


triggerBttn.addEventListener( 'click', toggleOverlay );
closeBttn.addEventListener( 'click', toggleOverlay );

2 个答案:

答案 0 :(得分:1)

你的else语句中有问题 - 将其更改为 -

else {
    if (classie.has(overlay, 'close')) {
        classie.remove( overlay, 'close' );
    }
    classie.add( overlay, 'open' );
    ... continue of the else statement
}

答案 1 :(得分:1)

我已经改变了你的jsfiddle。

您应该最初添加到您的课程附近并在其他地方进行更改,如下所示:

HTML更改:

<div class="overlay overlay-slidedown close">

Javascript更改:

else if( classie.has( overlay, 'close' ) ) {
            classie.add( overlay, 'open' );
            paths.forEach( function( p, i ) {
                setTimeout( function() {
                    p.style.display = 'block';
                }, i * 30 );
            });
        }

JS小提琴working link