Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $.throttle = jq_throttle = function( delay, no_trailing, callback, debounce_mode ) {
- // After wrapper has stopped being called, this timeout ensures that
- // `callback` is executed at the proper times in `throttle` and `end`
- // debounce modes.
- var timeout_id,
- // Keep track of the last time `callback` was executed.
- last_exec = 0;
- // `no_trailing` defaults to falsy.
- if ( typeof no_trailing !== 'boolean' ) {
- debounce_mode = callback;
- callback = no_trailing;
- no_trailing = undefined;
- }
- // The `wrapper` function encapsulates all of the throttling / debouncing
- // functionality and when executed will limit the rate at which `callback`
- // is executed.
- function wrapper() {
- var that = this,
- elapsed = +new Date() - last_exec,
- args = arguments;
- // Execute `callback` and update the `last_exec` timestamp.
- function exec() {
- last_exec = +new Date();
- callback.apply( that, args );
- };
- // If `debounce_mode` is true (at_begin) this is used to clear the flag
- // to allow future `callback` executions.
- function clear() {
- timeout_id = undefined;
- };
- if ( debounce_mode && !timeout_id ) {
- // Since `wrapper` is being called for the first time and
- // `debounce_mode` is true (at_begin), execute `callback`.
- exec();
- }
- // Clear any existing timeout.
- timeout_id && clearTimeout( timeout_id );
- if ( debounce_mode === undefined && elapsed > delay ) {
- // In throttle mode, if `delay` time has been exceeded, execute
- // `callback`.
- exec();
- } else if ( no_trailing !== true ) {
- // In trailing throttle mode, since `delay` time has not been
- // exceeded, schedule `callback` to execute `delay` ms after most
- // recent execution.
- //
- // If `debounce_mode` is true (at_begin), schedule `clear` to execute
- // after `delay` ms.
- //
- // If `debounce_mode` is false (at end), schedule `callback` to
- // execute after `delay` ms.
- timeout_id = setTimeout( debounce_mode ? clear : exec, debounce_mode === undefined ? delay - elapsed : delay );
- }
- };
- if ( $.guid ) {
- wrapper.guid = callback.guid = callback.guid || $.guid++;
- }
- return wrapper;
- };
- $.fn.isInViewport = function() {
- var elementTop = $(this).offset().top;
- var elementBottom = elementTop + $(this).outerHeight();
- var viewportTop = $(window).scrollTop();
- var viewportBottom = viewportTop + $(window).height();
- let percent = (($(this).height() - (viewportBottom - elementBottom)) / $(this).height())*100;
- return elementBottom > viewportTop && elementTop < viewportBottom && percent >= 59 && percent <= 160;
- };
- $('.content').on('resize scroll', $.throttle(200,function() {
- let reviewsView = $('#reviews').isInViewport() ;
- let tariffsView = $('#tariffs').isInViewport();
- let topView = $('#top').isInViewport() || $('#how-it-works').isInViewport() || $('.for-what').isInViewport() ;
- if (!reviewsView && !tariffsView && !topView){
- return
- }
- $('.active').removeClass('active')
- reviewsView?$("a[href$='/#reviews']").addClass('active'):'';
- tariffsView?$("a[href$='/#tariffs']").addClass('active'):'';
- topView?$("a[href$='/#top']").addClass('active'):'';
- }));
Add Comment
Please, Sign In to add comment