How to Build a Shifting Underline Hover Effect With CSS and JavaScript

In today’s tutorial, we’re going to use a little bit of CSS and JavaScript to create a fancy menu hover effect. It’s not a complicated end result, yet building it will be a great opportunity to practice our front-end skills.

Without further intro, let’s check out what we’ll be building:

The Markup

We start with some very basic markup; a nav element which contains the menu and an empty span element:

The CSS

With the markup ready, next we specify some basic styles for the related elements:

Notice that the span element (.target) is absolutely positioned. As we’ll see in a moment, we’ll use JavaScript to determine its exact position. In addition, it should appear behind the menu links, so we give it a negative z-index.

The JavaScript

At this point, let’s focus our attention on the required JavaScript. To begin with, we target the desired elements. We also define an array of colors which we’ll use later.

Events

Next we listen for the click and mouseenter events of the menu links. 

When the click event happens, we prevent the page from reloading. Of course, this works in our case because all links have an empty href attribute. In a real project however, each of the menu links would likely open a different page.  

Most importantly, as soon as the mouseenter event fires, the mouseenterFunc callback function is executed:

mouseenterFunc

The body of the mouseenterFunc function looks like this:

Inside this function we do the following:

  1. Add the active class to the immediate parent (li) of the target link.
  2. Decrease the opacity from all menu links, apart from the “active” one.
  3. Use the getBoundingClientRect method to retrieve the size of the associated link and its position relative to the viewport. 
  4. Get a random color from the aforementioned array and pass it as value to the border-color property of the span element. Remember, its initial property value is set to transparent.
  5. Assign the values extracted from the getBoundingClientRect method to the corresponding properties of the span element. In other words, the span tag inherits the size and the position of the link that’s being hovered over.
  6. Reset the default transformation applied to the span element. This behavior is only important the first time we hover over a link. In this case, the transformation of the element goes from transform: translateX(-60px) to transform: none. That gives us a nice slide-in effect.

If Active

It’s important to note that the code above is executed every time we hover over a link. It therefore runs when we hover over an “active” link as well. To prevent this behavior, we wrap the code above inside an if statement:

So far, our demo looks as follows:

Nearly, but Not Quite

So, everything seems to work as expected, right? Well, that’s not true because if we scroll through the page, or resize the viewport, and then try to select a link, things get messy. Specifically, the position of the span element becomes incorrect.

Play around with the full page demo (make sure you’ve added enough dummy content) to see what I mean.

To solve it, we have to calculate how far we’ve scrolled from the top of the window and add this value to the current top value of the target element. In the same way we should calculate how far the document has been scrolled horizontally (just in case). The resulting value is added to the current left value of the target element.

Here are the two lines of code that we update:

Keep in mind that all the code above is executed as soon as the browser processes the DOM and finds the relevant script. Again, for your own implementations and designs you might want to run this code when the page loads, or something like that. In such a scenario, you’ll have to embed it within an event handler (e.g. load event handler).

Viewport

The last thing we have to do is to ensure that the effect will still work as we resize the browser window. To accomplish this, we listen for the resize event and register the resizeFunc event handler.

Here’s the body of this handler:

Inside the function above, we do the following:

  1. Check if there’s a menu list item with the class of active. If there is such an element, that states that we’ve already hovered over a link.
  2.  Get the new left and top properties of the “active” item along with the related window properties and assign them to the span element. Note that we retrieve the values only for the properties that change during the resize event. That means, there’s no need to recalculate the width and height of the menu links.

Browser Support

The demo works well in all recent browsers. If you encounter any issues though, let me know in the comments below. Also, as you’ve possibly noticed, we use Babel to compile our ES6 code down to ES5.

Conclusion

In this tutorial we went through the process of creating a simple, yet interesting menu hover effect.

I hope you enjoyed what we built here and took inspiration for developing even more powerful menu effects like the one appearing (at the time of writing) in the Stripe site.

Have you ever created something similar? If so, be sure to share with us the challenges you faced.


This content originally appeared on Envato Tuts+ Tutorials and was authored by George Martsoukos

In today’s tutorial, we’re going to use a little bit of CSS and JavaScript to create a fancy menu hover effect. It’s not a complicated end result, yet building it will be a great opportunity to practice our front-end skills.

Without further intro, let’s check out what we’ll be building:

The Markup

We start with some very basic markup; a nav element which contains the menu and an empty span element:

The CSS

With the markup ready, next we specify some basic styles for the related elements:

Notice that the span element (.target) is absolutely positioned. As we’ll see in a moment, we’ll use JavaScript to determine its exact position. In addition, it should appear behind the menu links, so we give it a negative z-index.

The JavaScript

At this point, let’s focus our attention on the required JavaScript. To begin with, we target the desired elements. We also define an array of colors which we’ll use later.

Events

Next we listen for the click and mouseenter events of the menu links. 

When the click event happens, we prevent the page from reloading. Of course, this works in our case because all links have an empty href attribute. In a real project however, each of the menu links would likely open a different page.  

Most importantly, as soon as the mouseenter event fires, the mouseenterFunc callback function is executed:

mouseenterFunc

The body of the mouseenterFunc function looks like this:

Inside this function we do the following:

  1. Add the active class to the immediate parent (li) of the target link.
  2. Decrease the opacity from all menu links, apart from the “active” one.
  3. Use the getBoundingClientRect method to retrieve the size of the associated link and its position relative to the viewport. 
  4. Get a random color from the aforementioned array and pass it as value to the border-color property of the span element. Remember, its initial property value is set to transparent.
  5. Assign the values extracted from the getBoundingClientRect method to the corresponding properties of the span element. In other words, the span tag inherits the size and the position of the link that’s being hovered over.
  6. Reset the default transformation applied to the span element. This behavior is only important the first time we hover over a link. In this case, the transformation of the element goes from transform: translateX(-60px) to transform: none. That gives us a nice slide-in effect.

If Active

It's important to note that the code above is executed every time we hover over a link. It therefore runs when we hover over an “active” link as well. To prevent this behavior, we wrap the code above inside an if statement:

So far, our demo looks as follows:

Nearly, but Not Quite

So, everything seems to work as expected, right? Well, that's not true because if we scroll through the page, or resize the viewport, and then try to select a link, things get messy. Specifically, the position of the span element becomes incorrect.

Play around with the full page demo (make sure you’ve added enough dummy content) to see what I mean.

To solve it, we have to calculate how far we’ve scrolled from the top of the window and add this value to the current top value of the target element. In the same way we should calculate how far the document has been scrolled horizontally (just in case). The resulting value is added to the current left value of the target element.

Here are the two lines of code that we update:

Keep in mind that all the code above is executed as soon as the browser processes the DOM and finds the relevant script. Again, for your own implementations and designs you might want to run this code when the page loads, or something like that. In such a scenario, you’ll have to embed it within an event handler (e.g. load event handler).

Viewport

The last thing we have to do is to ensure that the effect will still work as we resize the browser window. To accomplish this, we listen for the resize event and register the resizeFunc event handler.

Here’s the body of this handler:

Inside the function above, we do the following:

  1. Check if there’s a menu list item with the class of active. If there is such an element, that states that we’ve already hovered over a link.
  2.  Get the new left and top properties of the “active” item along with the related window properties and assign them to the span element. Note that we retrieve the values only for the properties that change during the resize event. That means, there’s no need to recalculate the width and height of the menu links.

Browser Support

The demo works well in all recent browsers. If you encounter any issues though, let me know in the comments below. Also, as you’ve possibly noticed, we use Babel to compile our ES6 code down to ES5.

Conclusion

In this tutorial we went through the process of creating a simple, yet interesting menu hover effect.

I hope you enjoyed what we built here and took inspiration for developing even more powerful menu effects like the one appearing (at the time of writing) in the Stripe site.

Have you ever created something similar? If so, be sure to share with us the challenges you faced.


This content originally appeared on Envato Tuts+ Tutorials and was authored by George Martsoukos


Print Share Comment Cite Upload Translate Updates
APA

George Martsoukos | Sciencx (2017-03-22T20:34:33+00:00) How to Build a Shifting Underline Hover Effect With CSS and JavaScript. Retrieved from https://www.scien.cx/2017/03/22/how-to-build-a-shifting-underline-hover-effect-with-css-and-javascript/

MLA
" » How to Build a Shifting Underline Hover Effect With CSS and JavaScript." George Martsoukos | Sciencx - Wednesday March 22, 2017, https://www.scien.cx/2017/03/22/how-to-build-a-shifting-underline-hover-effect-with-css-and-javascript/
HARVARD
George Martsoukos | Sciencx Wednesday March 22, 2017 » How to Build a Shifting Underline Hover Effect With CSS and JavaScript., viewed ,<https://www.scien.cx/2017/03/22/how-to-build-a-shifting-underline-hover-effect-with-css-and-javascript/>
VANCOUVER
George Martsoukos | Sciencx - » How to Build a Shifting Underline Hover Effect With CSS and JavaScript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2017/03/22/how-to-build-a-shifting-underline-hover-effect-with-css-and-javascript/
CHICAGO
" » How to Build a Shifting Underline Hover Effect With CSS and JavaScript." George Martsoukos | Sciencx - Accessed . https://www.scien.cx/2017/03/22/how-to-build-a-shifting-underline-hover-effect-with-css-and-javascript/
IEEE
" » How to Build a Shifting Underline Hover Effect With CSS and JavaScript." George Martsoukos | Sciencx [Online]. Available: https://www.scien.cx/2017/03/22/how-to-build-a-shifting-underline-hover-effect-with-css-and-javascript/. [Accessed: ]
rf:citation
» How to Build a Shifting Underline Hover Effect With CSS and JavaScript | George Martsoukos | Sciencx | https://www.scien.cx/2017/03/22/how-to-build-a-shifting-underline-hover-effect-with-css-and-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.