|
| 1 | +;(function($) { |
| 2 | +// Animated Scrolling for Same-Page Links |
| 3 | +// @see http://www.learningjquery.com/2007/10/improved-animated-scrolling-script-for-same-page-links |
| 4 | + |
| 5 | +$.fn.smoothScroll = function(options) { |
| 6 | + var opts = $.extend({}, $.fn.smoothScroll.defaults, options), |
| 7 | + locationPath = filterPath(location.pathname), |
| 8 | + scrollElem = scrollableElement('html', 'body'); |
| 9 | + |
| 10 | + this.each(function() { |
| 11 | + var link = this, |
| 12 | + $link = $(this), |
| 13 | + hostMatch = ((location.hostname === link.hostname) || !link.hostname), |
| 14 | + pathMatch = (filterPath(link.pathname) || locationPath) === locationPath, |
| 15 | + thisHash = link.hash && link.hash.replace('#',''), |
| 16 | + scrollTargetExists = thisHash && !!$('#' + thisHash).length; |
| 17 | + |
| 18 | + if (hostMatch && pathMatch && scrollTargetExists) { |
| 19 | + var include = true, |
| 20 | + |
| 21 | + exclude = opts.exclude, |
| 22 | + elCounter = 0, |
| 23 | + el = exclude.length, |
| 24 | + |
| 25 | + excludeWithin = opts.excludeWithin, |
| 26 | + ewlCounter = 0, |
| 27 | + ewl = excludeWithin.length; |
| 28 | + |
| 29 | + while (include && elCounter < el) { |
| 30 | + if ($link.is(exclude[elCounter++])) { |
| 31 | + include = false; |
| 32 | + } |
| 33 | + } |
| 34 | + while (include && ewlCounter < ewl) { |
| 35 | + if ($link.parents(excludeWithin[ewlCounter++] + ':first').length) { |
| 36 | + include = false; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + if (include) { |
| 41 | + $link.data('scrollTarget', '#' + thisHash); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + }); |
| 46 | + |
| 47 | + |
| 48 | + this.die('click.smoothscroll').live('click.smoothscroll', function(event) { |
| 49 | + var scrollTargetId = $(this).data('scrollTarget'); |
| 50 | + if (scrollTargetId) { |
| 51 | + event.preventDefault(); |
| 52 | + |
| 53 | + var scrollTargetOffset = $(scrollTargetId).offset().top; |
| 54 | + |
| 55 | + $(scrollElem).animate({scrollTop: scrollTargetOffset + opts.offset}, 400, function() { |
| 56 | + // location.hash = target; |
| 57 | + }); |
| 58 | + } |
| 59 | + }); |
| 60 | + return this; |
| 61 | + |
| 62 | + // private functions |
| 63 | + |
| 64 | + // don't pass window or document |
| 65 | + function scrollableElement(els) { |
| 66 | + for (var i = 0, argLength = arguments.length; i < argLength; i++) { |
| 67 | + var el = arguments[i], |
| 68 | + $scrollElement = $(el); |
| 69 | + if ($scrollElement.scrollTop() > 0) { |
| 70 | + return el; |
| 71 | + } else { |
| 72 | + $scrollElement.scrollTop(1); |
| 73 | + var isScrollable = $scrollElement.scrollTop() > 0; |
| 74 | + $scrollElement.scrollTop(0); |
| 75 | + if (isScrollable) { |
| 76 | + return el; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + return []; |
| 81 | + } |
| 82 | + |
| 83 | + function filterPath(string) { |
| 84 | + return string |
| 85 | + .replace(/^\//,'') |
| 86 | + .replace(/(index|default).[a-zA-Z]{3,4}$/,'') |
| 87 | + .replace(/\/$/,''); |
| 88 | + } |
| 89 | + |
| 90 | + function debug($obj) { |
| 91 | + if (window.console && window.console.log) { |
| 92 | + window.console.log($obj); |
| 93 | + } |
| 94 | + } |
| 95 | +}; |
| 96 | + |
| 97 | +// default options |
| 98 | +$.fn.smoothScroll.defaults = { |
| 99 | + exclude: [], |
| 100 | + excludeWithin:[], |
| 101 | + offset: 0 |
| 102 | +}; |
| 103 | + |
| 104 | +})(jQuery); |
0 commit comments