Skip to content

Commit 2da2bf3

Browse files
committed
Add "options" argument feature to get/set options after init
1 parent 2c246cf commit 2da2bf3

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

demo/index.html

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
width: 180px;
2525
}
2626
</style>
27-
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
27+
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
2828
<script>!window.jQuery && document.write(unescape('%3Cscript src="../lib/jquery/jquery.js"%3E%3C/script%3E'));</script>
2929

3030
<script src="../src/jquery.smooth-scroll.js"></script>
@@ -41,6 +41,14 @@
4141
});
4242
});
4343

44+
$('#change-speed').on('click', function() {
45+
var $p1 = $('ul.mainnav a').first(),
46+
p1Opts = $p1.smoothScroll('options') || {};
47+
48+
p1Opts.speed = p1Opts.speed === 1400 ? 400 : 1400;
49+
$p1.smoothScroll('options', p1Opts);
50+
});
51+
4452
$('button.scrollsomething').click(function() {
4553
$.smoothScroll({
4654
scrollElement: $('div.scrollme'),
@@ -73,6 +81,7 @@
7381
<li><a href="bbq.html#p5">bbq.html#p5</a></li>
7482
</ul>
7583
<p>Or, <a href="bbq.html">try it with hashchange support</a></p>
84+
<p>Toggle scrolling speed for the p1 link <button id="change-speed">Change speed option</button></p>
7685
<button class="scrollsomething">Scroll vertically</button>
7786
<button class="scrollhorz">Scroll horizontally</button>
7887

src/jquery.smooth-scroll.js

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
(function($) {
22
var version = '1.4.12',
3+
optionOverrides = {},
34
defaults = {
45
exclude: [],
56
excludeWithin:[],
@@ -86,8 +87,21 @@ $.fn.extend({
8687
return this.pushStack(scrl);
8788
},
8889

89-
smoothScroll: function(options) {
90+
smoothScroll: function(options, extra) {
9091
options = options || {};
92+
93+
if ( options === 'options' ) {
94+
if ( !extra ) {
95+
return this.first().data('ssOpts');
96+
}
97+
return this.each(function() {
98+
var $this = $(this),
99+
opts = $.extend($this.data('ssOpts') || {}, extra);
100+
101+
$(this).data('ssOpts', opts);
102+
});
103+
}
104+
91105
var opts = $.extend({}, $.fn.smoothScroll.defaults, options),
92106
locationPath = $.smoothScroll.filterPath(location.pathname);
93107

@@ -96,16 +110,17 @@ $.fn.extend({
96110
.bind('click.smoothscroll', function(event) {
97111
var link = this,
98112
$link = $(this),
113+
thisOpts = $.extend({}, opts, $link.data('ssOpts') || {}),
99114
exclude = opts.exclude,
100-
excludeWithin = opts.excludeWithin,
115+
excludeWithin = thisOpts.excludeWithin,
101116
elCounter = 0, ewlCounter = 0,
102117
include = true,
103118
clickOpts = {},
104119
hostMatch = ((location.hostname === link.hostname) || !link.hostname),
105-
pathMatch = opts.scrollTarget || ( $.smoothScroll.filterPath(link.pathname) || locationPath ) === locationPath,
120+
pathMatch = thisOpts.scrollTarget || ( $.smoothScroll.filterPath(link.pathname) || locationPath ) === locationPath,
106121
thisHash = escapeSelector(link.hash);
107122

108-
if ( !opts.scrollTarget && (!hostMatch || !pathMatch || !thisHash) ) {
123+
if ( !thisOpts.scrollTarget && (!hostMatch || !pathMatch || !thisHash) ) {
109124
include = false;
110125
} else {
111126
while (include && elCounter < exclude.length) {
@@ -122,15 +137,14 @@ $.fn.extend({
122137

123138
if ( include ) {
124139

125-
if ( opts.preventDefault ) {
140+
if ( thisOpts.preventDefault ) {
126141
event.preventDefault();
127142
}
128143

129-
$.extend( clickOpts, opts, {
130-
scrollTarget: opts.scrollTarget || thisHash,
144+
$.extend( clickOpts, thisOpts, {
145+
scrollTarget: thisOpts.scrollTarget || thisHash,
131146
link: link
132147
});
133-
134148
$.smoothScroll( clickOpts );
135149
}
136150
});
@@ -140,6 +154,9 @@ $.fn.extend({
140154
});
141155

142156
$.smoothScroll = function(options, px) {
157+
if ( options === 'options' && typeof px === 'object' ) {
158+
return $.extend(optionOverrides, px);
159+
}
143160
var opts, $scroller, scrollTargetOffset, speed,
144161
scrollerOffset = 0,
145162
offPos = 'offset',
@@ -148,12 +165,11 @@ $.smoothScroll = function(options, px) {
148165
aniOpts = {},
149166
scrollprops = [];
150167

151-
152168
if (typeof options === 'number') {
153-
opts = $.fn.smoothScroll.defaults;
169+
opts = $.extend({link: null}, $.fn.smoothScroll.defaults, optionOverrides);
154170
scrollTargetOffset = options;
155171
} else {
156-
opts = $.extend({}, $.fn.smoothScroll.defaults, options || {});
172+
opts = $.extend({link: null}, $.fn.smoothScroll.defaults, options || {}, optionOverrides);
157173
if (opts.scrollElement) {
158174
offPos = 'position';
159175
if (opts.scrollElement.css('position') == 'static') {
@@ -162,7 +178,6 @@ $.smoothScroll = function(options, px) {
162178
}
163179
}
164180

165-
opts = $.extend({link: null}, opts);
166181
scrollDir = opts.direction == 'left' ? 'scrollLeft' : scrollDir;
167182

168183
if ( opts.scrollElement ) {

0 commit comments

Comments
 (0)