JSFiddle - React, Tailwind, and code Playground

by satantx

HTML

<div id="topMenu">
<div id="activeMenu" style="left: -999px; width: 0px;"></div>
    <ul>
        <li><a href="#" class="mainlevel" id="active_menu">Пункт 1</a></li>
        <li><a href="#" class="mainlevel">Пункт 2</a></li>
        <li><a href="#" class="mainlevel">Пункт 3</a></li>
        <li><a href="#" class="mainlevel lastMenuItem">Пункт 4</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;
}
#activeMenu{
    position: absolute;
    width: 0px;
    height: 47px;
    left: -999px;
    background: #c0c0c0;
    z-index: 1;
}

JavaScript

jQuery(document).ready(function(){
    if(jQuery("#active_menu").length>0){ // если есть активный пункт меню, то позиционируем двигающуюся плашку на нем
        var menuWidth = jQuery("#active_menu").outerWidth(); // определяем ширину активного пункта меню
        var menuLeft = jQuery("#active_menu").position().left; // определяем смещение активного пункта меню слева
        jQuery("#activeMenu").stop().animate({ // анимируем движущуюся плашку
            left: menuLeft+'px',
            width: menuWidth+'px'
        }, 500, 'linear');
    }
    jQuery("#topMenu a.mainlevel").mouseover(function(){ // поведение движущейся плашки при наведении на любой пункт меню. Все тоже самое, что и при наличии активного пункта, только позиция плашки определяется относительно пункта, на который произошло наведение курсора мыши
            var menuWidth = jQuery(this).outerWidth();
            var menuLeft = jQuery(this).position().left;
            jQuery("#activeMenu").stop().animate({
                left: menuLeft+'px',
                width: menuWidth+'px'
            }, 300, 'linear');      
    });
    jQuery("#topMenu").mouseleave(function(){ // поведение плашки при окончании события наведения мыши на пункт меню (выход курсора мыши на пределы блока, в котором содержится меню)
        if(jQuery("#active_menu").length<=0){ // если активного пункта нет, то перемещаем плашку за границу экрана
            jQuery("#activeMenu").stop().animate({
                left: '-999px',
                width: '0px'
            }, 500, 'linear');
        }
        else{ // иначе, если есть активный пункт меню – возвращаем плашку на него
            var menuWidth = jQuery("#active_menu").outerWidth();
            var menuLeft = jQuery("#active_menu").position().left;
            jQuery("#activeMenu").stop().animate({
                left: menuLeft+'px',
                width: menuWidth+'px'
            }, 500, 'linear');
        }
    });
});