tipso.min.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*!
  2. * tipso - A Lightweight Responsive jQuery Tooltip Plugin v1.0.1
  3. * Copyright (c) 2014 Bojan Petkovski
  4. * http://tipso.object505.com
  5. * Licensed under the MIT license
  6. * http://object505.mit-license.org/
  7. */
  8. ;(function($, window, document, undefined) {
  9. var pluginName = "tipso",
  10. defaults = {
  11. speed : 400,
  12. background : '#3498db',
  13. color : '#ffffff',
  14. position : 'top',
  15. width : 200,
  16. delay : 200,
  17. offsetX : 0,
  18. offsetY : 0,
  19. content : null,
  20. ajaxContentUrl : null,
  21. useTitle : true,
  22. onBeforeShow : null,
  23. onShow : null,
  24. onHide : null
  25. };
  26. function Plugin(element, options) {
  27. this.element = $(element);
  28. this.settings = $.extend({}, defaults, options);
  29. this._defaults = defaults;
  30. this._name = pluginName;
  31. this._title = this.element.attr('title');
  32. this.mode = 'hide';
  33. this.init();
  34. }
  35. $.extend(Plugin.prototype, {
  36. init: function() {
  37. var obj = this,
  38. $e = this.element;
  39. $e.addClass('tipso_style').removeAttr('title');
  40. if (isTouchSupported()) {
  41. $e.on('click' + '.' + pluginName, function(e) {
  42. obj.mode == 'hide' ? obj.show() : obj.hide();
  43. e.stopPropagation();
  44. });
  45. $(document).on('click', function() {
  46. if (obj.mode == 'show') {
  47. obj.hide();
  48. }
  49. });
  50. } else {
  51. $e.on('mouseover' + '.' + pluginName, function() {
  52. obj.show();
  53. });
  54. $e.on('mouseout' + '.' + pluginName, function() {
  55. obj.hide();
  56. });
  57. }
  58. },
  59. tooltip: function() {
  60. if (!this.tipso_bubble) {
  61. this.tipso_bubble = $(
  62. '<div class="tipso_bubble"><div class="tipso_content"></div><div class="tipso_arrow"></div></div>'
  63. );
  64. }
  65. return this.tipso_bubble;
  66. },
  67. show: function() {
  68. var tipso_bubble = this.tooltip(),
  69. obj = this,
  70. $win = $(window);
  71. if ($.isFunction(obj.settings.onBeforeShow)) {
  72. obj.settings.onBeforeShow($(this));
  73. }
  74. tipso_bubble.css({
  75. background: obj.settings.background,
  76. color: obj.settings.color,
  77. width: obj.settings.width
  78. }).hide();
  79. tipso_bubble.find('.tipso_content').html(obj.content());
  80. reposition(obj);
  81. $win.resize(function() {
  82. reposition(obj);
  83. });
  84. obj.timeout = window.setTimeout(function() {
  85. tipso_bubble.appendTo('body').stop(true, true).fadeIn(obj.settings
  86. .speed, function() {
  87. obj.mode = 'show';
  88. if ($.isFunction(obj.settings.onShow)) {
  89. obj.settings.onShow($(this));
  90. }
  91. });
  92. }, obj.settings.delay);
  93. },
  94. hide: function() {
  95. var obj = this,
  96. tipso_bubble = this.tooltip();
  97. window.clearTimeout(obj.timeout);
  98. obj.timeout = null;
  99. tipso_bubble.stop(true, true).fadeOut(obj.settings.speed,
  100. function() {
  101. $(this).remove();
  102. if ($.isFunction(obj.settings.onHide) && obj.mode == 'show') {
  103. obj.settings.onHide($(this));
  104. }
  105. obj.mode = 'hide';
  106. });
  107. },
  108. destroy: function() {
  109. var $e = this.element;
  110. $e.off('.' + pluginName);
  111. $e.removeData(pluginName);
  112. $e.removeClass('tipso_style').attr('title', this._title);
  113. },
  114. content: function() {
  115. var content,
  116. $e = this.element,
  117. obj = this,
  118. title = this._title;
  119. if (obj.settings.ajaxContentUrl) {
  120. content = $.ajax({
  121. type: "GET",
  122. url: obj.settings.ajaxContentUrl,
  123. async: false
  124. }).responseText;
  125. } else if (obj.settings.content) {
  126. content = obj.settings.content;
  127. } else {
  128. if (obj.settings.useTitle === true) {
  129. content = title;
  130. } else {
  131. content = $e.data('tipso');
  132. }
  133. }
  134. return content;
  135. },
  136. update: function(key, value) {
  137. var obj = this;
  138. if (value) {
  139. obj.settings[key] = value;
  140. } else {
  141. return obj.settings[key];
  142. }
  143. }
  144. });
  145. function isTouchSupported() {
  146. var msTouchEnabled = window.navigator.msMaxTouchPoints;
  147. var generalTouchEnabled = "ontouchstart" in document.createElement(
  148. "div");
  149. if (msTouchEnabled || generalTouchEnabled) {
  150. return true;
  151. }
  152. return false;
  153. }
  154. function realHeight(obj) {
  155. var clone = obj.clone();
  156. clone.css("visibility", "hidden");
  157. $('body').append(clone);
  158. var height = clone.outerHeight();
  159. clone.remove();
  160. return height;
  161. }
  162. function reposition(thisthat) {
  163. var tipso_bubble = thisthat.tooltip(),
  164. $e = thisthat.element,
  165. obj = thisthat,
  166. $win = $(window),
  167. arrow = 10,
  168. pos_top, pos_left, diff;
  169. switch (obj.settings.position) {
  170. case 'top':
  171. pos_left = $e.offset().left + ($e.outerWidth() / 2) - (tipso_bubble
  172. .outerWidth() / 2);
  173. pos_top = $e.offset().top - realHeight(tipso_bubble) - arrow;
  174. tipso_bubble.find('.tipso_arrow').css({
  175. marginLeft: -8
  176. });
  177. if (pos_top < $win.scrollTop()) {
  178. pos_top = $e.offset().top + $e.outerHeight() + arrow;
  179. tipso_bubble.find('.tipso_arrow').css({
  180. 'border-bottom-color': obj.settings.background,
  181. 'border-top-color': 'transparent'
  182. });
  183. tipso_bubble.removeClass('top bottom left right');
  184. tipso_bubble.addClass('bottom');
  185. } else {
  186. tipso_bubble.find('.tipso_arrow').css({
  187. 'border-top-color': obj.settings.background,
  188. 'border-bottom-color': 'transparent'
  189. });
  190. tipso_bubble.removeClass('top bottom left right');
  191. tipso_bubble.addClass('top');
  192. }
  193. break;
  194. case 'bottom':
  195. pos_left = $e.offset().left + ($e.outerWidth() / 2) - (tipso_bubble
  196. .outerWidth() / 2);
  197. pos_top = $e.offset().top + $e.outerHeight() + arrow;
  198. tipso_bubble.find('.tipso_arrow').css({
  199. marginLeft: -8
  200. });
  201. if (pos_top + realHeight(tipso_bubble) > $win.scrollTop() + $win.outerHeight()) {
  202. pos_top = $e.offset().top - realHeight(tipso_bubble) - arrow;
  203. tipso_bubble.find('.tipso_arrow').css({
  204. 'border-top-color': obj.settings.background,
  205. 'border-bottom-color': 'transparent'
  206. });
  207. tipso_bubble.removeClass('top bottom left right');
  208. tipso_bubble.addClass('top');
  209. } else {
  210. tipso_bubble.find('.tipso_arrow').css({
  211. 'border-bottom-color': obj.settings.background,
  212. 'border-top-color': 'transparent'
  213. });
  214. tipso_bubble.removeClass('top bottom left right');
  215. tipso_bubble.addClass(obj.settings.position);
  216. }
  217. break;
  218. case 'left':
  219. pos_left = $e.offset().left - tipso_bubble.outerWidth() - arrow;
  220. pos_top = $e.offset().top + ($e.outerHeight() / 2) - (realHeight(
  221. tipso_bubble) / 2);
  222. tipso_bubble.find('.tipso_arrow').css({
  223. marginTop: -8,
  224. marginLeft: ''
  225. });
  226. if (pos_left < $win.scrollLeft()) {
  227. pos_left = $e.offset().left + $e.outerWidth() + arrow;
  228. tipso_bubble.find('.tipso_arrow').css({
  229. 'border-right-color': obj.settings.background,
  230. 'border-left-color': 'transparent',
  231. 'border-top-color': 'transparent',
  232. 'border-bottom-color': 'transparent'
  233. });
  234. tipso_bubble.removeClass('top bottom left right');
  235. tipso_bubble.addClass('right');
  236. } else {
  237. tipso_bubble.find('.tipso_arrow').css({
  238. 'border-left-color': obj.settings.background,
  239. 'border-right-color': 'transparent',
  240. 'border-top-color': 'transparent',
  241. 'border-bottom-color': 'transparent'
  242. });
  243. tipso_bubble.removeClass('top bottom left right');
  244. tipso_bubble.addClass(obj.settings.position);
  245. }
  246. break;
  247. case 'right':
  248. pos_left = $e.offset().left + $e.outerWidth() + arrow;
  249. pos_top = $e.offset().top + ($e.outerHeight() / 2) - (realHeight(
  250. tipso_bubble) / 2);
  251. tipso_bubble.find('.tipso_arrow').css({
  252. marginTop: -8,
  253. marginLeft: ''
  254. });
  255. if (pos_left + arrow + obj.settings.width > $win.scrollLeft() +
  256. $win.outerWidth()) {
  257. pos_left = $e.offset().left - tipso_bubble.outerWidth() - arrow;
  258. tipso_bubble.find('.tipso_arrow').css({
  259. 'border-left-color': obj.settings.background,
  260. 'border-right-color': 'transparent',
  261. 'border-top-color': 'transparent',
  262. 'border-bottom-color': 'transparent'
  263. });
  264. tipso_bubble.removeClass('top bottom left right');
  265. tipso_bubble.addClass('left');
  266. } else {
  267. tipso_bubble.find('.tipso_arrow').css({
  268. 'border-right-color': obj.settings.background,
  269. 'border-left-color': 'transparent',
  270. 'border-top-color': 'transparent',
  271. 'border-bottom-color': 'transparent'
  272. });
  273. tipso_bubble.removeClass('top bottom left right');
  274. tipso_bubble.addClass(obj.settings.position);
  275. }
  276. break;
  277. }
  278. if (pos_left < $win.scrollLeft() && (obj.settings.position == 'bottom' ||
  279. obj.settings.position == 'top')) {
  280. tipso_bubble.find('.tipso_arrow').css({
  281. marginLeft: pos_left - 8
  282. });
  283. pos_left = 0;
  284. }
  285. if (pos_left + obj.settings.width > $win.outerWidth() && (obj.settings.position ==
  286. 'bottom' || obj.settings.position == 'top')) {
  287. diff = $win.outerWidth() - (pos_left + obj.settings.width);
  288. tipso_bubble.find('.tipso_arrow').css({
  289. marginLeft: -diff - 8,
  290. marginTop: ''
  291. });
  292. pos_left = pos_left + diff;
  293. }
  294. if (pos_left < $win.scrollLeft() && (obj.settings.position == 'left' ||
  295. obj.settings.position == 'right')) {
  296. pos_left = $e.offset().left + ($e.outerWidth() / 2) - (tipso_bubble.outerWidth() /
  297. 2);
  298. tipso_bubble.find('.tipso_arrow').css({
  299. marginLeft: -8,
  300. marginTop: ''
  301. });
  302. pos_top = $e.offset().top - realHeight(tipso_bubble) - arrow;
  303. if (pos_top < $win.scrollTop()) {
  304. pos_top = $e.offset().top + $e.outerHeight() + arrow;
  305. tipso_bubble.find('.tipso_arrow').css({
  306. 'border-bottom-color': obj.settings.background,
  307. 'border-top-color': 'transparent',
  308. 'border-left-color': 'transparent',
  309. 'border-right-color': 'transparent'
  310. });
  311. tipso_bubble.removeClass('top bottom left right');
  312. tipso_bubble.addClass('bottom');
  313. } else {
  314. tipso_bubble.find('.tipso_arrow').css({
  315. 'border-top-color': obj.settings.background,
  316. 'border-bottom-color': 'transparent',
  317. 'border-left-color': 'transparent',
  318. 'border-right-color': 'transparent'
  319. });
  320. tipso_bubble.removeClass('top bottom left right');
  321. tipso_bubble.addClass('top');
  322. }
  323. if (pos_left + obj.settings.width > $win.outerWidth()) {
  324. diff = $win.outerWidth() - (pos_left + obj.settings.width);
  325. tipso_bubble.find('.tipso_arrow').css({
  326. marginLeft: -diff - 8,
  327. marginTop: ''
  328. });
  329. pos_left = pos_left + diff;
  330. }
  331. if (pos_left < $win.scrollLeft()) {
  332. tipso_bubble.find('.tipso_arrow').css({
  333. marginLeft: pos_left - 8
  334. });
  335. pos_left = 0;
  336. }
  337. }
  338. if (pos_left + obj.settings.width > $win.outerWidth() && (obj.settings.position ==
  339. 'left' || obj.settings.position == 'right')) {
  340. pos_left = $e.offset().left + ($e.outerWidth() / 2) - (tipso_bubble.outerWidth() /
  341. 2);
  342. tipso_bubble.find('.tipso_arrow').css({
  343. marginLeft: -8,
  344. marginTop: ''
  345. });
  346. pos_top = $e.offset().top - realHeight(tipso_bubble) - arrow;
  347. if (pos_top < $win.scrollTop()) {
  348. pos_top = $e.offset().top + $e.outerHeight() + arrow;
  349. tipso_bubble.find('.tipso_arrow').css({
  350. 'border-bottom-color': obj.settings.background,
  351. 'border-top-color': 'transparent',
  352. 'border-left-color': 'transparent',
  353. 'border-right-color': 'transparent'
  354. });
  355. tipso_bubble.removeClass('top bottom left right');
  356. tipso_bubble.addClass('bottom');
  357. } else {
  358. tipso_bubble.find('.tipso_arrow').css({
  359. 'border-top-color': obj.settings.background,
  360. 'border-bottom-color': 'transparent',
  361. 'border-left-color': 'transparent',
  362. 'border-right-color': 'transparent'
  363. });
  364. tipso_bubble.removeClass('top bottom left right');
  365. tipso_bubble.addClass('top');
  366. }
  367. if (pos_left + obj.settings.width > $win.outerWidth()) {
  368. diff = $win.outerWidth() - (pos_left + obj.settings.width);
  369. tipso_bubble.find('.tipso_arrow').css({
  370. marginLeft: -diff - 8,
  371. marginTop: ''
  372. });
  373. pos_left = pos_left + diff;
  374. }
  375. if (pos_left < $win.scrollLeft()) {
  376. tipso_bubble.find('.tipso_arrow').css({
  377. marginLeft: pos_left - 8
  378. });
  379. pos_left = 0;
  380. }
  381. }
  382. tipso_bubble.css({
  383. left: pos_left + obj.settings.offsetX,
  384. top: pos_top + obj.settings.offsetY
  385. });
  386. }
  387. $[pluginName] = $.fn[pluginName] = function(options) {
  388. var args = arguments;
  389. if (options === undefined || typeof options === 'object') {
  390. if (!(this instanceof $)) {
  391. $.extend(defaults, options);
  392. }
  393. return this.each(function() {
  394. if (!$.data(this, 'plugin_' + pluginName)) {
  395. $.data(this, 'plugin_' + pluginName, new Plugin(this, options));
  396. }
  397. });
  398. } else if (typeof options === 'string' && options[0] !== '_' && options !==
  399. 'init') {
  400. var returns;
  401. this.each(function() {
  402. var instance = $.data(this, 'plugin_' + pluginName);
  403. if (!instance) {
  404. instance = $.data(this, 'plugin_' + pluginName, new Plugin(
  405. this, options));
  406. }
  407. if (instance instanceof Plugin && typeof instance[options] ===
  408. 'function') {
  409. returns = instance[options].apply(instance, Array.prototype.slice
  410. .call(args, 1));
  411. }
  412. if (options === 'destroy') {
  413. $.data(this, 'plugin_' + pluginName, null);
  414. }
  415. });
  416. return returns !== undefined ? returns : this;
  417. }
  418. };
  419. })(jQuery, window, document);