1

我正在尝试制作一个 jQuery 脚本来显示和隐藏导航,同时更改另一个 div 的类。它几乎可以工作,当我第一次按我的时候它工作正常,a#mobile-nav-button但后来它不能同步工作。它应该是这样工作的:如果我按下链接,show-nav当我再次按下内容获取类时,内容面板会获取类hide-nav,并且应该始终遵循导航。显示它向左滑动,隐藏它向后滑动。我应该改变什么才能以正确的方式同步它?我感谢任何帮助!

pS任何人为什么当我点击我的链接时,#会出现在url栏中?它不应该出现,因为我使用的是 jQuery,而且我的 1000 时间也不起作用..

$(document).ready(function() {
    $("#mobile-nav-button").click(function() {

        $("#mobile-nav-button").toggleClass("navon");
        $("#mobile-content-panel").toggleClass("show-nav");

        if($("#mobile-nav-button").hasClass("navon")) {
            $("#mobile-nav").show('slide', {
                direction: "left"
            }, 1000);

        } else {
            $("#mobile-nav").hide('slide', {
                direction: "left"
            }, 1000);
            $("#mobile-content-panel").toggleClass("hide-nav");
        }
    });
});

HTML:

<div id="mobile-header">
   <a id="mobile-nav-button" class="navbutton" href="#">
       <span>Menu</span>
   </a>

   <h1>Header</h1>
</div>

<div id="mobile-nav">
    <nav id="mobile-menu-nav">
        <a href="#">Link #1</a>
        <a href="#">Link #2</a>
        <a href="#">Link #3</a>
    </nav>
</div>

<div id="mobile-content-panel" class="panel">
    //Content
</div>

CSS:

div#mobile-header {
    position: absolute;
    width: 100%;
    height: 43px;
    background-color: #003C63;
    color: #FFFFFF;
    display: block;
    z-index: 10;
    box-shadow: 0px 1px 5px rgba(0, 0, 0, 0.25);
    text-align: center;
}

div#mobile-header a {
    position: absolute;
    left: 5px;
    top: 7px;
    height: 28px;
    line-height: 26px;
    padding: 0 8px;
}

div#mobile-header a span {
    background: url(img/menu_icon.png) no-repeat scroll center center / 20px 20px transparent;
    width: 30px;
    height: 30px;
    margin-left: -5px;
    display: inline-block;
    text-indent: -99999px;
}

div#mobile-header h1 {
    font-family: Roboto, Arial, sans-serif;
    color: #FFFFFF;
    font-size: 18px;
    font-weight: bold;
    line-height: 42px;
    width: 70%;
    margin: 0 auto;
}

div#mobile-nav {
    position: absolute;
    left: 0;
    top: 43px;
    width: 272px;
    height: 100%;
    max-height: none;
    display: block;
    background-color: #003C63;
    border-right: 1px solid #999999;
    z-index: 5;
    display: none;
}

nav#mobile-menu-nav a {
    position: relative;
    color: #FFFFFF;
    display: block;
    font-size: 15px;
    font-weight: bold;
    padding: 15px 25px 15px 25px;
    text-decoration: none;
    text-shadow: none;
    overflow: hidden;
    background-color: #003C63;
    border-bottom: 1px solid #DDDDDD;
    white-space: nowrap;
    text-overflow: ellipsis;
}

nav#mobile-menu-nav a:first-child {
    border-top: 1px solid #DDDDDD;
}

div#mobile-content-panel {
    box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.9);
    background-color: #E3E3E3;
    margin: 40px 0 0 0;
    padding: 20px 5px 0 5px;
    z-index: 7;
}

/* Showing & Hiding Navigation Panel */
div.show-nav {
    -webkit-transform: translatex(272px);
    -moz-transform: translatex(272px);
    -webkit-animation-name: shownav;
    -moz-animation-name: shownav;
    -webkit-animation-timing-function: ease-out;
    -moz-animation-timing-function: ease-out;
    animation-timing-function: ease-out;
    -webkit-animation-duration: 175ms;
    -moz-animation-duration: 175ms;
    animation-duration: 175ms;
}

@-webkit-keyframes shownav {
    from{
        -webkit-transform:translatex(0)
    }

    to{
        -webkit-transform:translatex(272px)
    }

}
@-moz-keyframes shownav {
    from{
        -moz-transform:translatex(0)
    }

    to{
        -moz-transform:translatex(272px)
    }

}

div.hide-nav{
    -webkit-transform: translatex(0);
    -moz-transform: translatex(0);
    -webkit-animation-name: hidenav;
    -moz-animation-name: hidenav;
    -webkit-animation-timing-function: ease-out;
    -moz-animation-timing-function: ease-out;
    animation-timing-function: ease-out;
    -webkit-animation-duration: 175ms;
    -moz-animation-duration: 175ms;
    animation-duration: 175ms;
}

