Skip to content

Instantly share code, notes, and snippets.

@0x3333
Last active August 29, 2015 14:17
Show Gist options
  • Save 0x3333/e9ccd950c4d30bf5393a to your computer and use it in GitHub Desktop.
Save 0x3333/e9ccd950c4d30bf5393a to your computer and use it in GitHub Desktop.
Scroll to an element if not visible
<div id="controls">
<button type="button" id="button1">Find #1</button>
<button type="button" id="button3">Find #3</button>
</div>
<div id="texts">
<p id="p1">#1 Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
<p>#2 Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
<p id="p3">#3 Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
<p>#4 Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
<p>#5 Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</div>
function scrollTo(domElement) {
var PADDING = 10;
var elem = $(domElement),
visibleHeight = window.innerHeight,
elemTop = elem.offset().top,
elemBottom = elemTop + elem.innerHeight(),
scrollToTop = window.scrollY;
if (elemBottom > (visibleHeight + window.scrollY)) {
scrollToTop = elemBottom - visibleHeight + PADDING;
} else if (elemTop < window.scrollY) {
scrollToTop = elemTop - PADDING;
}
$('html, body').animate({
scrollTop : scrollToTop
}, 250);
}
$("#button1").click(function() {
scrollTo(document.getElementById("p1"));
});
$("#button3").click(function() {
scrollTo(document.getElementById("p3"));
});
#controls {
position: fixed;
top: 5px;
}
#texts {
margin-top: 30px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment