Posted by Steven Tue 20th Mar 2007 23:38 - Syntax is JavaScript - 20 views
Download | New Post | Modify | Hide line numbers
Description:
Simple tooltip function...



  1. /*
  2. *
  3. * Tooltip class - by Steven Bakhtiari 2007
  4. *
  5. * Requires Dom.class.js
  6. *
  7. * Simple tooltip that displays the title attribute of the tag.
  8. *
  9. */
  10.  
  11. function Tooltip() {
  12.  
  13.   var d = document;
  14.  
  15.   dom.addListener(window, "load", init);
  16.  
  17.   this.toolTip = false;
  18.  
  19.   function init() {
  20.     links = d.getElementsByTagName("a");
  21.     for (var i = 0; i < links.length; i++) {
  22.       links[i].titleText    = links[i].getAttribute("title");
  23.       links[i].removeAttribute("title");
  24.       links[i].onmouseover  = showTip;
  25.       links[i].onmousemove  = moveTip;
  26.       links[i].onmouseout   = removeTip;
  27.       links[i].onclick  = removeTip;
  28.     }
  29.   }
  30.  
  31.   function showTip(ev) {
  32.     try {
  33.       if ((this.titleText) && (this.toolTip = createTip(this.titleText))) {
  34.         var coords = dom.getMousePos(ev);
  35.         this.toolTip.style.top  = (coords['y']+20)+'px';
  36.         this.toolTip.style.left = (coords['x']+10)+'px';
  37.         d.getElementsByTagName('body')[0].appendChild(this.toolTip);
  38.       }
  39.     } catch(err) { } // catch any errors
  40.   }
  41.  
  42.   function moveTip(ev) {
  43.     try {
  44.       var coords = dom.getMousePos(ev);
  45.       this.toolTip.style.top  = (coords['y']+20)+'px';
  46.       this.toolTip.style.left = (coords['x']+10)+'px';
  47.     } catch(err) { } // catch any errors
  48.   }
  49.  
  50.   function removeTip(ev) {
  51.     try {
  52.       dom.removeNode(this.toolTip);
  53.     } catch(err) { } // catch any errors
  54.   }
  55.  
  56.   function createTip(text) {
  57.     tipElement = dom.newElement(d, "span");
  58.     tipElement.setAttribute("id", "tooltip");
  59.     tipElement.appendChild(dom.addText(d, text));
  60.     return tipElement;
  61.   }
  62.  
  63. }
  64.  

PermaLink to this entry https://pastebin.co.uk/12105
Posted by Steven Tue 20th Mar 2007 23:38 - Syntax is JavaScript - 20 views
Download | New Post | Modify | Hide line numbers

 

Comments: 0