@-webkit-keyframes hidenav {
    from{
        -webkit-transform:translatex(272px)
    }

    to{
        -webkit-transform:translatex(0)
    }

}
@-moz-keyframes hidenav {
    from{
        -moz-transform:translatex(272px)
    }

    to{
        -moz-transform:translatex(0)
    }

}
4

1 回答 1

0

工作演示

尝试这个

由于 css 和 jquery 动画时间很难合并,所以我使用了 setTimeout()

代码

$(document).ready(function() {
    $("#mobile-nav-button").click(function() {

        $("#mobile-nav-button").toggleClass("navon");


        if($("#mobile-nav-button").hasClass("navon")) {
             $("#mobile-content-panel").removeClass("hide-nav");
            $("#mobile-nav").show('slide', {
                direction: "left"
            }, 1000);
            setTimeout(function(){$("#mobile-content-panel").addClass("show-nav");},130);

        } else {
            $("#mobile-content-panel").removeClass("show-nav");
            $("#mobile-nav").hide('slide', {
                direction: "left"
            }, 1000);
            setTimeout(function() {$("#mobile-content-panel").addClass("hide-nav");},100);
        }
    });
});

css

演示中我更改了span背景,因为该图像不可用,请更改它

div#mobile-header {
    position: absolute;
    width: 100%;
    height: 43px;
    background-color: #003C63;
    color: #FFFFFF;
    display: block;
    z-index: 10;
    box-shadow: 0px 1px 5px rgba(0, 0, 0, 0.25);
    text-align: center;
}

div#mobile-header a {
    position: absolute;
    left: 5px;
    top: 7px;
    height: 28px;
    line-height: 26px;
    padding: 0 8px;
}

div#mobile-header a span {
   background: url(img/menu_icon.png) no-repeat scroll center center / 20px 20px transparent;
    width: 30px;
    height: 30px;
    margin-left: -5px;
    display: inline-block;
    text-indent: -99999px;
}

div#mobile-header h1 {
    font-family: Roboto, Arial, sans-serif;
    color: #FFFFFF;
    font-size: 18px;
    font-weight: bold;
    line-height: 42px;
    width: 70%;
    margin: 0 auto;
}

div#mobile-nav {
    position: absolute;
    left: 0;
    top: 43px;
    width: 272px;
    height: 100%;
    max-height: none;
    display: block;
    background-color: #003C63;
    border-right: 1px solid #999999;
    z-index: 5;
    display: none;
}

nav#mobile-menu-nav a {
    position: relative;
    color: #FFFFFF;
    display: block;
    font-size: 15px;
    font-weight: bold;
    padding: 15px 25px 15px 25px;
    text-decoration: none;
    text-shadow: none;
    overflow: hidden;
    background-color: #003C63;
    border-bottom: 1px solid #DDDDDD;
    white-space: nowrap;
    text-overflow: ellipsis;
}

nav#mobile-menu-nav a:first-child {
    border-top: 1px solid #DDDDDD;
}

div#mobile-content-panel {
    box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.9);
    background-color: #E3E3E3;
    margin: 40px 0 0 0;
    padding: 20px 5px 0 5px;
    z-index: 7;
}

/* Showing & Hiding Navigation Panel */
div.show-nav {
    -webkit-transform: translatex(272px);
    -moz-transform: translatex(272px);
    -webkit-animation-name: shownav;
    -moz-animation-name: shownav;
    -webkit-animation-timing-function: ease-out;
    -moz-animation-timing-function: ease-out;
    animation-timing-function: ease-out;
    -webkit-animation-duration: 1000ms;
    -moz-animation-duration: 1000ms;
    animation-duration: 1000ms;
}

@-webkit-keyframes shownav {
    from{
        -webkit-transform:translatex(0)
    }

    to{
        -webkit-transform:translatex(272px)
    }

}
@-moz-keyframes shownav {
    from{
        -moz-transform:translatex(0)
    }

    to{
        -moz-transform:translatex(272px)
    }

}

div.hide-nav{
    -webkit-transform: translatex(0);
    -moz-transform: translatex(0);
    -webkit-animation-name: hidenav;
    -moz-animation-name: hidenav;
    -webkit-animation-timing-function: ease-out;
    -moz-animation-timing-function: ease-out;
    animation-timing-function: ease-out;
    -webkit-animation-duration: 1000ms;
    -moz-animation-duration: 1000ms;
    animation-duration: 1500ms;
}

@-webkit-keyframes hidenav {
    from{
        -webkit-transform:translatex(272px)
    }

    to{
        -webkit-transform:translatex(0)
    }

}
@-moz-keyframes hidenav {
    from{
        -moz-transform:translatex(272px)
    }

    to{
        -moz-transform:translatex(0)
    }

}
于 2013-09-29T18:30:54.013 回答