/* Based on the recommendation of Chris Coyier */ jQuery(document).ready(function() { function findPosY(obj) { var curtop = 0; if (obj.offsetParent) { while (1) { curtop+=obj.offsetTop; if (!obj.offsetParent) { break; } obj=obj.offsetParent; } } else if (obj.y) { curtop+=obj.y; } return curtop; } function filterPath(string) { return string .replace(/^\//,'') .replace(/(index|default).[a-zA-Z]{3,4}$/,'') .replace(/\/$/,''); } var locationPath = filterPath(location.pathname); var scrollElem = scrollableElement('html', 'body'); var $scrollElem = jQuery(scrollElem); jQuery('a[href*=#]').each(function() { var $parent = jQuery(this).parent(); var $grandparent = jQuery($parent).parent(); if (jQuery($grandparent).hasClass('ui-tabs-nav') == false) { var thisPath = filterPath(this.pathname) || locationPath; if ( locationPath == thisPath && (location.hostname == this.hostname || !this.hostname) && this.hash.replace(/#/,'') ) { var $target = jQuery(this.hash).first(), $targethash = this.hash; if ($target.length < 1) { $target = jQuery("[name=" + this.hash.slice(1) + "]").first(); } if ($targethash && $target.length > 0) { //console.log('Target Offset Top: ' + findPosY($target[0])); jQuery(this).click(function(event) { var targetOffset = findPosY($target[0]) - (jQuery(window).height() * 0.2); var distScale = 0.001; var timeScale = 0.2; var distance = ($scrollElem.scrollTop() - targetOffset) * distScale; if (distance < 0) { distance = 0 - distance; } // make distance an absolute value var time = 500 + distance * timeScale * 1000; jQuery(scrollElem).animate({scrollTop: targetOffset}, time, function() { }); window.location.hash = ' '; /*history.pushState(' ', document.title, window.location.pathname);*/ event.preventDefault(); //setTimeout(function() { jQuery(scrollElem).stop(true, false); }, time - (time * 0.05)); }); } } } }); // use the first element that is "scrollable" function scrollableElement(els) { for (var i = 0, argLength = arguments.length; i 0) { return el; } else { $scrollElement.scrollTop(1); var isScrollable = $scrollElement.scrollTop()> 0; $scrollElement.scrollTop(0); if (isScrollable) { return el; } } } return []; } });