Menyediakan informasi seputar perkembangan mesin Blogger dan internet secara umum.

UPDATE :: jQuery Toolpik Revision 1

Toolpik - A jQuery Tooltip

I decided to update this Toolpik because I realize that there are some fatal mistakes about HTML manipulation when mouseout and things that I think still can be shortened. In addition, a problem which in my opinion (and all of us) the most annoying is that Toolpik not have the ability to keep itself stay on the visible area:

Toolpik tooltip BUG
ARRGGHHH!!!

And moreover, the acronym tag is already deprecated and was replaced by abbr (Read: Avoid the Deprecated <acronym> Tag in HTML 5). But, if you want to continue include Toolpik for the acronym tag, just add it beside the abbr with separate comma:

var ttTarget = "abbr, acronym";

I don't know who is the author on this discussion, but gratitude to him for all of his knowledge. This is how I keep Toolpik stay on the visible area:

// Get the toolpik width and height
var toolpikWidth  = $("#toolpik").outerWidth(),
    toolpikHeight = $("#toolpik").outerHeight(),

    // Get the window width and height
    winWidth      = $(window).width(),
    winHeight     = $(window).height(),

    top           = e.clientY - toolpikHeight/2, // Set top and bottom position of the toolpik
    left          = e.clientX - toolpikWidth/2;  // Set the left and right position of the toolpik

if (top + toolpikHeight > winHeight) {
    // Flip the toolpik position to the top of the object
    // so it won't go out of the current window height
    // and show up in the correct place
    top = winHeight - toolpikHeight - 50;
} else if (top <= 0) {
    top = 10;
} else {
    top = top;
}

if (left + toolpikWidth > winWidth) {
    // Shift the toolpik position to the left of the object
    // so it won't go out of width of current window width
    // and show up in the correct place
    left = winWidth - toolpikWidth - 50;
} else if (left <= 0) {
    left = 10;
} else {
    left = left;
}

Complete Code

$(function() {

    $(ttTarget).css('cursor', 'help').hover(function(e) {

        $(this).append('<div id="toolpik"><div id="nurrohman"></div><div style="clear:both;"></div><img class="absol" src="' + ttImg + '" alt="" /></div>');
        $('#nurrohman', '#toolpik').html($(this).attr('title'));
        $(this).removeAttr('title');

        // Get the toolpik width and height
        var toolpikWidth  = $("#toolpik").outerWidth(),
            toolpikHeight = $("#toolpik").outerHeight(),

            // Get the window width and height
            winWidth      = $(window).width(),
            winHeight     = $(window).height(),

            top           = e.clientY - toolpikHeight/2, // Set top and bottom position of the toolpik
            left          = e.clientX - toolpikWidth/2;  // Set the left and right position of the toolpik

        if (top + toolpikHeight > winHeight) {
            // Flip the toolpik position to the top of the object
            // so it won't go out of the current window height
            // and show up in the correct place
            top = winHeight - toolpikHeight - 50;
        } else if (top <= 0) {
            top = 10;
        } else {
            top = top;
        }

        if (left + toolpikWidth > winWidth) {
            // Shift the toolpik position to the left of the object
            // so it won't go out of width of current window width
            // and show up in the correct place 
            left = winWidth - toolpikWidth - 50;
        } else if (left <= 0) {
            left = 10;
        } else {
            left = left;
        }

        // Last: Show and set the position of the toolpik with animation
        $('#toolpik')
            .delay(600)
            .fadeIn(1000, movement);

            function movement() {
                $(this)
                .delay(600)
                .animate({
                    top:top-70,
                    left:left
                }, {
                    duration:2000,
                    easing:"easeOutBack"
                })
                .animate({top:top}, 400);
            };

    }, function() {
        // Put back the title attribute's value
        $(this).attr('title', $('#nurrohman', '#toolpik').html());
        // Remove the appended toolpik
        $('#nurrohman').parent().remove();
    });
 
});

View Demo Download Files

Usage

Install the toolpik-styler.css, jQuery library and jQuery Easing 1.3, then add the Toolpik scripts above the </head>:

<link rel='stylesheet' href='css/toolpik-styler.css'></link>
<script src='js/jquery-1.7.1.min.js'></script>
<script src='js/jquery-easing-1.3.pack.js'></script>
<script src='js/toolpik.js'></script>
<script>
    var ttImg    = "ikon1.png"; // REPLACE THIS ICON WITH YOUR OWN
    var ttTarget = "abbr, acronym";
</script>

Activation

Toolpik will find the value of title attribute and convert it to text or HTML element inside Toolpik. Simply wrap the text that you want with <abbr> tag and add the title attribute with the specified value and it will be a description in Toolpik:

Lorem ipsum <abbr title="I'm a Toolpik">I have a Toolpik</abbr>. Dolor sit amet-amet jabang bayi oek-oek.

However, if you are not familiar using <abbr> as a marker, you can use another tags such as <a>. To enable <a> as Toolpik, replace the variable var ttTarget = "abbr, acronym"; to var ttTarget = "a[title]";

You can also insert HTML code into the title attribute to get various content. For safety reasons, just make sure that if you use double quotation symbol on the title attribute, you must use single quotes in tag attributes inside. And for more secure, parse all the code < with &lt; and > with &gt;

Lorem ipsum <abbr title="<h2 class='single-quotes'>I'm a Header</h2>I'm a Toolpik">I have a Toolpik</abbr>. Dolor sit amet-amet jabang bayi oek-oek.

Or...

Lorem ipsum <abbr title="&lt;h2 class='single-quotes'&gt;I'm a Header&lt;/h2&gt;I'm a Toolpik">I have a Toolpik</abbr>. Dolor sit amet-amet jabang bayi oek-oek.

  

Berlangganan

Berlangganan: Dapatkan berbagai materi dan informasi seputar dunia pemrograman yang tidak akan pernah bisa kalian dapatkan di buku dan di bangku kuliah.

 9 Komentar

Memuat…

Tautan (link) hidup dalam komentar akan terhapus secara otomatis.
Untuk menyisipkan tautan dengan aman, gunakan kode [url=http://example.com]Teks Tautan[/url]
Untuk menyisipkan kode, gunakan tag <i rel="code">KODE ANDA</i>
Untuk menyisipkan gambar, gunakan kode [img]URL GAMBAR[/img]
Untuk menyisipkan judul, gunakan tag <b rel="h4">JUDUL ANDA DI SINI…</b>
Untuk menciptakan efek tebal gunakan tag <strong>TEKS ANDA DI SINI…</strong>
Untuk menciptakan efek huruf miring gunakan tag <em>TEKS ANDA DI SINI…</em>

Khusus untuk membalas komentar disarankan menggunakan tombol balas di samping komentar terkait dibandingkan menggunakan formulir komentar di bawah agar komunikasi lebih terstruktur. Karena mungkin, apa yang Anda tanyakan/katakan saat ini akan sangat bermanfaat bagi pembaca lain.

:) :( ^_^ :D ;) :bd :'( :\ ;p B) :Q :Ozzz 7:( \o/ **p :* 0:) :a 7:O *fuck* x& x@ ~x( *yaya* =p*

Nama Saya Taufik Nurrohman. Sebenarnya Saya tidak bisa disebut sebagai seorang yang profesional dalam bidang ini, karena Saya tidak menguasai bidang ini melalui pendidikan formal. Saya hanyalah seorang antusias. Dibandingkan sebagai seorang desainer atau pengembang, Saya lebih suka jika disebut sebagai seorang hobiis saja yang tertarik dengan dunia web.

Daftar Tautan

Saya tidak bisa hidup tanpa JavaScript! Aktifkan JavaScript?
Ke atas!