Home » , , , » UPDATE :: JQuery Toolpik Revision 1

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();
});

});



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' type='text/css'></link>
<script type='text/javascript' src='js/jquery-1.7.1.min.js'></script>
<script type='text/javascript' src='js/jquery-easing-1.3.pack.js'></script>
<script type='text/javascript' src='js/toolpik.js'></script>
<script type='text/javascript'>
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.
Jika Anda menyukai Artikel di blog ini, Silahkan klik disini untuk berlangganan gratis via email, dengan begitu Anda akan mendapat kiriman artikel setiap ada artikel yang terbit di Creating Website

0 comments:

Post a Comment

 
Support : Creating Website | Johny Template | Mas Template
Copyright © 2011. Homtimpa - All Rights Reserved
Template Modify by Creating Website
Proudly powered by Blogger