Wouldn't it be cool to have a magic line that follows your mouse as you navigate through the header menu?
Take a look at this example:
It works seamlessly and smoothly.
I tried implementing a similar jQuery script myself, but it's not as smooth as I'd like it to be.
Check out my HTML code below:
<nav>
<ul>
<li>
<a href="" class="active"><p>Home</p></a>
</li>
// more list items...
<li id="magic-line"></li>
</ul>
</nav>
Here is the CSS for the magic line:
#magic-line{
display: block;
position: absolute;
width: 100px;
height: 3px;
background-color: #7190C9;
left: 0px;
top:97px;
}
And here is the jQuery script I'm currently using:
var bar = $("#magic-line");
$("a").hover(function(){
var link = $(this);
switch(link.children("p").html()) {
case "Home":
var linkWidth = link.parent("li").width();
var offset = link.offset().left;
bar.css({left : offset, width : linkWidth, transition : "0.5s"});
break;
// additional cases...
}
});
Do you have any suggestions on how I can improve this script?
You can view my script in action on JsFiddle here:
https://jsfiddle.net/3jeu2L7v/
Although it runs smoother there than on my actual website. Any insights on why this might be happening?