Create a gist now

Instantly share code, notes, and snippets.

@stryju /1_debounce.js Secret
Last active Aug 29, 2015

What would you like to do?
var debounce;
$(window).on('scroll', function (ev) {
if (debounce) {
clearTimeout(debounce);
}
debounce = setTimeout(function () {
if($(window).scrollTop() > secondaryNavTopPosition ) {
// ...
} else {
// ...
}
});
});
var triggerTimeout;
$(window).on('scroll', function (ev) {
if (triggerTimeout) {
clearTimeout(triggerTimeout);
}
if($(window).scrollTop() > secondaryNavTopPosition ) {
secondaryNav.addClass('is-fixed');
triggerTimeout = setTimeout(function() {
secondaryNav.addClass('animate-children');
$('#cd-logo').addClass('slide-in');
$('.cd-btn').addClass('slide-in');
}, 60);
} else {
// ...
}
});
var debounce;
$(window).on('scroll', function (ev) {
if (debounce) {
clearTimeout(debounce);
}
debounce = setTimeout(function () {
if($(window).scrollTop() > secondaryNavTopPosition ) {
secondaryNav.addClass('is-fixed');
debounce = setTimeout(function() {
secondaryNav.addClass('animate-children');
$('#cd-logo').addClass('slide-in');
$('.cd-btn').addClass('slide-in');
}, 60);
} else {
// ...
}
});
});
var win = $(window),
logo = $('#cd-logo'),
btn = $('.cd-btn'),
secondaryNav = $('.cd-secondary-nav'),
secondaryNavTopPosition = secondaryNav.offset().top,
debounce;
win.on('scroll', function(){
if (debounce) {
clearTimeout(debounce);
}
debounce = setTimeout(function () {
var collapsedTop = win.scrollTop() > secondaryNavTopPosition;
secondaryNav.toggleClass('is-fixed', collapsedTop);
debounce = setTimeout(function() {
secondaryNav.toggleClass('animate-children', collapsedTop);
logo.toggleClass('slide-in', collapsedTop);
btn.toggleClass('slide-in', collapsedTop);
}, 60);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment