jquery.aSimpleTour.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /**
  2. * jQuery aSimpleTour
  3. *
  4. * @description Tour web
  5. * @author alvaro.veliz@gmail.com
  6. * @servedby perkins (http://p.erkins.com)
  7. *
  8. * Dependencies :
  9. * - jQuery scrollTo
  10. *
  11. * This idea is based on http://tympanus.net/codrops/2010/12/21/website-tour/ by Manoela Lic (https://twitter.com/#!/crnacura)
  12. */
  13. (function( $ ) {
  14. var settings = {
  15. data : [],
  16. autoStart : false,
  17. controlsPosition : 'TR',
  18. welcomeMessage : '<h2>Tour</h2><p>Welcome to the Tour Plugin</p>',
  19. buttons : {
  20. next : 'Next',
  21. prev : 'Previous',
  22. start : 'Start',
  23. end : 'End'
  24. },
  25. controlsColors : {
  26. background: 'rgba(8, 68, 142, 0.80)',
  27. color: '#fff'
  28. },
  29. tooltipColors : {
  30. background: 'rgba(0, 0, 0, 0.70)',
  31. color: '#fff'
  32. },
  33. endCallback: $.noop()
  34. };
  35. var options, step, steps;
  36. var ew, eh, el, et;
  37. var started = false;
  38. var direction = 'f';
  39. var $tooltip = $('<div>',{
  40. id : 'tourtip',
  41. className : 'tourtip',
  42. html : ''
  43. }).css({
  44. 'display' : 'none',
  45. 'padding' : '10px 20px',
  46. 'position' : 'absolute',
  47. 'font-family' : 'sans-serif',
  48. 'border-radius' : '5px',
  49. 'font-size' : '12px',
  50. 'box-sizing' : 'border-box'
  51. });
  52. var methods = {
  53. init : function( opts ) {
  54. if (started == false ) {
  55. started = true;
  56. options = $.extend(settings, opts);
  57. controls = '<div id="tourControls">\
  58. <div id="tourText">'+options.welcomeMessage+'</div>\
  59. <div id="tourButtons">\
  60. <button id="tourPrev" style="display:none">'+options.buttons.prev+'</button>\
  61. <button id="tourNext">'+options.buttons.start+'</button>\
  62. <a href="javascript:void(0);" id="tourEnd" style="display:block;">'+options.buttons.end+'</a>\
  63. </div>\
  64. </div>';
  65. $controlsCss = { 'display' : 'block', 'position': 'fixed', 'width' : '200px', 'padding' : '10px 20px', 'border-radius' : '10px', 'font-family' : 'sans-serif' };
  66. $controls = $(controls).css($controlsCss).css(options.controlsColors);
  67. $cpos = methods.getControlPosition(options.controlsPosition);
  68. $controls.css($cpos);
  69. $('body').append($controls);
  70. $tooltip.css(options.tooltipColors);
  71. step = -1;
  72. steps = options.data.length;
  73. $('body').prepend($tooltip);
  74. }
  75. },
  76. next : function() {
  77. direction = 'f';
  78. step++;
  79. if (step == steps) {
  80. options.endCallback();
  81. methods.destroy();
  82. }
  83. else {
  84. $tooltip.hide();
  85. stepData = options.data[step];
  86. if (step <= steps) {
  87. $('#tourPrev').show();
  88. $('#tourEnd').show();
  89. $('#tourNext').show().html(options.buttons.next);
  90. }
  91. methods.setTooltip(stepData);
  92. }
  93. },
  94. prev : function() {
  95. direction = 'b';
  96. $tooltip.hide();
  97. if (step < steps) {
  98. $('#tourNext').show().html(options.buttons.next);
  99. }
  100. if (step <= 0) {
  101. $('#tourPrev').hide();
  102. $('#tourEnd').hide();
  103. $('#tourNext').html(options.buttons.start);
  104. step--;
  105. }
  106. else {
  107. step--;
  108. stepData = options.data[step];
  109. methods.setTooltip(stepData);
  110. }
  111. },
  112. setTooltip : function(stepData) {
  113. $element = $(stepData.element);
  114. if (stepData.controlsPosition) {
  115. methods.setControlsPosition(stepData.controlsPosition);
  116. }
  117. if (stepData.tooltip) {
  118. $tooltip.html(stepData.tooltip);
  119. text = (typeof stepData.text != 'undefined') ? stepData.text : stepData.tooltip;
  120. $('#tourText').html(text);
  121. tooltipPos = (typeof stepData.position == 'undefined') ? 'BL' : stepData.position;
  122. $pos = methods.getTooltipPosition(tooltipPos, $element);
  123. $tooltip.css({ 'top': $pos.top+'px', 'left': $pos.left+'px' });
  124. $tooltip.show('fast');
  125. $.scrollTo($tooltip, 400, { offset : -100});
  126. }
  127. if (typeof stepData.tooltipCallback != 'undefined') {
  128. stepData.tooltipCallback();
  129. }
  130. if (typeof stepData.callback != 'undefined') {
  131. if (stepData.callback == 'click') {
  132. urlRegex = /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/;
  133. urlslugRegex = /^[a-z0-9-]+$/;
  134. if (urlRegex.test($element.attr('href')) || urlslugRegex.test($element.attr('href')) ) {
  135. location.href = $element.attr('href');
  136. }
  137. else {
  138. $element.trigger('click');
  139. }
  140. }
  141. else {
  142. if (typeof eval(stepData.callback) == 'function') {
  143. eval(stepData.callback);
  144. }
  145. }
  146. }
  147. if (stepData.next === true) {
  148. if (direction == 'f') {
  149. methods.next();
  150. }
  151. else {
  152. methods.prev();
  153. }
  154. }
  155. },
  156. setControlsPosition : function(pos) {
  157. chtml = $controls.html();
  158. $controls.remove();
  159. $controls = $(controls).html(chtml);
  160. $controls = $controls.css($controlsCss).css(options.controlsColors);
  161. position = methods.getControlPosition(pos);
  162. $controls.css(position);
  163. $('body').append($controls);
  164. },
  165. getTooltipPosition : function(pos, $e) {
  166. ew = $element.outerWidth();
  167. eh = $element.outerHeight();
  168. el = $element.offset().left;
  169. et = $element.offset().top;
  170. tw = $tooltip.width() + parseInt($tooltip.css('padding-left')) + parseInt($tooltip.css('padding-right'));
  171. th = $tooltip.height() + parseInt($tooltip.css('padding-top')) + + parseInt($tooltip.css('padding-bottom'));
  172. $('.tourArrow').remove();
  173. tbg = $tooltip.css('background-color');
  174. $upArrow = $('<div class="tourArrow"></div>').css({ 'position' : 'absolute', 'display' : 'block', 'width' : '0', 'height' : '0', 'border-left' : '5px solid transparent', 'border-right' : '5px solid transparent', 'border-bottom' : '5px solid '+tbg });
  175. $downArrow = $('<div class="tourArrow"></div>').css({ 'position' : 'absolute', 'display' : 'block', 'width' : '0', 'height' : '0', 'border-left' : '5px solid transparent', 'border-right' : '5px solid transparent', 'border-top' : '5px solid '+tbg });
  176. $rightArrow = $('<div class="tourArrow"></div>').css({ 'position' : 'absolute', 'display' : 'block', 'width' : '0', 'height' : '0', 'border-top' : '5px solid transparent', 'border-bottom' : '5px solid transparent', 'border-left' : '5px solid '+tbg });
  177. $leftArrow = $('<div class="tourArrow"></div>').css({ 'position' : 'absolute', 'display' : 'block', 'width' : '0', 'height' : '0', 'border-top' : '5px solid transparent', 'border-bottom' : '5px solid transparent', 'border-right' : '5px solid '+tbg });
  178. switch (pos) {
  179. case 'BL' :
  180. position = { 'left' : el, 'top' : et + eh + 10 };
  181. $upArrow.css({ top: '-5px', left: '48%' });
  182. $tooltip.prepend($upArrow);
  183. break;
  184. case 'BR' :
  185. position = { 'left' : el + ew - tw, 'top' : et + eh + 10 };
  186. $upArrow.css({ top: '-5px', left: '48%' });
  187. $tooltip.prepend($upArrow);
  188. break;
  189. case 'TL' :
  190. position = { 'left' : el, 'top' : (et - th) -10 };
  191. $downArrow.css({ top: th, left: '48%' });
  192. $tooltip.append($downArrow);
  193. break;
  194. case 'TR' :
  195. position = { 'left' : (el + ew) - tw, 'top' : et - th -10 };
  196. $downArrow.css({ top: th, left: '48%' });
  197. $tooltip.append($downArrow);
  198. break;
  199. case 'RT' :
  200. position = { 'left' : el + ew + 10, 'top' : et };
  201. $leftArrow.css({ left: '-5px' });
  202. $tooltip.prepend($leftArrow);
  203. break;
  204. case 'RB' :
  205. position = { 'left' : el + ew + 10, 'top' : et + eh - th };
  206. $leftArrow.css({ left: '-5px' });
  207. $tooltip.prepend($leftArrow);
  208. break;
  209. case 'LT' :
  210. position = { 'left' : (el - tw) - 10, 'top' : et };
  211. $rightArrow.css({ right: '-5px' });
  212. $tooltip.prepend($rightArrow);
  213. break;
  214. case 'LB' :
  215. position = { 'left' : (el - tw) - 10, 'top' : et + eh - th};
  216. $rightArrow.css({ right: '-5px' });
  217. $tooltip.prepend($rightArrow);
  218. break;
  219. case 'B' :
  220. position = { 'left' : el + ew/2 - tw/2, 'top' : (et + eh) + 10 };
  221. $upArrow.css({ top: '-5px', left: '48%' });
  222. $tooltip.prepend($upArrow);
  223. break;
  224. case 'L' :
  225. position = { 'left' : (el - tw) - 10, 'top' : et + eh/2 - th/2 };
  226. $rightArrow.css({ right: '-5px' });
  227. $tooltip.prepend($rightArrow);
  228. break;
  229. case 'T' :
  230. position = { 'left' : el + ew/2 - tw/2, 'top' : (et - th) - 10 };
  231. $downArrow.css({ top: th, left: '48%' });
  232. $tooltip.append($downArrow);
  233. break;
  234. case 'R' :
  235. position = { 'left' : (el + ew) + 10, 'top' : et + eh/2 - th/2 };
  236. $leftArrow.css({ left: '-5px' });
  237. $tooltip.prepend($leftArrow);
  238. break;
  239. }
  240. return position;
  241. },
  242. getControlPosition: function(pos) {
  243. switch (pos)
  244. {
  245. case 'TR':
  246. pos = { 'top' : '10px', 'right' : '10px' };
  247. break;
  248. case 'TL':
  249. pos = { 'top' : '10px', 'left' : '10px' };
  250. break;
  251. case 'BL':
  252. pos = { 'bottom' : '10px', 'left' : '10px' };
  253. break;
  254. case 'BR':
  255. pos = { 'bottom' : '10px', 'right' : '10px' };
  256. break;
  257. }
  258. return pos;
  259. },
  260. destroy : function() {
  261. $('#tourControls').remove();
  262. $('#tourtip').remove();
  263. $tooltip.css({ 'display' : 'none' }).html('');
  264. step = -1;
  265. started = false;
  266. }
  267. };
  268. $('#tourNext').live('click', function() {
  269. methods.next();
  270. });
  271. $('#tourPrev').live('click', function() {
  272. methods.prev();
  273. });
  274. $('#tourEnd').live('click', function() {
  275. options.endCallback();
  276. methods.destroy();
  277. })
  278. $.fn.aSimpleTour = function( method )
  279. {
  280. if ( methods[method] ) {
  281. return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
  282. }
  283. else if ( typeof method === 'object' || ! method ) {
  284. return methods.init.apply( this, arguments );
  285. }
  286. else {
  287. $.error( 'Method ' + method + ' does not exist on jQuery.aSimpleTour' );
  288. }
  289. };
  290. })(jQuery);
  291. // Direct Access
  292. $.aSimpleTour = function(opts) { $.fn.aSimpleTour(opts); }