Skip to content
Browse files

Initial commit

  • Loading branch information...
0 parents commit dc4473181d6d694b4cf08d60999a51d6f337d6a2 @mathiasbynens committed Dec 15, 2009
Showing with 68 additions and 0 deletions.
  1. +1 −0 .gitattributes
  2. +25 −0 README.md
  3. +36 −0 jquery.smoothscroll.js
  4. +6 −0 jquery.smoothscroll.min.js
1 .gitattributes
@@ -0,0 +1 @@
+* crlf=input
25 README.md
@@ -0,0 +1,25 @@
+# Smooth Scrolling jQuery Plugin
+
+## Demo & Examples
+
+[http://mathiasbynens.be/examples/slideshow](http://mathiasbynens.be/examples/slideshow)
+
+## Example Usage
+
+### Simple – Inside entire document
+
+Find all links to anchors in the same document, and make them scroll smoothly when clicked:
+
+ $('html').smoothScroll();
+
+### Simple – Inside a specified element
+
+Find all links to anchors in the same document inside `#content`, and make them scroll smoothly when clicked:
+
+ $('#content').smoothScroll();
+
+### Specifying parameters
+
+ $('html').smoothScroll({ speed: 300 });
+
+_– [Mathias](http://mathiasbynens.be/)_
36 jquery.smoothscroll.js
@@ -0,0 +1,36 @@
+/*!
+ * Smooth Scrolling jQuery Plugin
+ * @link http://github.com/mathiasbynens/Smooth-Scrolling-jQuery-Plugin
+ * @author Mathias Bynens <http://mathiasbynens.be/>
+ */
+;(function($) {
+ $.fn.smoothScroll = function(settings) {
+ // Specify default settings
+ var config = {
+ speed: 400 // 'normal'
+ };
+ // Use custom settings, if any
+ if (settings) {
+ $.extend(config, settings);
+ };
+ // Inside the .click() block, `this` references the `A` element object instead of the selected parent element, so let’s store it inside a var
+ $elem = $(this);
+ // Look for links to anchors (on any page)
+ $(this).find('a[href*=#]').click(function() {
+ // If it’s a link to an anchor in the same document
+ if (location.pathname.replace(/^\//, '') === this.pathname.replace(/^\//, '') && location.hostname === this.hostname) {
+ // Get the in-document element the link points to…
+ var $hash = $(this.hash);
+ // …and if it exists…
+ if ($hash.length) {
+ // …smoothly scroll to it
+ $elem.animate({ scrollTop: $hash.offset().top }, config.speed);
+ // Don’t jump to the link right away
+ return false;
+ };
+ };
+ });
+ // Allow chaining
+ return this;
+ };
+})(jQuery);
6 jquery.smoothscroll.min.js
@@ -0,0 +1,6 @@
+/*!
+ * Smooth Scrolling jQuery Plugin
+ * @link http://github.com/mathiasbynens/Smooth-Scrolling-jQuery-Plugin
+ * @author Mathias Bynens <http://mathiasbynens.be/>
+ */
+(function(a){a.fn.smoothScroll=function(c){var b={speed:400};if(c){a.extend(b,c)}$elem=a(this);a(this).find('a[href*=#]').click(function(){if(location.pathname.replace(/^\//,'')===this.pathname.replace(/^\//,'')&&location.hostname===this.hostname){var d=a(this.hash);if(d.length){$elem.animate({scrollTop:d.offset().top},b.speed);return false}}});return this}})(jQuery);

0 comments on commit dc44731

Please sign in to comment.
Something went wrong with that request. Please try again.