CoolMints' ToolTips
Why is this code special? Because it is compact, cross-browser compatible, and lighting fast to implement and to change. Do you want a tooltip for an element? Just add an extra attribute.
AI
Resumo por IA: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.
Código fonte
<h3>What?</h3>
<p>This tooltips
do not function like the usual anoying boxes following your mouse pointer.
When you hover over an element, information is shown in single, customisable
spot on the page. Every element can have it's tooltip. It only takes
one function plus a single line. The script is compatible and tested
with Internet Explorer 6.0 and Netscape 7.1. Presumably, it will work
on some older versions too.</p>
<h3>How to implement?</h3>
<ol>
<li>Paste the script into the head section.<br>
<br>
<code><script language="javascript" type="text/javascript"><br>
function displaytip(){<br>
o=(o=window.event || arguments.callee.arguments[0]).srcElement
|| o.target;<br>
j=o.attributes["tiptext"];<br>
if(!j && o.parentNode.attributes) j=o.parentNode.attributes["tiptext"];<br>
document.getElementById("tooltips").innerHTML=(j?j.nodeValue:"");<br>
}<br>
window.onload = new Function("document.body.onmouseover=displaytip");<br>
</script><br>
</code></li>
<li>Make an element with id <code>tooltips</code>.<br>
</li>
<li>Add a <code>tiptext</code> attribute to the elements you want to
have a tooltip. <br>
</li>
</ol>
<h3>How does it work?</h3>
<p>If the mouse is moved, the <code>displaytip</code> function checks if
the underlaying element has a <code>tiptext</code> attribute. <em>(If
not, it tries using the <code>tiptext</code> attribute of the parent element. For
example: a paragraph containing a link.) </em>The value of this attribute
is set as <code>innerHTML</code> of the <code>tooltips</code> element.
If no <code>tiptext</code> has been found, the <code>innerHTML</code> is
emptied.</p>
<h3>Why this <em>unconventional</em> code writing style?</h3>
<p>To make the code as compact as possible, without having a messy code.
A brief explanation:</p>
<p><code>o=(o=window.event || arguments.callee.arguments[0]).srcElement
|| o.target;</code><br>
This duality is because Netscape does not have the <code>window.event</code> object.
I adapted this trick from http://www.javascriptkit.com/dhtmltutors/nsevents.shtml. First, the event object is stored in o. Then, the
object that has fired the event is stored, again in o. In IE, this is <code>srcElement</code>;
in Netscape, this is <code>target</code>.</p>
<p><code>j=o.attributes["tiptext"];<br>
if(!j) j=o.parentNode.attributes["tiptext"];</code><br>
The attribute <code>tiptext</code> is stored in <code>j</code>. If this
attribute doens't exist, the <code>tiptext</code> attribute of the parent
element is stored in <code>j</code>.</p>
<p><code>document.getElementById("tooltips").innerHTML=(j?j.nodeValue:"");</code><br>
The <code>innerHTML</code> of the <code>tooltips</code> element is set
to the value of the <code>tiptext</code> attribute, if present. Otherwise,
the <code>nodeValue</code> element is cleared.</p>
<p><code>window.onload = new Function("document.body.onmouseover=displaytip");</code><br>
This line assigns the <code>displaytip</code> function to the <code>onmouseover</code> event
of the body. The construction I used makes it possible to put the code
in the head part of your document, as I like to have all my scripts there.</p>
<p><strong>Have fun, and if you like it: vote for this code!</strong></p>
Upload
Comentários originais (3)
Recuperado do Wayback Machine