在If语句中使用.toggleClass发出问题

时间:2013-09-29 16:37:07

标签: jquery

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

p.S当我按下我的链接时,为什么#会出现在网址栏中?它不应该出现,因为我正在使用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)
    }

}

1 个答案:

答案 0 :(得分:0)

工作DEMO

试试这个

由于很难合并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)
    }

}