Skip to content

Instantly share code, notes, and snippets.

@bueckl
Created July 26, 2016 13:38
Show Gist options
  • Save bueckl/9823f8b31bb672aee20d5c33494830f9 to your computer and use it in GitHub Desktop.
Save bueckl/9823f8b31bb672aee20d5c33494830f9 to your computer and use it in GitHub Desktop.
Scroll Check -> Scroll Out/In Nav on mobile Devices
add to app.js on very top:
(function() {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame']
|| window[vendors[x]+'CancelRequestAnimationFrame'];
}
if (!window.requestAnimationFrame)
window.requestAnimationFrame = function(callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function() { callback(currTime + timeToCall); },
timeToCall);
lastTime = currTime + timeToCall;
return id;
};
if (!window.cancelAnimationFrame)
window.cancelAnimationFrame = function(id) {
clearTimeout(id);
};
}());
add to site:
scrollCheck: function() {
//if mobile, we dont animate the searchboxholder
if ($( window ).width() > 992 ) {
var el = $('header, .searchbox-holder, #map-big');
} else {
var el = $('header');
}
//$(window).on('scroll', function() {
if ( Site.scrollDirection() == "up" ) {
if ( el.data('faded') ) {
el.data('faded', 0);
el.velocity('stop');
el.velocity({
'top': '0px'
},{
duration: 400,
mobileHA: false
});
$(logo).velocity('stop');
$(logo).velocity({
'scale': 1
},{
duration: 400,
mobileHA: false
});
}
} else if ( Site.scrollDirection() == "down" ) {
// Folded!!!
if ( $(this).scrollTop() > 300 || ( $( window ).width() < 801 && $(this).scrollTop() > 80) ) {
if ( !el.data('faded') ) {
el.data('faded', 1);
el.velocity('stop');
el.velocity({
'top': '-50px'
},{
duration: 400,
mobileHA: false
});
$(logo).velocity('stop');
$(logo).velocity({
'scale': 0,
'transform-origin': 'top left'
},{
duration: 400,
mobileHA: false
});
}
} else if (el.data('faded')) {
el.data('faded', 0);
if(st + $(window).height() < $(document).height()) {
el.velocity('stop');
el.velocity({
'top': '0px'
},{
duration: 400,
mobileHA: false
});
$(logo).velocity('stop');
$(logo).velocity({
'scale': 1
},{
duration: 400,
mobileHA: false
});
}
}
}
//});
},
scrollDirection : function() {
st = window.pageYOffset || document.documentElement.scrollTop;
if (st > lastScrollTop ) {
direction = "down";
} else if (st < lastScrollTop){
direction = "up";
}
lastScrollTop = st;
return direction;
},
………
include raf.js in template first.
the add to app.js:
$(window).scroll(function () {
window.requestAnimationFrame(Site.scrollCheck);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment