Skip to content

Instantly share code, notes, and snippets.

@fullpipe
Created June 27, 2014 06:49
Show Gist options
  • Save fullpipe/8cb31e6cd185455c2abd to your computer and use it in GitHub Desktop.
Save fullpipe/8cb31e6cd185455c2abd to your computer and use it in GitHub Desktop.
Scroll page to specific element.
$(document).ready(function() {
"use strict";
$.fn.scrollToMe = function(force) {
var
force = force || false,
elemOffset = $(this).offset(),
$bodyElem = $.browser.safari ? $("body") : $("html"),
topScroll = +$bodyElem.scrollTop(),
windowHeight = $(window).height()
;
if (!force) {
if (elemOffset.top > topScroll && elemOffset.top < (topScroll + windowHeight)) {
return false;
};
};
$bodyElem.animate({
scrollTop: elemOffset.top
}, 'fast');
}
});
@fullpipe
Copy link
Author

$('#menu').scrollToMe();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment