weetips.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * weetips.js
  3. *
  4. * @category javascript
  5. * @package jquery
  6. * @author Jack <xiejinci@gmail.com>
  7. * @version
  8. */
  9. var weetips = function() {
  10. var self = this;
  11. $('<style id="weetipsstyle" type="text/css">' +
  12. '#weetips{' +
  13. 'border:1px solid #FFBA43;' +
  14. 'z-index:65535;' +
  15. 'position:absolute;' +
  16. 'background:#FDFFCE;' +
  17. 'color:#ff6600;' +
  18. 'padding:5px 10px;' +
  19. '}' +
  20. '#weetips-content{' +
  21. 'text-align:left;'+
  22. 'word-break:break-all;'+
  23. '}'+
  24. '</style>').appendTo('body');
  25. $('<div id="weetips"><div id="weetips-content"></div><div style="both:clear"></div></div>').appendTo('body').hide();
  26. this.init = function() {
  27. $(".weetips").each(function(){
  28. if ($(this).attr("title")) {
  29. $(this).attr("msg", $(this).attr("title")).removeAttr('title');
  30. }
  31. var msg = $(this).attr('msg');
  32. if ($.trim(msg) == '') {
  33. $(this).removeClass("weetips");
  34. }
  35. });
  36. $(".weetips").mouseover(function(event) {
  37. var msg = $(this).attr('msg');
  38. if ($.trim(msg) == '') {return false;}
  39. $('#weetips-content').html(msg);
  40. var width = parseInt("0"+$(this).attr('bwidth'), 10);
  41. if (width) {
  42. $('#weetips-content').css("width", width).css("overflow", "hidden").css("white-space", "normal");
  43. } else {
  44. $('#weetips-content').css("width", "").css("overflow", "").css("white-space", "nowrap");
  45. }
  46. self.setPosition(event);
  47. }).mousemove(function(event){
  48. self.setPosition(event);
  49. }).mouseout(function() {
  50. $('#weetips').hide();
  51. });
  52. }
  53. this.setPosition = function(event) {
  54. var left = self.getLeft(event);
  55. var top = self.getTop(event);
  56. $('#weetips').css({left:left, top:top}).show();
  57. }
  58. this.getLeft = function(event) {
  59. var docWidth = document.documentElement.clientWidth || document.body.clientWidth;
  60. var docLeft = document.documentElement.scrollLeft|| document.body.scrollLeft;
  61. var docRight = docLeft + docWidth;
  62. var left = docLeft+event.clientX;
  63. if (left+$('#weetips').width()+38 >= docRight) {
  64. left = Math.min(left - 10 - $('#weetips').width(), docRight-$('#weetips').width()-38);
  65. } else {
  66. left += 10;
  67. }
  68. return left;
  69. }
  70. this.getTop = function(event) {
  71. var docHeight = document.documentElement.clientHeight|| document.body.clientHeight;
  72. var docTop = document.documentElement.scrollTop|| document.body.scrollTop;
  73. var docBottom = docTop + docHeight;
  74. var top = docTop+event.clientY;
  75. if (top+$('#weetips').height()+10 >= docBottom) {
  76. top = top - 10 - $('#weetips').height();
  77. } else {
  78. top = top + 10;
  79. }
  80. return top;
  81. }
  82. this.init();
  83. };
  84. var $weetips = null;
  85. $(document).ready(function(){
  86. $weetips = new weetips();
  87. $weetips.init();
  88. });