JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://gsgd.co.uk/sandbox/jquery/easing/jquery.easing.1.3.js"></script>
<div id="topMenu">
<div id="animMenu" style="left: -999px; width: 0px;"></div>
<ul>
<li><a href="#" class="mainlevel" id="active_menu">Пункт 1</a></li>
<li><a href="#" class="mainlevel">Пункт 222222</a></li>
<li><a href="#" class="mainlevel">Пункт 3333333333333333</a></li>
<li><a href="#" class="mainlevel">Пункт 444</a></li>
</ul>
</div>
CSS
#topMenu{
clear: both;
position: relative;
overflow: hidden;
top: 23px;
margin-left: 14px;
}
#topMenu a{
font-family: Arial;
font-size: 12px;
font-weight: bold;
text-transform: uppercase;
color: #686868;
text-decoration: none;
display: block;
float: left;
height: 32px;
vertical-align: middle;
padding-top: 14px;
padding-left: 20px;
padding-right: 20px;
border-left: 1px solid #ccc;
z-index: 2;
position: relative;
}
#topMenu a:hover{
text-decoration: none;
color: #3d3d3d;
}
#topMenu a.lastMenuItem{
border-right: 1px solid #ccc;
}
#animMenu{
position: absolute;
width: 0px;
height: 47px;
left: -999px;
background: #c0c0c0;
z-index: 1;
}
JavaScript
jQuery(document).ready(function() {
$('#topMenu').each(function() {
var method = 'easeOutQuint',
topMenu = jQuery('#topMenu'),
activeMenu = jQuery("#active_menu",topMenu),
mainlevel = jQuery('a',topMenu),
animMenu = jQuery('#animMenu')
var animFunc = function(el) {
var menuWidth = el.outerWidth();
var menuLeft = el.position().left;
animMenu.stop(true).animate({
left: menuLeft,
width: menuWidth
}, 1000, method);
}
// если есть активный пункт меню, то позиционируем двигающуюся плашку на нем
animFunc(activeMenu);
// поведение движущейся плашки при наведении на любой пункт меню. Все тоже самое, что и при наличии активного пункта, только позиция плашки определяется относительно пункта, на который произошло наведение курсора мыши
mainlevel.on('mouseenter',function() {
animFunc(jQuery(this));
})
// поведение плашки при окончании события наведения мыши на пункт меню (выход курсора мыши на пределы блока, в котором содержится меню)
topMenu.on('mouseleave',function() {
// иначе, если есть активный пункт меню – возвращаем плашку на него
if (activeMenu.length) {
animFunc(activeMenu);
}
// если активного пункта нет, то перемещаем плашку за границу экрана
else {
animMenu.stop(true).animate({
left: '-999px',
width: '0px'
}, 1000, method);
}
})
})
});