jquery.toptip.js 1.27 KB
(function($) {
    var template = '<div class="toptip"> \
                        <i class="mark">!</i> \
                        <div class="content"></div> \
                        <div class="close">&times;</div> \
                    </div>';

    var defaultOpts = {
        selector: document.body,
        mark: true,
        close: true
    };

    function hide () {
        this.dom.hide();
    }

    function show () {
        this.dom.css('display','flex');
    }

    function config (options) {
        this.dom.find('.content').text(options.text);

        if (options.mark) {
            this.dom.find('.mark').show();
        } else {
            this.dom.find('.mark').hide();
        }

        if (options.close) {
            this.dom.find('.close').show();
        } else {
            this.dom.find('.close').hide();
        }
    }

    var toptip = function(options) {
        var $dom = $(template).addClass('toptip-' + options.type);
        var opts = $.extend({}, defaultOpts, options);

        var obj = {
            dom: $dom,
            hide: hide,
            show: show,
            reset: config
        };

        obj.reset(opts);

        $(opts.selector).prepend($dom);

        return obj;
    };

    $.extend({
        toptip: toptip
    });
})(jQuery);