JSFiddle - React, Tailwind, and code Playground

tooltip plugin test

by bitstarr

HTML

<div id="container">
	<div id="info"><abbr title="This is a demo of article called <em>CSS and jQuery Tooltip: Responsive, Mobile-Friendly</em>." rel="tooltip">i</abbr></div>
	<p>Tooltip is a splendid invention. Small detail in web design that plays a big role when it comes to <abbr title="User Experience" rel="tooltip">UX</abbr>. Usually, tooltips are used to present a tiny amount of hidden content (mainly explanatory, so-called tips), that pops up when user moves a cursor over or clicks (less common) on a special target. <a href="http://osvaldas.info/elegant-css-and-jquery-tooltip-responsive-mobile-friendly" title="CSS and jQuery Tooltip: Responsive, Mobile-Friendly" rel="tooltip" style="white-space: nowrap;">Keep reading &rsaquo;</a></p>
</div>

CSS

body {
    font: 1em/1.5 sans-serif;
    color: #111;
    background: #f3f3f3;
}

#container {
    max-width: 40em;
    margin: 5em auto 0;
}
#info {
    width: 60px;
    height: 60px;
    background-color: rgba( 0, 0, 0, .05 );
    -webkit-border-radius: 50%;
    border-radius: 50%;
    padding: 5px;
    margin: 0 auto 50px;
}
#info abbr {
    height: 100%;
    font-size: 2.188em;
    font-weight: bold;
    line-height: 1.4;
    text-align: center;
    text-shadow: 0 1px rgba( 0, 0, 0, .25 );
    color: #fff;
    background: #84e100;
    background: -webkit-linear-gradient( top, #84e100, #6fbc00 );
    background: linear-gradient( top, #84e100, #6fbc00 );
    -webkit-border-radius: 50%;
    -moz-border-radius: 50%;
    border-radius: 50%;
    border: none;
    cursor: help;
    display: block;
}


.tooltip {
    max-width: 20em;
    text-align: center;
    color: #fff;
    background: #111;
    position: absolute;
    z-index: 100;
    padding: 15px;
    opacity: 0;
    -webkit-transition: opacity 0.5s;
    -o-transition: opacity 0.5s;
    transition: opacity 0.5s;
}
.tooltip-visible { opacity: 1 }
@media screen and (min-width: 40em) {
    /* triangle decoration */
    .tooltip:after {
        width: 0;
        height: 0;
        border-left: 10px solid transparent;
        border-right: 10px solid transparent;
        border-top: 10px solid #111;
        content: '';
        position: absolute;
        left: 50%;
        bottom: -10px;
        margin-left: -10px;
    }
}

JavaScript

(function($, window, undefined) {
    $.fn.toolTipper = function(method) {
        var settings = {},
            defaults = {
                containerClass: 'tooltip'
            };

        var methods = {
            init: function (options) {
                settings = $.extend({}, defaults, options);

                var toolTip = $('<div id="tooltip" />')
                    .addClass(settings.containerClass)
                    .appendTo('body');

                return this.each(function() {
                    var $this = $(this),
                    data = $this.data('tooltip');

                    if (!data) {
                        // if the plugin wasn't initialized yet
                        $this.data('tooltip', {
                            target: $this,
                            toolTip: toolTip,
                            title: this.title,
                            active: false
                        });
                    }

                    // define when to show
                    $this.on({
                        'mouseenter.toolTip' : methods.show,
                        'mouseout.toolTip' : methods.hide
                    });

                    // if screen rotates, hide the opened tooltip
                    $( window ).resize(function () {
                        data = $this.data('tooltip');
                        if (data.active) {
                            methods.hide($this);
                        }
                    });
                });
            },
            getPosition: function (data) {
                // get some data about target and position
                var center = data.target.offset().left + ( data.target.outerWidth() / 2 ),
                    ttSize = data.toolTip.outerWidth() / 2,
                    wSize = $( window ).width(),
                    left = Math.round( center - ttSize ),
                    right = Math.round( center + ttSize ),
                    css = {};

       ...