Javascript提示文本HelpTip.js源代码

来源:互联网 发布:苏34 知乎 编辑:程序博客网 时间:2024/04/28 14:47

 示例:

Help

           

代码:

HelpTip.js

 

  1. /*----------------------------------------------------------------------------/
  2. |                               Help Tip 1.12                                 |
  3. |-----------------------------------------------------------------------------|
  4. |                         Created by Erik Arvidsson                           |
  5. |                  (http://webfx.eae.net/contact.html#erik)                   |
  6. |                      For WebFX (http://webfx.eae.net/)                      |
  7. |-----------------------------------------------------------------------------|
  8. |           A tool tip like script that can be used for context help          |
  9. |-----------------------------------------------------------------------------|
  10. |                  Copyright (c) 1999 - 2002 Erik Arvidsson                   |
  11. |-----------------------------------------------------------------------------|
  12. | This software is provided "as is", without warranty of any kind, express or |
  13. | implied, including  but not limited  to the warranties of  merchantability, |
  14. | fitness for a particular purpose and noninfringement. In no event shall the |
  15. | authors or  copyright  holders be  liable for any claim,  damages or  other |
  16. | liability, whether  in an  action of  contract, tort  or otherwise, arising |
  17. | from,  out of  or in  connection with  the software or  the  use  or  other |
  18. | dealings in the software.                                                   |
  19. | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
  20. | This  software is  available under the  three different licenses  mentioned |
  21. | below.  To use this software you must chose, and qualify, for one of those. |
  22. | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
  23. | The WebFX Non-Commercial License          http://webfx.eae.net/license.html |
  24. | Permits  anyone the right to use the  software in a  non-commercial context |
  25. | free of charge.                                                             |
  26. | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
  27. | The WebFX Commercial license           http://webfx.eae.net/commercial.html |
  28. | Permits the  license holder the right to use  the software in a  commercial |
  29. | context. Such license must be specifically obtained, however it's valid for |
  30. | any number of  implementations of the licensed software.                    |
  31. | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
  32. | GPL - The GNU General Public License    http://www.gnu.org/licenses/gpl.txt |
  33. | Permits anyone the right to use and modify the software without limitations |
  34. | as long as proper  credits are given  and the original  and modified source |
  35. | code are included. Requires  that the final product, software derivate from |
  36. | the original  source or any  software  utilizing a GPL  component, such  as |
  37. | this, is also licensed under the GPL license.                               |
  38. |-----------------------------------------------------------------------------|
  39. | 2002-09-27 |                                                                |
  40. | 2001-11-25 | Added a resize to the tooltip if the document width is too     |
  41. |            | small.                                                         |
  42. | 2002-05-19 | IE50 did not recognise the JS keyword undefined so the test    |
  43. |            | for scroll support was updated to be IE50 friendly.            |
  44. | 2002-07-06 | Added flag to hide selects for IE                              |
  45. | 2002-10-04 | (1.1) Restructured and made code more IE garbage collector     |
  46. |            | friendly. This solved the most nasty memory leaks. Also added  |
  47. |            | support for hiding the tooltip if ESC is pressed.              |
  48. | 2002-10-18 | Fixed verrical position in case of scrolled document.          |
  49. | 2002-12-02 | Mozilla bug workaround related to mousedown and move.          |
  50. |-----------------------------------------------------------------------------|
  51. | Dependencies: helptip.css (To set up the CSS of the help-tooltip class)     |
  52. |-----------------------------------------------------------------------------|
  53. | Usage:                                                                      |
  54. |                                                                             |
  55. |   <script type="text/javascript" src="helptip.js">< /script>                |
  56. |   <link type="text/css" rel="StyleSheet" href="helptip.css" />              |
  57. |                                                                             |
  58. |   <a class="helpLink" href="?" onclick="showHelp(event, 'String to show');  |
  59. |      return false">Help</a>                                                 |
  60. |-----------------------------------------------------------------------------|
  61. | Created 2001-09-27 | All changes are in the log above. | Updated 2002-12-02 |
  62. /----------------------------------------------------------------------------*/
  63. function showHelpTip(e, sHtml, bHideSelects) {
  64.     
  65.     // find anchor element
  66.     var el = e.target || e.srcElement;
  67.     
  68.     while (el.tagName != "A")
  69.         el = el.parentNode;
  70.     // is there already a tooltip? If so, remove it
  71.     if (el._helpTip) {
  72.         helpTipHandler.hideHelpTip(el);
  73.     }
  74.     helpTipHandler.hideSelects = Boolean(bHideSelects);
  75.     // create element and insert last into the body
  76.     helpTipHandler.createHelpTip(el, sHtml);
  77.     
  78.     // position tooltip
  79.     helpTipHandler.positionToolTip(e);
  80.     // add a listener to the blur event.
  81.     // When blurred remove tooltip and restore anchor
  82.     el.onblur = helpTipHandler.anchorBlur;
  83.     el.onkeydown = helpTipHandler.anchorKeyDown;
  84. }
  85. var helpTipHandler = {
  86.     hideSelects:    false,
  87.     
  88.     helpTip:        null,
  89.     
  90.     showSelects:    function (bVisible) {
  91.         if (!this.hideSelects) return;
  92.         // only IE actually do something in here
  93.         var selects = [];
  94.         if (document.all)
  95.             selects = document.all.tags("SELECT");
  96.         var l = selects.length;
  97.         for (var i = 0; i < l; i++)
  98.             selects[i].runtimeStyle.visibility = bVisible ? "" : "hidden";  
  99.     },
  100.     
  101.     create: function () {
  102.         var d = document.createElement("DIV");
  103.         d.className = "help-tooltip";
  104.         d.onmousedown = this.helpTipMouseDown;
  105.         d.onmouseup = this.helpTipMouseUp;
  106.         document.body.appendChild(d);       
  107.         this.helpTip = d;
  108.     },
  109.     
  110.     createHelpTip:  function (el, sHtml) {
  111.         if (this.helpTip == null) {
  112.             this.create();
  113.         }
  114.         var d = this.helpTip;
  115.         d.innerHTML = sHtml;
  116.         d._boundAnchor = el;
  117.         el._helpTip = d;
  118.         return d;
  119.     },
  120.     
  121.     // Allow clicks on A elements inside tooltip
  122.     helpTipMouseDown:   function (e) {
  123.         var d = this;
  124.         var el = d._boundAnchor;
  125.         if (!e) e = event;
  126.         var t = e.target || e.srcElement;
  127.         while (t.tagName != "A" && t != d)
  128.             t = t.parentNode;
  129.         if (t == d) return;
  130.         
  131.         el._onblur = el.onblur;
  132.         el.onblur = null;
  133.     },
  134.     
  135.     helpTipMouseUp: function () {
  136.         var d = this;
  137.         var el = d._boundAnchor;
  138.         el.onblur = el._onblur;
  139.         el._onblur = null;
  140.         el.focus();
  141.     },  
  142.     
  143.     anchorBlur: function (e) {
  144.         var el = this;
  145.         helpTipHandler.hideHelpTip(el);
  146.     },
  147.     
  148.     anchorKeyDown:  function (e) {
  149.         if (!e) e = window.event
  150.         if (e.keyCode == 27) {  // ESC
  151.             helpTipHandler.hideHelpTip(this);
  152.         }
  153.     },
  154.     
  155.     removeHelpTip:  function (d) {
  156.         d._boundAnchor = null;
  157.         d.style.filter = "none";
  158.         d.innerHTML = "";
  159.         d.onmousedown = null;
  160.         d.onmouseup = null;
  161.         d.parentNode.removeChild(d);
  162.         //d.style.display = "none";
  163.     },
  164.     
  165.     hideHelpTip:    function (el) {
  166.         var d = el._helpTip;
  167.         /*  Mozilla (1.2+) starts a selection session when moved
  168.             and this destroys the mouse events until reloaded
  169.         d.style.top = -el.offsetHeight - 100 + "px";
  170.         */      
  171.         
  172.         d.style.visibility = "hidden";
  173.         //d._boundAnchor = null;
  174.         el.onblur = null;
  175.         el._onblur = null;
  176.         el._helpTip = null;
  177.         el.onkeydown = null;
  178.         
  179.         this.showSelects(true);
  180.     },
  181.     
  182.     positionToolTip:    function (e) {
  183.         this.showSelects(false);        
  184.         var scroll = this.getScroll();
  185.         var d = this.helpTip;
  186.         
  187.         // width
  188.         if (d.offsetWidth >= scroll.width)
  189.             d.style.width = scroll.width - 10 + "px";
  190.         else
  191.             d.style.width = "";
  192.         
  193.         // left
  194.         if (e.clientX > scroll.width - d.offsetWidth)
  195.             d.style.left = scroll.width - d.offsetWidth + scroll.left + "px";
  196.         else
  197.             d.style.left = e.clientX - 2 + scroll.left + "px";
  198.         
  199.         // top
  200.         if (e.clientY + d.offsetHeight + 18 < scroll.height)
  201.             d.style.top = e.clientY + 18 + scroll.top + "px";
  202.         else if (e.clientY - d.offsetHeight > 0)
  203.             d.style.top = e.clientY + scroll.top - d.offsetHeight + "px";
  204.         else
  205.             d.style.top = scroll.top + 5 + "px";
  206.             
  207.         d.style.visibility = "visible";
  208.     },
  209.     
  210.     // returns the scroll left and top for the browser viewport.
  211.     getScroll:  function () {
  212.         if (document.all && typeof document.body.scrollTop != "undefined") {    // IE model
  213.             var ieBox = document.compatMode != "CSS1Compat";
  214.             var cont = ieBox ? document.body : document.documentElement;
  215.             return {
  216.                 left:   cont.scrollLeft,
  217.                 top:    cont.scrollTop,
  218.                 width:  cont.clientWidth,
  219.                 height: cont.clientHeight
  220.             };
  221.         }
  222.         else {
  223.             return {
  224.                 left:   window.pageXOffset,
  225.                 top:    window.pageYOffset,
  226.                 width:  window.innerWidth,
  227.                 height: window.innerHeight
  228.             };
  229.         }
  230.         
  231.     }
  232. };

HelpTip.css

  1. <style>
  2. .help-tooltip {
  3.     position:   absolute;
  4.     width:      385px;
  5.     border:     1px Solid WindowFrame;
  6.     background: Infobackground;
  7.     color:      InfoText;
  8.     font:       StatusBar;
  9.     font:       Status-Bar;
  10.     padding:    3px;
  11.     filter:     progid:DXImageTransform.Microsoft.Shadow(color="#777777", Direction=135, Strength=3);
  12.     z-index:    10000;
  13. }
  14. </style>
原创粉丝点击