Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.27 KB | None | 0 0
  1. (function(window, $) {
  2.  
  3. 'use strict';
  4.  
  5.  
  6. // Document -------------------------------------------------------------------
  7.  
  8. $(document).ready(function () {
  9.  
  10. console.log('## Document ready');
  11.  
  12. // VARS -------------------------------------------------------------------
  13. //
  14.  
  15. var elementPosition = $('.subnav').offset();
  16. var source = getParameterByName('src');
  17. var email = getParameterByName('emailAddress');
  18. var tel = getParameterByName('tel');
  19.  
  20.  
  21. // ACTIONS ----------------------------------------------------------------
  22. //
  23.  
  24. $(window).on('scroll', windowScrollHandler);
  25. $('.subnav .js-scroll').on('click', subnavClickHandler);
  26.  
  27. console.log(source);
  28. console.log(email);
  29. console.log(tel);
  30.  
  31. storeLocallySource(source);
  32. storeLocallyEmail(email);
  33. storeLocallyTel(tel);
  34. dynamicNavigation();
  35.  
  36.  
  37. });
  38. //
  39. // FUNCTIONS --------------------------------------------------------------
  40. //
  41.  
  42. function windowScrollHandler() {
  43. if($(window).scrollTop() > 331){
  44. $('.subnav').css('position','fixed').css('top','0');
  45. } else {
  46. $('.subnav').css('position','initial');
  47. }
  48. }
  49.  
  50. function subnavClickHandler(e) {
  51. e.preventDefault();
  52. $(document).off('scroll');
  53.  
  54.  
  55. $(this).parent().siblings().removeClass('active');
  56.  
  57.  
  58. $(this).parent().addClass('active');
  59.  
  60. var target = this.hash,
  61. menu = target;
  62. var $target = $(target);
  63. console.log(this.hash);
  64. $('html, body').stop().animate({
  65. 'scrollTop': $target.offset().top+2
  66. }, 500, 'swing', function () {
  67. window.location.hash = target;
  68.  
  69. $(document).on('scroll', onScroll);
  70. });
  71. }
  72.  
  73. function onScroll(event){
  74. var scrollPosition = $(document).scrollTop();
  75. $('.sub-nav .js-scroll').each(function () {
  76.  
  77. var currentLink = $(this);
  78. var refElement = $(currentLink.attr('href'));
  79. if (refElement.position().top <= scrollPosition && refElement.position().top + refElement.height() > scrollPosition) {
  80. $('.sub-navigation ul li').removeClass('active');
  81. currentLink.addClass('active');
  82. }
  83. else{
  84. currentLink.removeClass('active');
  85. }
  86. });
  87. }
  88.  
  89.  
  90.  
  91. // GET Parameter from URL
  92. function getParameterByName(name, url) {
  93. if (!url) {
  94. url = window.location.href;
  95. }
  96.  
  97. name = name.replace(/[\[\]]/g, '\\$&%');
  98.  
  99. var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
  100. results = regex.exec(url);
  101.  
  102. if (!results) return null;
  103. if (!results[2]) return null;
  104.  
  105. return decodeURIComponent(results[2].replace(/\+/g, ' '));
  106. }
  107.  
  108. // STORE src parameter in LocalStorage
  109. function storeLocallySource(source) {
  110. if (source !== null && source !== '') {
  111.  
  112. if(source === 'default' || source === 'efficacy' || source === 'safety' || source === 'practicalities' || source === 'all' ) {
  113. localStorage.setItem('source', source);
  114. }
  115. }
  116. }
  117. // STORE email parameter in LocalStorage
  118. function storeLocallyEmail(email) {
  119. if (email !== null && email !== '') {
  120. if (email === '{!User.Email}') {
  121. localStorage.setItem('email', 'janssen@jacbe.jnj.com');
  122. } else{
  123. localStorage.setItem('email', email);
  124. }
  125. }
  126.  
  127. }
  128.  
  129. // STORE tel parameter in LocalStorage
  130. function storeLocallyTel(tel) {
  131. if (tel !== null && tel !== '') {
  132. if (tel ==='{!User.MobilePhone}') {
  133. localStorage.setItem('tel', '0800 93 377');
  134. } else{
  135. localStorage.setItem('tel', tel);
  136. }
  137. }
  138. }
  139.  
  140.  
  141.  
  142. function dynamicNavigation() {
  143. var source = localStorage.getItem('source');
  144. var email = localStorage.getItem('email');
  145. var tel = localStorage.getItem('tel');
  146.  
  147. var $emailTarget = $('#dyn-email');
  148. var $telTarget = $('#dyn-tel');
  149.  
  150. var $emailTarget2 = $('#dyn-email-2');
  151. var $telTarget2 = $('#dyn-tel-2');
  152.  
  153. $emailTarget.text(email);
  154. $telTarget.text(tel);
  155.  
  156. $emailTarget2.text(email);
  157. $telTarget2.text(tel);
  158.  
  159. $emailTarget.attr('href',"mailto:" + email);
  160.  
  161. $emailTarget2.attr('href',"mailto:" + email);
  162.  
  163. switch (source) {
  164.  
  165. case 'efficacy':
  166. $('#navigation li').addClass('show');
  167. $('#navigation li:nth-child(5)').addClass('border-left');
  168. $('#navigation li:nth-child(3)').addClass('no-border-left');
  169. $('#navigation li:nth-child(4)').removeClass('show');
  170. $('#navigation li:last-child').addClass('show');
  171. $('#navigation ul').addClass('intro-nav');
  172.  
  173. break;
  174. case 'safety':
  175. $('#navigation li').addClass('show');
  176. $('#navigation li:last-child').removeClass('show');
  177. $('#navigation li:nth-child(3)').addClass('no-border-left');
  178. $('#navigation li:nth-child(5)').addClass('no-border-left');
  179. $('#navigation ul').addClass('intro-nav');
  180.  
  181. break;
  182. case 'practicalities':
  183. $('#navigation li').addClass('show');
  184. $('#navigation li:nth-child(5)').addClass('border-left');
  185. $('#navigation li:nth-child(2)').removeClass('no-border-left');
  186. $('#navigation li:nth-child(3)').removeClass('show');
  187. $('#navigation li:nth-child(4)').removeClass('show');
  188. $('#navigation li:last-child').removeClass('show');
  189. break;
  190. case 'all':
  191. $('#navigation li').addClass('show');
  192. $('#navigation li').removeClass('no-border-left');
  193. $('#navigation li').removeClass('border-left');
  194. $('#navigation ul').addClass('all-nav');
  195. $('#navigation li:last-child').removeClass('show');
  196. break;
  197. case 'default':
  198. $('#navigation li').addClass('show');
  199.  
  200. $('#navigation li:nth-child(4)').removeClass('show');
  201. $('#navigation li:nth-child(5)').removeClass('show');
  202. $('#navigation li:nth-child(6)').removeClass('show');
  203. $('#navigation li:last-child').removeClass('show');
  204. $('#navigation ul').addClass('intro-nav');
  205. break;
  206. default:
  207. console.log('default');
  208. $('#navigation li').addClass('show');
  209. $('#navigation li:nth-child(3)').removeClass('show');
  210. $('#navigation li:nth-child(4)').removeClass('show');
  211. $('#navigation li:nth-child(5)').removeClass('show');
  212. $('#navigation li:last-child').removeClass('show');
  213. $('#navigation ul').addClass('intro-nav');
  214.  
  215. break;
  216. }
  217. }
  218.  
  219.  
  220.  
  221. }(window, $));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement