megwoo
6/24/2015 - 5:13 AM

JQuery Page Scrolling Functions

// smooth scroll with offset 
	var headerHeight = 100;
	
	$('.div-name a').bind('click', function(e) {
	    e.preventDefault();
				
	    var target = this.hash; //Get the target
	    var scrollToPosition = $(target).offset().top - headerHeight;
	
	    $('html,body').animate({'scrollTop': scrollToPosition }, 600, function(){
	        window.location.hash = '' + target;
	        // This hash change will jump the page to the top of the div with the same id
	        // so we need to force the page to back to the end of the animation
	        $('html').animate({ 'scrollTop': scrollToPosition }, 0);
	    });
	
	    $('body').append('called');    
	});


	// check offset if coming from different page
	$(window).load(function(e){
		if(window.location.hash) {
			// alert (window.location.hash);
			if ($(window.location.hash).length) {
				// alert("exists");
				$(document).scrollTop( $(window.location.hash).offset().top -100 );
			}
		}
	});
    
	
	// back to top
	$('a.to-top').bind('click', function(e) {
	    e.preventDefault();
	    $('html,body').animate({'scrollTop': 0}); 
	});