Skip to content

January 22, 2015

A Super-Lightweight jQuery Toast Plugin

Looking for a lightweight toast jQuery plugin?  Here’s a super simple one I wrote for this site, called gtqToast. It minifies to just 520 bytes.

TOAST PLUGIN DEMO

(function($) {
    'use strict';

    var $toastList = $('<ul id="gtqToast"></ul>').appendTo('body');

    //register the plugin
    $.fn.gtqToast = function(options) {

        var settings = $.extend({},
                                {fade:      500,
                                linger:    4000},
                                options),
            $toast = $('<li style="position:absolute;visibility:hidden;"></li>')
                            .appendTo($toastList),
            $container = $('<div></div>')
                            .html(this)
                            .appendTo($toast),
            height = $toast.height();

        $container.hide();
        $toast.css({display: 'none',
                    position: 'static',
                    visibility: 'visible',
                    height: height})
            .slideDown(160, function() {
                $container.fadeIn(settings.fade)
                    .delay(settings.linger)
                    .fadeOut(settings.fade, function() { $toast.remove(); });
            });
    };

    $.gtqToast = function(text, options) {
        $('<span></span>').text(text).gtqToast(options);
    };

})(jQuery);

It needs this bit of CSS as well (note that I assume you use a CSS reset, otherwise you’ll want to add properties like list-style: none):

#gtqToast { position: fixed;
            bottom: 10%;
            z-index: 102;
            text-align: center;
            width: 100%; }
#gtqToast div { display: inline-block;
                background: rgba(0,0,0,0.75);
                color: #fff;
                font-size: 15px;
                font-weight: bold;
                padding: 14px 20px;
                border-radius: 20px;
                box-shadow: 3px 3px 9px 1px rgba(0,0,0,0.3);
                margin-top: 8px; }

What the plugin does: inserts an empty ul (unordered list) tag into the DOM with a fixed bottom position 10% up from the bottom of the viewport. This list will hold the toasts. You can trigger a toast in two ways.

// #1, call on a jQuery object
$('<span>This is my toast</span>').gtqToast();

// #2 call with text
$.gtqToast('Another toast, yay!');

These calls will add the toast to our list and fade them in. If you look at the plugin code, I actually add a div inside the li to contain the toast message. Plus you’ll notice some weird CSS changing. I did this to account for multiple toasts becoming visible at the same time. What will happen is, existing toasts will slide upward when a new toast is added, then the new toast will fade in.

There are only two settings you can change on this basic plugin: fade is the fade in/out speed, and linger is the length of time the toast will stay on the screen. The defaults should be fine for most cases.

Feel free to use this if it fits your needs!

Read more from Javascript

Share your thoughts, post a comment.

(required)
(required)

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments