ModernDesigner
ModernDesigner

Reputation: 7717

CSS + jQuery toggle menu

I am working on a menu using jQuery + CSS3.

I have an up arrow on the right side of the menu and when clicked the menu slides up and the image switches to a down arrow.

The only problem is that if you click the down arrow, it does't slide back down, even though I've provided a somewhat legit piece of code in order for it to work.

I am new to jquery so any help would be very much appreciated!

HTML:

<nav id="tfc-new-nav">
    <div class="wrapper">
        <ul>
            <li><a href="index.html">Home</a></li>
            <li><a href="contact.html">Contact</a></li>
            <li><a href="cart.html">Shopping Cart</a></li>
            <li><a href="login.html">My Account</a></li>
        </ul>
    </div>
    <div class="hide-menu menu-active"></div>
</nav>​

CSS:

.wrapper {
    display: block;
    height: 100%;
    width: 1000px;
    position: relative;
    margin: 0 auto;
}

#tfc-new-nav {
    display: block;
    height: 45px;
    width: 100%;
    background: #808E91;
    position: relative;
    top: 50px;
}

#tfc-new-nav ul {
    list-style: none;
}

#tfc-new-nav ul li {
    display: block;
    height: 45px;
    width: 10%;
    float: left;
    text-align: center;
    line-height: 45px;
}

#tfc-new-nav ul li a {
    display: block;
    color: #fff;
    text-decoration: none;
    text-transform: uppercase;
    font-family: 'Felix Titling', serif;
    cursor: pointer;
    -webkit-transition: background .3s ease-in;
    -moz-transition: background .3s ease-in;
    -ms-transition: background .3s ease-in;
    -o-transition: background .3s ease-in;
    transition: background .3s ease-in;
}

#tfc-new-nav ul li a:hover {
    background: #50798D;
}

#tfc-new-nav .hide-menu {
    position: absolute;
    top: 20px;
    right: 10px;
    cursor: pointer;
}

#tfc-new-nav .hide-menu.menu-active {
    display: block;
    background-image: url('http://upload.wikimedia.org/wikipedia/commons/6/61/Black_Up_Arrow.png');
    background-size: 100% 100%;
    height: 7px;
    width: 7px;
}

#tfc-new-nav .hide-menu.menu-hidden {
    display: block;
    background-image: url('http://www.wpclipart.com/signs_symbol/BW/direction_arrows/down_arrow.png');
    background-size: 100% 100%;
    height: 7px;
    width: 7px;
}​

JavaScript:

$(document).ready(function() {

    $("#tfc-new-nav .hide-menu.menu-active").click(function() {

        $("#tfc-new-nav").animate({

            top: "30px"

        });

        $(this).removeClass("menu-active");
        $(this).addClass("menu-hidden");

        $(this).animate({
            top: "35px"
        });

    });

    $("#tfc-new-nav .hide-menu.menu-hidden").click(function() {

        $("#tfc-new-nav").animate({

            top: "95px"

        });

        $(this).removeClass("menu-hidden");
        $(this).addClass("menu-active");

        $(this).animate({
            top: "20px"
        });

    });

});​

LIVE DEMO

Upvotes: 2

Views: 1756

Answers (3)

Zoltan Toth
Zoltan Toth

Reputation: 47687

You should delegate your events, like

$("#tfc-new-nav").on("click", ".menu-hidden", function() {
    ...
});

DEMO

Upvotes: 3

str11
str11

Reputation: 437

Use .live() instead .click()

Example:

$("#tfc-new-nav .hide-menu.menu-hidden").live("click", function() {
    // Do stuff here
}

This takes into account updated manipulation of the DOM, tested and worked

Upvotes: 0

Shawn Northrop
Shawn Northrop

Reputation: 6036

You should be able to call $.slideToggle() to handle this and minify your code.

Here is a link to the manual entry: http://api.jquery.com/slideToggle/

This will animate and set display:none when the anitmation is complete.

Upvotes: 0

Related Questions