Creating a link swipe effect with Javascript

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 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. Including basic concepts, HTTP and event triggers, activities, callbacks, nested workflows, design patterns and best practices.

Get Your Copy