JQuery animation border
I have this html code
<nav id="mainNav">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
He has a css style of this
#mainNav { display:block; padding:5px; }
#mainNav ul { display:block; list-style-type:none; width:500px; border-collapse:collapse; }
#mainNav li { display:inline-block; width:100px; height:66px; text-align:center; padding-top:10px; float:left; background-color:gray; cursor:pointer; border-bottom:0px solid red; }
#mainNav a { color:white; text-decoration:none; text-transform:capitalize; }
#mainNav a.aHover { color:red; }
And the jQuery code attached to it
$(document).ready(function() {
$('#mainNav li').mouseover(function() {
var el = $(this);
$(this).animate({
"border-bottom-width":"+=5px"
},{ queue: false, duration: 500 }, function() {
el.css('border-bottom-width', '5px');
});
$(this).children('a').addClass('aHover');
});
$('#mainNav li').mouseout(function() {
var el = $(this);
$(this).animate({
"border-bottom-width":"-=5px"
},{ queue: false, duration: 500 }, function() {
el.css('border-bottom-width', '0px');
});
$(this).children('a').removeClass('aHover');
});
});
Now what I want to do is either: fade in the border color to red, or cross it out, or as written in the code, expand the border to max. 5px on hover, then undo the border to 0px.
Question: as you can see, I'm trying to change the class of the LI element at the end of the animation to make sure that the border goes to the maximum or minimum width, but this does not work, why?
How would you fade out the color of the frame and out of it?
+5
1 answer
Try below
$(document).ready(function() {
$('#mainNav li').hover(function() {
var el = $(this);
$(this).stop(true, true).animate({
"border-bottom-width": "5px"
}, {
queue: false,
duration: 500
});
$(this).children('a').addClass('aHover');
}, function() {
var el = $(this);
$(this).stop(true, true).animate({
"border-bottom-width": "0px"
}, {
queue: false,
duration: 500
});
$(this).children('a').removeClass('aHover');
});
});
Modified
mouseovereventmouseenteras it is more appropriate in your case- Changed
mouseenter/mouseleavetohover +=5px,-=5px5px0px..stop(true, true), , .
+4