ImageQtip.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. if(typeof ImageQtip == "undefined"){
  2. ImageQtip = {};
  3. }
  4. //预加载图片
  5. ImageQtip.drawImg = function(tag,src,options,callback){
  6. var image = new Image();
  7. image.src = src;
  8. image.alt="未能成功加载图片,可能已被删除...";
  9. if (image.complete) {
  10. ImageQtip.resizeImg(image,options);
  11. if(callback)
  12. callback(tag,image,options);
  13. else
  14. ImageQtip.initqTip(tag,image,options);
  15. return;
  16. }
  17. image.onload = function () {
  18. ImageQtip.resizeImg(image,options);
  19. if(callback)
  20. callback(tag,image,options);
  21. else
  22. ImageQtip.initqTip(tag,image,options);
  23. };
  24. };
  25. ImageQtip.getImage = function(src,options){
  26. var image = new Image();
  27. var imageObj = $(image);
  28. imageObj.addClass("hidden");
  29. image.src = src;
  30. image.alt="图片加载中...";
  31. if(typeof(options)!=undefined && options!=null && options!=""){
  32. var id = options.id;
  33. if( typeof(id)!=undefined && id!=null && id!="" ){
  34. image.id = id;
  35. }
  36. var title = options.title;
  37. if(typeof(title)!=undefined && title!=null && title!=""){
  38. image.alt = title;
  39. }
  40. var zoomUrl = options.zoomUrl;
  41. if( typeof(zoomUrl)!=undefined && zoomUrl!=null && zoomUrl!="" ){
  42. imageObj.attr("jqimg",zoomUrl);
  43. }
  44. }
  45. if (image.complete) {
  46. ImageQtip.resizeImg(image,options);
  47. imageObj.removeClass("hidden");
  48. return image;
  49. }
  50. image.onload = function () {
  51. ImageQtip.resizeImg(image,options);
  52. imageObj.removeClass("hidden");
  53. };
  54. if(imageObj.hasClass("hidden")){
  55. imageObj.removeClass("hidden");
  56. }
  57. return image;
  58. };
  59. //等比例调整图像尺寸
  60. ImageQtip.resizeImg = function(image,options){
  61. if (image.width > 0 && image.height > 0) {
  62. if(options.maxWidth>0){
  63. if(image.width>options.maxWidth){
  64. image.height=options.maxWidth*image.height/image.width;
  65. image.width=options.maxWidth;
  66. }
  67. }
  68. if(options.maxHeight>0){
  69. if(image.height>options.maxHeight){
  70. image.width=options.maxHeight*image.width/image.height;
  71. image.height=options.maxHeight;
  72. }
  73. }
  74. }
  75. };
  76. //初始化qTip2
  77. ImageQtip.initqTip = function (tag,image,options){
  78. var html=image.outerHTML;
  79. $(tag).qtip({
  80. content : {
  81. text : html,
  82. title : {
  83. text : '附件预览'
  84. }
  85. },
  86. position : {
  87. at : 'center',
  88. target : 'event',
  89. viewport : $(window)
  90. },
  91. show : {
  92. effect : function(offset) {
  93. $(this).slideDown(200);
  94. }
  95. },
  96. hide : {
  97. event : 'mouseleave',
  98. fixed : true
  99. },
  100. style : {
  101. classes : 'ui-tooltip-light ui-tooltip-shadow'
  102. }
  103. });
  104. };