Creating a link swipe effect with Javascript

You can become a serverless blackbelt. Enrol in my course Learn you some Lambda best practice for great good! and learn best practices for performance, cost, security, resilience, observability and scalability. By the end of this course, you should be able to make informed decisions on which AWS service to use with Lambda and how to build highly scalable, resilient and cost efficient serverless applications.

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>
    <nav>
        <ul>
            <li><a href="#">Home</a></li>
            <li><a href="#">Previous</a></li>
            <li><a href="#">Next</a></li>
        </ul>
    </nav>
</div>

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
            span.stop();

            // expand the width of the span to completely cover the <a> element
            // when the mouse moves over
            span.show().animate({
                width: aWidth + "px"
            }, 200);
        }, function () {
            // do a fade off animation when mouse moves off
            span.animate({
                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:

Liked this article? Support me on Patreon and get direct help from me via a private Slack channel or 1-2-1 mentoring.
Subscribe to my newsletter


Hi, I’m Yan. I’m an AWS Serverless Hero and the author of Production-Ready Serverless.

I specialise in rapidly transitioning teams to serverless and building production-ready services on AWS.

Are you struggling with serverless or need guidance on best practices? Do you want someone to review your architecture and help you avoid costly mistakes down the line? Whatever the case, I’m here to help.

Hire me.


Check out my new podcast Real-World Serverless where I talk with engineers who are building amazing things with serverless technologies and discuss the real-world use cases and challenges they face. If you’re interested in what people are actually doing with serverless and what it’s really like to be working with serverless day-to-day, then this is the podcast for you.


Check out my new course, Learn you some Lambda best practice for great good! In this course, you will learn best practices for working with AWS Lambda in terms of performance, cost, security, scalability, resilience and observability. We will also cover latest features from re:Invent 2019 such as Provisioned Concurrency and Lambda Destinations. Enrol now and start learning!


Check out my video course, Complete Guide to AWS Step Functions. In this course, we’ll cover everything you need to know to use AWS Step Functions service effectively. There is something for everyone from beginners to more advanced users looking for design patterns and best practices. Enrol now and start learning!


Are you working with Serverless and looking for expert training to level-up your skills? Or are you looking for a solid foundation to start from? Look no further, register for my Production-Ready Serverless workshop to learn how to build production-grade Serverless applications!

Find a workshop near you