Skip to content

Instantly share code, notes, and snippets.

@yratof
Last active December 21, 2015 06:59
Show Gist options
  • Save yratof/6267793 to your computer and use it in GitHub Desktop.
Save yratof/6267793 to your computer and use it in GitHub Desktop.
Smooth scrolling & snapping headers
//This makes all links that start with # scrolling links
$('a[href^="#"]').on('click',function (e) {
e.preventDefault();
var target = this.hash,
$target = $(target);
$('html, body').stop().animate({
'scrollTop': $target.offset().top - 100
}, 900, 'swing', function () {
window.location.hash = target - 100;
});
});
//This makes a submenu called .breadcrumbs stick to the top of the browser once it goes past it
//Uses the class .fixed when it triggers it. it's then offsetting the scroll to link by 100px which is the height of the fixed element
$(function() {
if($('.breadcrumbs').length > 0){
var top = $('.breadcrumbs').offset().top - 100; //parseFloat($('.breadcrumbs').css('margin-top').replace(/auto/, 0));
$(window).scroll(function() {
// what the y position of the scroll is
var y = $(this).scrollTop();
// whether that's below the form
if (y >= top) {
// if so, ad the fixed class
$('.crumbs').addClass('fixed');
$('.bread .page-title').addClass('hideAway');
$('.BackToTop').removeClass('hideAway');
} else {
// otherwise remove it
$('.crumbs').removeClass('fixed');
$('.bread .page-title').removeClass('hideAway');
$('.BackToTop').addClass('hideAway');
}
});
}
});
//back to top link scrolls back to the top of the page
$(".BackToTop").click(function() {
$('html, body').animate({scrollTop: 0}, 1000);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment