Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Hover Effect for NavBar not working?

For some reason, I just can't get the hover effect to work
HTML:

<div id="navbarcontainer">
    <ul>
        <li id="left" class="current">
            <a id="current">Home</a></li>
        <li class="dependant1">
            <a id="dependant1">Services</a></li>
        <li id="right" class="dependant2">
            <a id="dependant2">Contact</a></li>
    </ul>
</div>

CSS:

#navbarcontainer {
margin: 0;
padding: 0;
height: 50px;
background: #01216D;
}

#navbarcontainer ul {
    clear: both;
    margin: 0;
    padding: 0;
    list-style: none;
}

#navbarcontainer li {
    display: inline-block;
    height: 50px;
    width: 100px;
    list-style: none;
    text-align: center;
    -moz-transition: .5s;
    -o-transition: .5s;
    -webkit-transition: .5s;
    transition: .5s;
    /* Firefox 4 */
    /* Opera */
    /* Safari and Chrome */
}

    #navbarcontainer ul li a {
        text-decoration: none;
        line-height: 50px;
        width: 100px;
        font-size: 20px;
        font-family: Calibri;
        cursor: pointer;
    }

#left {
    margin-right: 40px;
    margin-left: 20px;
}

#right {
    margin-left: 40px;
}

.current {
    background: #fff;
}

#current {
    color: #01216D;
font-weight: bold;
}

#dependant1, #dependant2 {
   color: #fff;
}

jQuery:

$("#dependant1").hover(function () {
    $('.dependant1').stop().animate({background: '#fff' }, "slow");
    $('#dependant1').stop().animate({color: '#01216D', 'font-weight': 'bold'}, "slow");
}, function () {
    $('.dependant1').stop().animate({ background: 'none' }, "slow");
    $('#dependant1').stop().animate({color: '#fff', 'font-weight': 'normal'}, "slow");
});

I feel like it has something to do with the jQuery, but I have it in the document.load, so I don't understand why it isn't working.

like image 577
Karmaxdw Avatar asked Oct 04 '22 12:10

Karmaxdw


2 Answers

You need to include after jQuery the jQuery UI Library :

LIVE DEMO

$("#navbarcontainer li").hover(function () {
    $(this).find('a').stop().animate({ color: '#01216D', backgroundColor: '#fff'}, 800);
}, function () {
    $(this).find('a').stop().animate({ color: '#fff', backgroundColor: '#01216D'}, 800);
 });

HTML:

 <div id="navbarcontainer">
    <ul>
      <li><a id="current">Home</a></li>
      <li><a>Services</a></li>
      <li><a>Contact</a></li>
    </ul>
</div> 

CSS:

#navbarcontainer {
    margin: 0;
    padding: 0;
    background: #01216D;
}
#navbarcontainer ul {
    clear: both;
    margin: 0;
    padding: 0;
    list-style: none;
    overflow:hidden;
}
#navbarcontainer li {
    list-style: none;
    text-align: center;
    -moz-transition: .5s;
    -o-transition: .5s;
    -webkit-transition: .5s;
    transition: .5s;
}
#navbarcontainer ul li a {
    float:left;
    color:#fff;
    background: #01216D;
    padding:10px 30px;
    text-decoration: none;
    line-height: 50px;
    font-size: 20px;
    font-family: Calibri;
    cursor: pointer;
}
#current {
    background: #fff !important;
    color: #01216D !important;
    font-weight: bold;
}

Or another script:

$("#navbarcontainer li").on('mouseenter mouseleave',function ( e ) {
  var set = e.type=='mouseenter' ? {c:"#01216D", bg:"#fff"} : {c:"#fff", bg:"#01216D"} ;
  $('a', this).stop().animate({ color: set.c, backgroundColor: set.bg}, 800);
});
like image 60
Roko C. Buljan Avatar answered Oct 09 '22 22:10

Roko C. Buljan


Try this in your a tag CSS

#navbarcontainer ul li a {

        display: block;
        height: 50px;

        text-decoration: none;
        line-height: 50px;
        width: 100px;
        font-size: 20px;
        font-family: Calibri;
        cursor: pointer;
    }
like image 44
Daniel Avatar answered Oct 09 '22 21:10

Daniel