tooltipbox.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. var _TipIDCounter = 0;
  2. function Tip(ele, msg, position, autoClose, withObject) {
  3. this.ElementID = ele;
  4. this.withObject = withObject;
  5. if (!position) {
  6. position = "top";
  7. }
  8. this.Position = position;
  9. this.Element = null;
  10. if(typeof ele == "object"){
  11. this.Element =ele.get(0);
  12. }
  13. else{
  14. if(ele.substr(0,1)!="#" && ele.substr(0,1)!="." && ele.substr(0,1)!=":"){
  15. this.Element = $("#" + this.ElementID).get(0);
  16. }
  17. else{
  18. this.Element= $(this.ElementID).get(0);
  19. }
  20. }
  21. this.Message = msg;
  22. if (!autoClose) {
  23. autoClose = true;
  24. }
  25. this.AutoClose = autoClose;
  26. this.X = 0;
  27. this.Y = 0;
  28. }
  29. Tip.AutoCloseTips = [];
  30. Tip.Tips = [];
  31. Tip.prototype.init = function() {
  32. }
  33. Tip.prototype.show = function() {
  34. var el = null;
  35. if(typeof this.ElementID=="object"){
  36. el = this.ElementID;
  37. }
  38. else{
  39. if(this.ElementID.substr(0,1)!="#" && this.ElementID.substr(0,1)!="." && this.ElementID.substr(0,1)!=":"){
  40. el = $("#" + this.ElementID);
  41. }
  42. else{
  43. el = $(this.ElementID);
  44. }
  45. }
  46. if (this.withObject != null && this.withObject != undefined) {
  47. if(typeof this.withObject=="object"){
  48. el = this.withObject;
  49. }
  50. else{
  51. if(this.withObject.substr(0,1)!="#" && this.withObject.substr(0,1)!="." && this.withObject.substr(0,1)!=":"){
  52. el = $("#" + this.withObject);
  53. }
  54. else{
  55. el = $(this.withObject);
  56. }
  57. }
  58. }
  59. if( el.length<=0){
  60. alert( this.ElementID + " is null" );
  61. return;
  62. }
  63. var arr = [];
  64. var divid = "tooltipbox_" + _TipIDCounter++;
  65. arr.push("<div id='" + divid + "' class='tooltipbox'><span>");
  66. arr.push(this.Message);
  67. arr.push("</span></div>");
  68. this.Html = arr.join('');
  69. $(this.Html).appendTo($(document.body));
  70. var dl = $("#" + divid);
  71. var p = el.offset();
  72. var fixLeft = parseInt(el.css("margin-left")) || 0;
  73. var fixTop = parseInt(el.css("margin-top")) || 0;
  74. var fixTopPadding = parseInt(el.css("padding-top")) || 0;
  75. var fixLeftPadding = parseInt(el.css("padding-left")) || 0;
  76. var fixRightPadding = parseInt(el.css("padding-right")) || 0;
  77. var x = 0;
  78. var y = 0;
  79. // $("#" + this.ElementID).css("background","#f9fcde");
  80. switch (this.Position) {
  81. case "left":
  82. x = p.left +el.width();
  83. y = p.top + (el.height() - dl.height()) / 2 + 3;
  84. break;
  85. case "right":
  86. x = p.left + el.width() + fixLeftPadding+fixLeft;
  87. y = p.top + (el.height() - dl.height()) / 2;
  88. break;
  89. case "top":
  90. x = p.left;
  91. y = p.top - el.height() - fixTopPadding - fixTop - 5;
  92. break;
  93. case "bottom":
  94. x = p.left;
  95. y = p.top + el.height() + fixTopPadding + fixTop + 5;
  96. y += 10;
  97. break;
  98. }
  99. dl.css({
  100. left: x+"px" ,
  101. top: y+"px"
  102. });
  103. this.Div =dl;
  104. if (this.AutoClose) {
  105. Tip.AutoCloseTips.push(this);
  106. }
  107. Tip.Tips.push(this);
  108. }
  109. Tip.prototype.close = function() {
  110. this.Div.remove();
  111. }
  112. Tip.show = function(ele, msg, pos, autoClose, withObject) {
  113. var tip = new Tip(ele, msg, pos, autoClose, withObject);
  114. tip.show();
  115. if (!tip.AutoClose) {
  116. if (!tip.Element._Tips) {
  117. tip.Element._Tips = [];
  118. }
  119. tip.Element._Tips.push(tip);
  120. }
  121. return tip;
  122. }
  123. Tip.focus = function(ele, msg, pos, autoClose, withObject) {
  124. if(typeof ele=="object"){
  125. ele.focus();
  126. }
  127. else{
  128. if(ele.substr(0,1)!="#" && ele.substr(0,1)!="." && ele.substr(0,1)!=":"){
  129. $("#" + ele).focus();
  130. }
  131. else{
  132. $(ele).focus();
  133. }
  134. }
  135. return Tip.show(ele, msg, pos, autoClose, withObject);
  136. }
  137. Tip.select = function(ele, msg, pos, autoClose, withObject) {
  138. if(typeof ele=="object"){
  139. ele.focus();
  140. ele.select();
  141. }
  142. else if(ele.substr(0,1)!="#" && ele.substr(0,1)!="." && ele.substr(0,1)!=":"){
  143. $("#" + ele).focus();
  144. $("#" + ele).select();
  145. }
  146. else{
  147. $(ele).focus();
  148. $(ele).select();
  149. }
  150. return Tip.show(ele, msg, pos, autoClose, withObject);
  151. }
  152. Tip.getTipCount = function(ele) {
  153. ele = $(ele);
  154. if (!ele._Tips) {
  155. return 0;
  156. }
  157. return ele._Tips.length;
  158. }
  159. Tip.closeAll = function() {
  160. var arr = Tip.Tips;
  161. for (var i = arr.length; i > 0; i--) {
  162. arr[i - 1].close();
  163. arr.splice(i - 1, 1)
  164. }
  165. };
  166. Tip.close = function(ele) {//将与ele有关的Tip都关闭掉
  167. ele = document.getElementById(ele);
  168. if (ele._Tips) {
  169. for (var i = 0; i < ele._Tips.length; i++) {
  170. if (ele._Tips[i]) {
  171. ele._Tips[i].close();
  172. }
  173. }
  174. ele._Tips = [];
  175. }
  176. }
  177. $(document).mousedown(function() {
  178. var arr = Tip.AutoCloseTips;
  179. for (var i = arr.length; i > 0; i--) {
  180. // $("#" + arr[i-1].ElementID).css("background","#fff");
  181. arr[i - 1].close();
  182. arr.splice(i - 1, 1)
  183. }
  184. });
  185. $(document).keydown(function() {
  186. var arr = Tip.AutoCloseTips;
  187. for (var i = arr.length; i > 0; i--) {
  188. // $("#" + arr[i-1].ElementID).css("background","#fff");
  189. arr[i - 1].close();
  190. arr.splice(i - 1, 1)
  191. }
  192. });