Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 4ff8dcd47698587345c222ff3135f9f9f669c8aa Karl Swedberg committed Sep 21, 2009
Showing with 113 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +104 −0 jquery.smooth-scroll.js
  3. +8 −0 readme.textile
@@ -0,0 +1 @@
+*.DS_Store
@@ -0,0 +1,104 @@
+;(function($) {
+// Animated Scrolling for Same-Page Links
+// @see http://www.learningjquery.com/2007/10/improved-animated-scrolling-script-for-same-page-links
+
+$.fn.smoothScroll = function(options) {
+ var opts = $.extend({}, $.fn.smoothScroll.defaults, options),
+ locationPath = filterPath(location.pathname),
+ scrollElem = scrollableElement('html', 'body');
+
+ this.each(function() {
+ var link = this,
+ $link = $(this),
+ hostMatch = ((location.hostname === link.hostname) || !link.hostname),
+ pathMatch = (filterPath(link.pathname) || locationPath) === locationPath,
+ thisHash = link.hash && link.hash.replace('#',''),
+ scrollTargetExists = thisHash && !!$('#' + thisHash).length;
+
+ if (hostMatch && pathMatch && scrollTargetExists) {
+ var include = true,
+
+ exclude = opts.exclude,
+ elCounter = 0,
+ el = exclude.length,
+
+ excludeWithin = opts.excludeWithin,
+ ewlCounter = 0,
+ ewl = excludeWithin.length;
+
+ while (include && elCounter < el) {
+ if ($link.is(exclude[elCounter++])) {
+ include = false;
+ }
+ }
+ while (include && ewlCounter < ewl) {
+ if ($link.parents(excludeWithin[ewlCounter++] + ':first').length) {
+ include = false;
+ }
+ }
+
+ if (include) {
+ $link.data('scrollTarget', '#' + thisHash);
+ }
+ }
+
+ });
+
+
+ this.die('click.smoothscroll').live('click.smoothscroll', function(event) {
+ var scrollTargetId = $(this).data('scrollTarget');
+ if (scrollTargetId) {
+ event.preventDefault();
+
+ var scrollTargetOffset = $(scrollTargetId).offset().top;
+
+ $(scrollElem).animate({scrollTop: scrollTargetOffset + opts.offset}, 400, function() {
+ // location.hash = target;
+ });
+ }
+ });
+ return this;
+
+ // private functions
+
+ // don't pass window or document
+ function scrollableElement(els) {
+ for (var i = 0, argLength = arguments.length; i < argLength; i++) {
+ var el = arguments[i],
+ $scrollElement = $(el);
+ if ($scrollElement.scrollTop() > 0) {
+ return el;
+ } else {
+ $scrollElement.scrollTop(1);
+ var isScrollable = $scrollElement.scrollTop() > 0;
+ $scrollElement.scrollTop(0);
+ if (isScrollable) {
+ return el;
+ }
+ }
+ }
+ return [];
+ }
+
+ function filterPath(string) {
+ return string
+ .replace(/^\//,'')
+ .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
+ .replace(/\/$/,'');
+ }
+
+ function debug($obj) {
+ if (window.console && window.console.log) {
+ window.console.log($obj);
+ }
+ }
+};
+
+// default options
+$.fn.smoothScroll.defaults = {
+ exclude: [],
+ excludeWithin:[],
+ offset: 0
+};
+
+})(jQuery);
@@ -0,0 +1,8 @@
+h1. Smooth Scroll Plugin
+
+* Allows for easy implementation of smooth scrolling for same-page links.
+* Works like this: $('a').smoothScroll();
+* Specify a containing element if you want: $('#container a').smoothScroll();
+* Exclude links if they are within a containing element: $('#container a').smoothScroll({excludeWithin: ['.container2']});
+* Exclude links if they match certain conditions: $('a').smoothScroll({exclude: ['.rough','#chunky']});
+* Adjust where the scrolling stops: $('.backtotop').smoothScroll({offset: -100});

0 comments on commit 4ff8dcd

Please sign in to comment.