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>
            <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:

Enjoy what you’re reading? Subscribe to my newsletter and get more content on AWS and serverless technologies delivered straight to your inbox.

Yan Cui

I’m an AWS Serverless Hero and the author of Production-Ready Serverless. I have run production workload at scale in AWS for nearly 10 years and I have been an architect or principal engineer with a variety of industries ranging from banking, e-commerce, sports streaming to mobile gaming. I currently work as an independent consultant focused on AWS and serverless.

You can contact me via Email, Twitter and LinkedIn.

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, design patterns and best practices.

Get Your Copy