Skip to content

Commit 4ff8dcd

Browse files
author
Karl Swedberg
committed
first commit
0 parents  commit 4ff8dcd

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.DS_Store

jquery.smooth-scroll.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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);

readme.textile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
h1. Smooth Scroll Plugin
2+
3+
* Allows for easy implementation of smooth scrolling for same-page links.
4+
* Works like this: $('a').smoothScroll();
5+
* Specify a containing element if you want: $('#container a').smoothScroll();
6+
* Exclude links if they are within a containing element: $('#container a').smoothScroll({excludeWithin: ['.container2']});
7+
* Exclude links if they match certain conditions: $('a').smoothScroll({exclude: ['.rough','#chunky']});
8+
* Adjust where the scrolling stops: $('.backtotop').smoothScroll({offset: -100});

0 commit comments

Comments
 (0)