I'm looking to create a sleek custom menu with 5 fields. I want the field under the mouse to expand in width on mouseover, while the other fields shrink in width.
The issue I'm facing is that the transition effect is not smooth and the total width of the menu changes unpredictably when the mouse moves quickly over the menu fields.
Does anyone have a more elegant and functional solution to this problem?
Here is the code I have so far:
function customizeMenu(id) {
document.getElementById('id-m1').style.width = "17%";
document.getElementById('id-m2').style.width = "17%";
document.getElementById('id-m3').style.width = "17%";
document.getElementById('id-m4').style.width = "17%";
document.getElementById('id-m5').style.width = "17%";
document.getElementById(id).style.width = "32%";
return true;
}
function resetMenu(id) {
document.getElementById('id-m1').style.width = "20%";
document.getElementById('id-m2').style.width = "20%";
document.getElementById('id-m3').style.width = "20%";
document.getElementById('id-m4').style.width = "20%";
document.getElementById('id-m5').style.width = "20%";
return true;
}
.menu-item {
float: left;
border: 1px solid white;
margin: 0px;
height: 300px;
background-color: grey;
justify-content: center;
transition: width 1s linear;
}
.menu-item:hover {
cursor: pointer;
}
#id-m1, #id-m2, #id-m3, #id-m4, #id-m5 {
width: 20%;
}
<div onmouseout="resetMenu()">
<div class="menu-item" id="id-m1" onmouseover="customizeMenu('id-m1')"> Menu 1</div>
<div class="menu-item" id="id-m2" onmouseover="customizeMenu('id-m2')"> Menu 2</div>
<div class="menu-item" id="id-m3" onmouseover="customizeMenu('id-m3')"> Menu 3</div>
<div class="menu-item" id="id-m4" onmouseover="customizeMenu('id-m4')"> Menu 4</div>
<div class="menu-item" id="id-m5" onmouseover="customizeMenu('id-m5')"> Menu 5</div>
</div>
Check out the result here.