Guest User

Untitled

a guest
Sep 24th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. $.throttle = jq_throttle = function( delay, no_trailing, callback, debounce_mode ) {
  2. // After wrapper has stopped being called, this timeout ensures that
  3. // `callback` is executed at the proper times in `throttle` and `end`
  4. // debounce modes.
  5. var timeout_id,
  6.  
  7. // Keep track of the last time `callback` was executed.
  8. last_exec = 0;
  9.  
  10. // `no_trailing` defaults to falsy.
  11. if ( typeof no_trailing !== 'boolean' ) {
  12. debounce_mode = callback;
  13. callback = no_trailing;
  14. no_trailing = undefined;
  15. }
  16.  
  17. // The `wrapper` function encapsulates all of the throttling / debouncing
  18. // functionality and when executed will limit the rate at which `callback`
  19. // is executed.
  20. function wrapper() {
  21. var that = this,
  22. elapsed = +new Date() - last_exec,
  23. args = arguments;
  24.  
  25. // Execute `callback` and update the `last_exec` timestamp.
  26. function exec() {
  27. last_exec = +new Date();
  28. callback.apply( that, args );
  29. };
  30.  
  31. // If `debounce_mode` is true (at_begin) this is used to clear the flag
  32. // to allow future `callback` executions.
  33. function clear() {
  34. timeout_id = undefined;
  35. };
  36.  
  37. if ( debounce_mode && !timeout_id ) {
  38. // Since `wrapper` is being called for the first time and
  39. // `debounce_mode` is true (at_begin), execute `callback`.
  40. exec();
  41. }
  42.  
  43. // Clear any existing timeout.
  44. timeout_id && clearTimeout( timeout_id );
  45.  
  46. if ( debounce_mode === undefined && elapsed > delay ) {
  47. // In throttle mode, if `delay` time has been exceeded, execute
  48. // `callback`.
  49. exec();
  50.  
  51. } else if ( no_trailing !== true ) {
  52. // In trailing throttle mode, since `delay` time has not been
  53. // exceeded, schedule `callback` to execute `delay` ms after most
  54. // recent execution.
  55. //
  56. // If `debounce_mode` is true (at_begin), schedule `clear` to execute
  57. // after `delay` ms.
  58. //
  59. // If `debounce_mode` is false (at end), schedule `callback` to
  60. // execute after `delay` ms.
  61. timeout_id = setTimeout( debounce_mode ? clear : exec, debounce_mode === undefined ? delay - elapsed : delay );
  62. }
  63. };
  64. if ( $.guid ) {
  65. wrapper.guid = callback.guid = callback.guid || $.guid++;
  66. }
  67. return wrapper;
  68. };
  69.  
  70.  
  71. $.fn.isInViewport = function() {
  72. var elementTop = $(this).offset().top;
  73. var elementBottom = elementTop + $(this).outerHeight();
  74. var viewportTop = $(window).scrollTop();
  75. var viewportBottom = viewportTop + $(window).height();
  76. let percent = (($(this).height() - (viewportBottom - elementBottom)) / $(this).height())*100;
  77. return elementBottom > viewportTop && elementTop < viewportBottom && percent >= 59 && percent <= 160;
  78.  
  79. };
  80.  
  81. $('.content').on('resize scroll', $.throttle(200,function() {
  82. let reviewsView = $('#reviews').isInViewport() ;
  83. let tariffsView = $('#tariffs').isInViewport();
  84. let topView = $('#top').isInViewport() || $('#how-it-works').isInViewport() || $('.for-what').isInViewport() ;
  85. if (!reviewsView && !tariffsView && !topView){
  86. return
  87. }
  88. $('.active').removeClass('active')
  89. reviewsView?$("a[href$='/#reviews']").addClass('active'):'';
  90. tariffsView?$("a[href$='/#tariffs']").addClass('active'):'';
  91. topView?$("a[href$='/#top']").addClass('active'):'';
  92. }));
Add Comment
Please, Sign In to add comment