简体   繁体   English

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

[英]Why navigation menu is clickable only one time?

I am trying to build a full screen navigation menu using one tutorial but at the end of it I can not understand why my navigation menu is clickable only once. 我正在尝试使用一个教程来构建全屏导航菜单,但是在它的结尾我无法理解为什么我的导航菜单只能单击一次。 So you click on it one time and the menu is showing then you are able to close it but if I want to open it again I have to refresh the page to be able to use it again ? 因此,您单击它一次,然后显示菜单,则可以将其关闭,但是如果我想再次打开它,则必须刷新页面才能再次使用它? Any ideas why the code is not working as it should be ? 有什么想法为什么代码不能正常工作?

Here is example on JSFIDDLE 这是关于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 );

you have a problem in your else statement - change it to - 您在else语句中遇到问题-将其更改为-

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

I had made changes to your jsfiddle. 我对您的jsfiddle进行了更改。

You should probably add close to your class initially and make changes in else if as following: 最初,您可能应该在类附近添加一个,然后在else中进行更改,如下所示:

HTML change: HTML更改:

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

Javascript Change: 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 Fiddle working link JS小提琴工作链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM