Creating a link swipe effect with Javascript

Yan Cui

I help clients go faster for less using serverless technologies.

I saw this in last month’s .Net magazine and thought it was pretty cool and worthwhile sharing.

Without giving away too much of the intricacies (seeing as this is not my own work), this technique essentially boils down to using Javascript to append a <span> element to mask over each of the links on the hover event. The <span> elements are set with 0 width initially, on the hover event, an animation is played to change the width to match that of the <a> elements they’re masking, a reverse animation is played when mouse moves out of the <a> elements.

The HTML for the page is simple enough:

<div id="wrapper">
<h1>Link swipe Demo</h1>
            <li><a href="#">Home</a></li>
            <li><a href="#">Previous</a></li>
            <li><a href="#">Next</a></li>

Some simple CSS is used to style the links and the <span> elements but the main thing is really is this linkswipe function which is called once the document finishes loading, I’ve filled in some more comments so hopefully it should be fairly self-explanatory:

function linkswipe() {
    $("li a").each(function (i) {
        // keep a reference to the current <a> element
        var a = $(this);

        // get the title of the link
        var title = a.html();

        // get the width of the element
        var aWidth = a.outerWidth();

        // add a span with the title of the link to mask over the link for the hover event
        // set its initial width and display so that it's invisible
        a.append("<span class='mask' style='width: 0; display: none;'>" + title + "</span>");

        // keep a reference to the new span element
        var span = a.find("span");

        // add the hover event handler
        a.hover(function () {
            // stop any ongoing animation to stop the animation from flickering

            // expand the width of the span to completely cover the <a> element
            // when the mouse moves over
                width: aWidth + "px"
            }, 200);
        }, function () {
            // do a fade off animation when mouse moves off
                width: "0px"
            }, 400, function () {
                span.hide(); // hide the span once the fade animation completes

Here’s a quick demo of the effect, just hover over the links to see it in action:

Whenever you’re ready, here are 4 ways I can help you:

  1. Production-Ready Serverless: Join 20+ AWS Heroes & Community Builders and 1000+ other students in levelling up your serverless game. This is your one-stop shop for quickly levelling up your serverless skills.
  2. Do you want to know how to test serverless architectures with a fast dev & test loop? Check out my latest course, Testing Serverless Architectures and learn the smart way to test serverless.
  3. I help clients launch product ideas, improve their development processes and upskill their teams. If you’d like to work together, then let’s get in touch.
  4. Join my community on Discord, ask questions, and join the discussion on all things AWS and Serverless.

1 thought on “Creating a link swipe effect with Javascript”

  1. How can you adjust the code so this doesnt affect all links wrapped in a LI tag. I only want this effect on my main nav. I can’t seem to figure out how to only apply this effect to the li’s in the nav div.

    Thanks in advance

Leave a Comment

Your email address will not be published. Required fields are marked *