Does jQuery Hover effect for NavBar not work?

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 this has something to do with jQuery, but I have it in document.load, so I don’t understand why it is not working.

+5
source share
5 answers

You need to enable after jQuery 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);
});
+1
source

Try it in CSS tag

#navbarcontainer ul li a {

        display: block;
        height: 50px;

        text-decoration: none;
        line-height: 50px;
        width: 100px;
        font-size: 20px;
        font-family: Calibri;
        cursor: pointer;
    }
+1
source

, jQuery. , , , .

, , .

, .

$('#color').on('click',function(){
    $(this).animate({backgroundColor:'#400101', color:'#fff'}); 
});
+1

jQuery, , , , , .

js : http://jsfiddle.net/Mbppv/

js :

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

. : jQuery animate backgroundColor

+1

CSS hover. CSS li :

li.dependant1:hover {
    background-color: red;
}

http://jsfiddle.net/xuHjJ/

0

All Articles