Tjena!
So I’ve implemented a scroll hijack. It’s working fine, but I want to adjust scroll speed so it matches the speed of the built-in scroll speed of Webflow controlled by the nav I’ve got fixed at the top of the page.
Public Link: https://preview.webflow.com/preview/azzopardi?preview=6b9df7ae5b7e2fccede9b9195691f2b3
This is the code I’ve got in the footer:
<script type="text/javascript" src="https://dl.dropboxusercontent.com/s/zkhrhwpagv1cxqg/jquery.hammer.js"></script>
<script>
function l(o) { console.log(o) }
$(document).ready(function(){
var sections = $('.box').toArray();
var scroll = {
activeSection: 0,
sectionCount: sections.length - 1,
isThrottled: false,
throttleDuration: 1000,
target: $(sections[0]).position().top
}
function setSizes(){
for (var i = 0; i < sections.length; i++) {
$(sections[i]).css({
'top' : window.innerHeight * i,
'height' : window.innerHeight,
'width' : window.innerWidth
});
}
}
setSizes();
$('body').on('resize', setSizes());
function downSection() {
var positionFromTop = $(sections[scroll.activeSection + 1]).position().top;
$("body, html").animate({ "scrollTop": positionFromTop }, 300);
++scroll.activeSection;
}
function upSection(){
var positionFromTop = $(sections[scroll.activeSection - 1]).position().top;
$("body, html").animate({ "scrollTop": positionFromTop }, 300);
--scroll.activeSection;
}
$("body").hammer({ preventDefault: true }).on("swipe", function(event) {
if (event.gesture.direction == 'up') {
if(scroll.activeSection != sections.length - 1){
downSection();
l('SWIPED UP');
}
} else if (event.gesture.direction == 'down') {
if(scroll.activeSection != 0){
upSection();
l('SWIPED DOWN');
}
}
});
$(window).on('scroll',function(e){
e.preventDefault();
});
$(window).on('mousewheel', function(event){
event.preventDefault();
if (scroll.isThrottled) { return; }
scroll.isThrottled = true;
setTimeout(function () {
scroll.isThrottled = false;
}, scroll.throttleDuration);
if(event.originalEvent.wheelDelta > 0) {
if(scroll.activeSection === 0) return false;
upSection();
l('WHEELED DOWN');
} else {
if(scroll.activeSection >= scroll.sectionCount) return false;
downSection();
l('WHEELED UP');
}
});
$(window).keydown(function(e){
if (e.keyCode == 40 && (scroll.activeSection != sections.length - 1) ){
downSection();
l('ARROW DOWN');
} else if(e.keyCode == 38 && (scroll.activeSection != 0)){
upSection();
l('ARROW UP');
}
});
}); // end doc ready
</script>
I tried to adjust the “throttleDuration: 1000”, but that didn’t do it.
Any thoughts on this? @bartekkustra?
One thing I’m thinking about is…if I match the scroll hijack speed to Webflows scroll speed…the page will be a pretty slow in scrolling…and annoy people…? There’s no way of speeding up the Webflow-scroll?
PS. You’ll find the script source here: http://codepen.io/smongey/pen/CLzkb