jquery.tips.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. (function ($) {
  2. function init(target) {
  3. var opt = $.data(target, "tips").options;
  4. var tips = $(".easyui-tips-hover");
  5. if (tips.length == 0) {
  6. tips = $("<div/>").css({
  7. "position": "absolute",
  8. "border-radius": "5px",
  9. "-webkit-border-radius": "5px",
  10. "-moz-border-radius": "5px",
  11. "padding": "5px",
  12. "background": "#f6f5ec",
  13. "display": "none",
  14. "border": "1px solid gray"
  15. }).hide().addClass("easyui-tips-hover").addClass(opt.cls);
  16. }
  17. opt.content = (opt.content || $(target).attr("tooltip"));
  18. tips.appendTo("body");
  19. $(target).css("color", opt.wrapColor);
  20. $(target).hover(function () {
  21. tips.html(opt.content);
  22. var offset = $(target).offset();
  23. //var outerWidth = tips.outerWidth();
  24. // if (outerWidth > 200) {
  25. // tips.width(200);
  26. // }
  27. var scrollTop = $(document).scrollTop();
  28. var tipsHeight = tips.outerHeight();
  29. var outerWidth = tips.outerWidth();
  30. var targetHeight = $(target).outerHeight();
  31. var top = offset.top - tipsHeight;
  32. var left = offset.left;
  33. if ((offset.top - scrollTop) < top || top < 100) {
  34. top = offset.top + targetHeight;
  35. }
  36. var bodyClienWidth = $("body")[0].clientWidth;
  37. if ((bodyClienWidth - left) < outerWidth) {
  38. left = bodyClienWidth - outerWidth;
  39. }
  40. tips.css({ top: top, left: left }).show();
  41. }, function () {
  42. tips.hide().width("auto");
  43. });
  44. }
  45. $.fn.tips = function (options, params) {
  46. if (typeof options === 'string') {
  47. return $(this).tips.methods[options].call(this, params);
  48. }
  49. options = options || {};
  50. return this.each(function () {
  51. var opt = $.data(this, "tips");
  52. if (opt) {
  53. $.extend(opt.options, options);
  54. } else {
  55. $.data(this, "tips", {
  56. options: $.extend({}, $.fn.tips.defaults, options)
  57. });
  58. init(this);
  59. }
  60. });
  61. };
  62. $.fn.tips.defaults = {
  63. cls: "",
  64. content: null,
  65. wrapColor: "blue"
  66. };
  67. if ($.parser) {
  68. $.parser.plugins.push('tips')
  69. }
  70. })(jQuery);