jquery/ui menu mouseover/out/click animation issue -
i ran problem while trying create navigation menu. defined 3 function using jqueryui, onmouseover function, onmouseout function , onclick function. first used emulate hover effect , last 1 change selections. works great unless remove mouse clicked option, before select animation finishes.
here code:
html
<ul id="inav"> <li class="opt selected">option1</li> <li class="opt">option2</li> <li class="opt">option3</li> <li class="opt">option4</li> <li class="opt">option5</li> </ul>
css
body { background: #000; } #inav { display: block; width: 300px; height: 400px; float: left; margin: 0; padding: 0; padding-top: 60px; background: url('../img/nbg.png'); } .opt { display: block; width: 100%; height: 40px; list-style: none; color: #fff; font-size: 1.7em; text-align: center; cursor: pointer; padding-top: 10px; text-shadow: 0 0 1px #fff; } .selected { background: #00f; }
jquery
$('.opt').mouseover(function(){ $(this).animate({backgroundcolor: '#f00'}, 400 ); }); $('.opt').mouseout(function(){ if($(this).hasclass('selected')) { $(this).animate({backgroundcolor: '#00f'}, 400 ); } else { $(this).animate({backgroundcolor: 'transparent'}, 400 ); } }); $('.opt').click(function(){ if(!$(this).hasclass('selected')) { $('.selected').animate({backgroundcolor: 'transparent'}, 400, function(){ $('.opt').removeclass('selected'); }); $(this).animate({backgroundcolor: '#00f'}, 400, function(){ $(this).addclass('selected'); }); } });
here working example of code: click here
i know problem onmouseover function running when should not, have no idea why or how fix it.
any appreciated.
the reason selected item becomes transparent when move out because put $(this).addclass('selected');
in callback function of animation, means li not have class "selected" until animation finished. because li not have class, when move out go inside else part of moveout function , become transparent. solution put addclass , removeclass functions outside of callbacks this:
$('.selected').animate({backgroundcolor: 'transparent'}, 400); $('.opt').removeclass('selected'); $(this).addclass('selected'); $(this).animate({backgroundcolor: '#00f'}, 400);
i have created jsfiddle show how looks like: http://jsfiddle.net/7nrz6/
Comments
Post a Comment