Skip to content

Instantly share code, notes, and snippets.

@gillesv
gillesv / gist:4277801
Created December 13, 2012 16:43
2 JQuery scrolling methods I use. Scroll to a position in pixels, or scroll to an element. Duration of scrolling animation is calculated dynamically based on how many pixels the viewport has to travel. Fiddle with the math on line 5 to tweak the speed to your liking.
// scrolling methods: scroll to pixels
function scrollViewTo(target, callback) {
var duration, pos;
pos = $body.scrollTop();
duration = ((Math.abs(pos - target) / 1000) * 1000);
$("html:not(:animated),body:not(:animated)").animate({scrollTop: target + 'px'}, duration, callback);
}
// scroll to element