Monday, January 23, 2012

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.

Labels: , ,

9 Comments:

At Monday, January 23, 2012 at 2:40:00 PM GMT+7, Blogger Sinto said...

Bang - Bang Harusnya Make bahasa indonesia nih biar aku pelajari lebih detail hihihihi tapi Udah terlanjur :D

 
At Sunday, April 29, 2012 at 1:03:00 PM GMT+7, Blogger Gara Pratama said...

mas kalao caranya buat ngasih... tooltip dengan gambar tuh gimana ya?
kalao pas ngarahin kursor ke link akan keluar gambar...

tolong ya... ^_^

 
At Sunday, April 29, 2012 at 3:41:00 PM GMT+7, Blogger Taufik Nurrohman said...

@Gara Pratama [gararpas] Pakai ini saja sebenarnya juga sudah bisa, tinggal dimodifikasi sedikit pada markupnya di bagian ini:

$('#nurrohman', '#toolpik').html('<img src="' + $(this).attr('title') + '" \/>');

dan ini:

$(this).attr('title', $('#nurrohman', '#toolpik').find('img').attr('src'));

Demo: http://jsfiddle.net/tovic/GwGkW/

Kapan-kapan Saya buat versi yang lebih sederhana. Sekarang lagi ingin istirahat dulu hehe... :Ozz

 
At Friday, May 4, 2012 at 5:57:00 AM GMT+7, Blogger Unknown said...

Mas tolong ajarin dong ! gimana caranya buat demo page dan apa templatenya

 
At Friday, May 4, 2012 at 9:56:00 AM GMT+7, Blogger Taufik Nurrohman said...

@Agust Nurfa Pakai template minima yang sudah dibuang sidebarnya. Sisakan posting saja. Kalau mau menggunakan versi yang lebih sederhana, biasanya template-template bertema Blogazine bisa digunakan. Tapi cari yang paling sederhana, yang tidak punya sidebar dan memiliki lebar maksimal.

 
At Wednesday, June 27, 2012 at 8:38:00 PM GMT+7, Blogger Unknown said...

Aduh pusing mas,, ~x(

Apa harus naruh kode cssnya lewat hosting ya???
aku tadi coba naruh kodenya ga lewat hosting mas,,

kode di bawah ini saya taruh di atas kode </head>
<script src='http://reader-download.googlecode.com/files/jquery.easing.1.3.js' type='text/javascript'/>
<script type='text/javascript'>
//<![CDATA[
$(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();
});

});
//]]>
</script>
<script type='text/javascript'>
var ttImg = &quot;http://2.bp.blogspot.com/-adzEOrQwrZI/TulXSIRL_II/AAAAAAAABn0/8HMRpiW6F-s/s1600/ikon1.png&quot;; // REPLACE THIS ICON WITH YOUR OWN
var ttTarget = &quot;abbr, acronym&quot;;
</script>


terus kode CSS yang aku download taruh di atas ]]></b:skin>

hasilnya nihil,, apa aku ga bisa ya mas??? :(

Mohon bantuannya 7:(

 
At Sunday, July 8, 2012 at 1:37:00 PM GMT+7, Blogger Taufik Nurrohman said...

@trii waluyo Sudah betul kok. Yang penting taruh JQuery Easing di bawah JQuery, bukan di atasnya (Siapa tahu log-nya tampil undefined). Tooltip diambil dari atribut title dalam elemen <abbr>

 
At Friday, October 11, 2013 at 7:53:00 PM GMT+7, Blogger John said...

mas gimana Caranya membuat tooltip agar jika gambar disorot akan muncul data snippet, seperti Widget recent post dengan thumbnail dan tooltip, tapi buat halaman utama..

 
At Saturday, October 12, 2013 at 10:33:00 AM GMT+7, Blogger Lalu said...

Lihat penerapannya di www[.]yujikop[.]com

 

Post a Comment

<< Home