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


Whenever you’re ready, here are 3 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.
  2. Consulting: If you want to improve feature velocity, reduce costs, and make your systems more scalable, secure, and resilient, then let’s work together and make it happen.
  3. Join my FREE Community on Skool, where you can ask for help, share your success stories and hang out with me and other like-minded people without all the negativity from social media.

 

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 *