Skip to content

Instantly share code, notes, and snippets.

@sarim
Created December 19, 2013 20:16
Show Gist options
  • Save sarim/8045531 to your computer and use it in GitHub Desktop.
Save sarim/8045531 to your computer and use it in GitHub Desktop.
//Code to keep div fixed at top during scrolling. heavily inspired from http://goo.gl/zYykrQ
var scroller = {
touchstart: function(e) {
e.data.state.posY = e.touches[0].clientY;
e.data.state.offsetY = $('#indicator').offset().top;
$("#status").html("touchstart");
},
touchmove: function(e) {
var currentY = e.touches[0].clientY;
var deltaY = currentY - e.data.state.posY;
var newY = deltaY + e.data.state.offsetY;
$("#status").html("new pos: " + newY);
$('#indicator').offset({top: newY , left:0});
},
touchend: function(e) {
$("#status").html("touchend");
},
scroll: function(e) {
$("#status").html("scroll finished");
$('#indicator').offset({top: $(window).scrollTop() , left:0});
},
state: {posY: 0, offsetY: 0}
}
$(function(){
if (1 || "TODO: Detect Mobile") {
$(window).on('touchstart',scroller,scroller.touchstart);
$(window).on('touchmove',scroller,scroller.touchmove);
$(window).on('touchend',scroller,scroller.touchend);
$(window).on('scroll',scroller,scroller.scroll);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment