-
Notifications
You must be signed in to change notification settings - Fork 421
Expand file tree
/
Copy pathindex.html
More file actions
309 lines (294 loc) · 15.1 KB
/
index.html
File metadata and controls
309 lines (294 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>jQuery Smooth Scroll Plugin</title>
<style>
html,
body {
margin: 0;
padding: 0;
border-width: 0;
}
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
width: 94%;
margin: 0 auto;
padding: 0 3%;
max-width: 1120px;
font-size: 1em;
line-height: 1.5;
background-color: #fff;
color: #242424;
}
pre {
background-color: #f3f3f3;
padding: .25em;
border: 1px solid #ccc;
font-size: .9em;
overflow: auto;
}
code {
font-family: Monaco, Courier, monospace;
}
.comment,
.comments {
color: #aaa;
}
.keyword {
color: blue;
}
.string {
color: #090;
}
.number {
color: #099;
}
td.gutter {
text-align: right;
padding-right: 3px;
padding-left: 3px;
background-color: #ddd;
color: #aaa;
}
td.code {
padding-left: 5px;
}
h1, h2, h3 {
font-weight: normal;
color: #141414;
}
h2 {
padding-bottom: .2em;
border-bottom: 1px solid #ccc;
}
.right {
float: right;
}
</style>
</head>
<body>
<div class="right"><a href="demo/index.html">Demo</a></div>
<h1>Smooth Scroll Plugin</h1>
<p>Allows for easy implementation of smooth scrolling for same-page links.</p>
<p><a href="https://npmjs.org/package/jquery-smooth-scroll"><img src="https://nodei.co/npm/jquery-smooth-scroll.png?compact=true" alt="NPM"></a></p>
<p>Note: Version 2.0+ of this plugin requires jQuery version 1.7 or greater.</p>
<h2>Setup</h2>
<h3>ES Module</h3>
<pre><code class="language-bash">npm install jquery-smooth-scroll
</code></pre>
<p>or</p>
<pre><code class="language-bash">yarn add jquery-smooth-scroll
</code></pre>
<p>Then, use <code>import jquery-smooth-scroll</code>;</p>
<p><strong>Note</strong>: This assumes <code>window.jQuery</code> is available at time of import.</p>
<h3>CDN</h3>
<pre><code class="language-html"><!--
You can get a URL for jQuery from
https://code.jquery.com
-->
<script src="SOME_URL_FOR_JQUERY"></script>
<script src="https://cdn.statically.io/gh/kswedberg/jquery-smooth-scroll/3948290d/jquery.smooth-scroll.min.js"></script>
</code></pre>
<h3>Copy/paste</h3>
<p>Grab the code and paste it into your own file:</p>
<ul>
<li><a href="https://raw.githubusercontent.com/kswedberg/jquery-smooth-scroll/master/jquery.smooth-scroll.js">Standard version</a></li>
<li><a href="https://raw.githubusercontent.com/kswedberg/jquery-smooth-scroll/master/jquery.smooth-scroll.js">Minified version</a></li>
</ul>
<h2>Demo</h2>
<p>You can try a bare-bones demo at <a href="https://kswedberg.github.io/jquery-smooth-scroll/demo/">kswedberg.github.io/jquery-smooth-scroll/demo/</a></p>
<h2>Features</h2>
<h3>$.fn.smoothScroll</h3>
<ul>
<li>Works like this: <code>$('a').smoothScroll();</code></li>
<li>Specify a containing element if you want: <code>$('#container a').smoothScroll();</code></li>
<li>Exclude links if they are within a containing element: <code>$('#container a').smoothScroll({excludeWithin: ['.container2']});</code></li>
<li>Exclude links if they match certain conditions: <code>$('a').smoothScroll({exclude: ['.rough','#chunky']});</code></li>
<li>Adjust where the scrolling stops: <code>$('.backtotop').smoothScroll({offset: -100});</code></li>
<li>Add a callback function that is triggered before the scroll starts: <code>$('a').smoothScroll({beforeScroll: function() { alert('ready to go!'); }});</code></li>
<li>Add a callback function that is triggered after the scroll is complete: <code>$('a').smoothScroll({afterScroll: function() { alert('we made it!'); }});</code></li>
<li>Add back button support by using a <a href="https://developer.mozilla.org/en-US/docs/Web/API/HashChangeEvent"><code>hashchange</code> event listener</a>. You can also include a history management plugin such as <a href="http://benalman.com/code/projects/jquery-bbq/docs/files/jquery-ba-bbq-js.html">Ben Alman's BBQ</a> for ancient browser support (IE < 8), but you'll need jQuery 1.8 or earlier. See <a href="demo/hashchange.html">demo/hashchange.html</a> or <a href="demo/bbq.html">demo/bbq.html</a> for an example of how to implement.</li>
</ul>
<h4>Options</h4>
<p>The following options, shown with their default values, are available for both <code>$.fn.smoothScroll</code> and <code>$.smoothScroll</code>:</p>
<pre><code class="language-javascript">{
offset: 0,
// one of 'top' or 'left'
direction: 'top',
// only use if you want to override default behavior or if using $.smoothScroll
scrollTarget: null,
// automatically focus the target element after scrolling to it
// (see https://github.com/kswedberg/jquery-smooth-scroll#focus-element-after-scrolling-to-it for details)
autoFocus: false,
// string to use as selector for event delegation
delegateSelector: null,
// fn(opts) function to be called before scrolling occurs.
// `this` is the element(s) being scrolled
beforeScroll: function() {},
// fn(opts) function to be called after scrolling occurs.
// `this` is the triggering element
afterScroll: function() {},
// easing name. jQuery comes with "swing" and "linear." For others, you'll need an easing plugin
// from jQuery UI or elsewhere
easing: 'swing',
// speed can be a number or 'auto'
// if 'auto', the speed will be calculated based on the formula:
// (current scroll position - target scroll position) / autoCoefficient
speed: 400,
// autoCoefficent: Only used when speed set to "auto".
// The higher this number, the faster the scroll speed
autoCoefficient: 2,
// $.fn.smoothScroll only: whether to prevent the default click action
preventDefault: true
}
</code></pre>
<p>The options object for <code>$.fn.smoothScroll</code> can take two additional properties:
<code>exclude</code> and <code>excludeWithin</code>. The value for both of these is an array of
selectors, DOM elements or jQuery objects. Default value for both is an
empty array.</p>
<h4>Setting options after initial call</h4>
<p>If you need to change any of the options after you've already called <code>.smoothScroll()</code>,
you can do so by passing the <code>"options"</code> string as the first argument and an
options object as the second.</p>
<h3>$.smoothScroll</h3>
<ul>
<li><p>Utility method works without a selector: <code>$.smoothScroll()</code></p>
</li>
<li><p>Can be used to scroll any element (not just <code>document.documentElement</code> / <code>document.body</code>)</p>
</li>
<li><p>Doesn't automatically fire, so you need to bind it to some other user
interaction. For example:</p>
<pre><code class="language-js">$('button.scrollsomething').on('click', function() {
$.smoothScroll({
scrollElement: $('div.scrollme'),
scrollTarget: '#findme'
});
return false;
});
</code></pre>
</li>
<li><p>The <code>$.smoothScroll</code> method can take one or two arguments.</p>
<ul>
<li>If the first argument is a number or a "relative string," the document is scrolled to that
position. If it's an options object, those options determine how the
document (or other element) will be scrolled.</li>
<li>If a number or "relative string" is provided as the second argument, it will override whatever may have been set for the <code>scrollTarget</code> option.</li>
<li>The relative string syntax, introduced in version 2.1, looks like <code>"+=100px"</code> or <code>"-=50px"</code> (see below for an example).</li>
</ul>
</li>
</ul>
<h4>Additional Option</h4>
<p>The following option, in addition to those listed for <code>$.fn.smoothScroll</code> above, is available
for <code>$.smoothScroll</code>:</p>
<pre><code class="language-javascript">{
// The jQuery set of elements you wish to scroll.
// if null (default), $('html, body').firstScrollable() is used.
scrollElement: null
}
</code></pre>
<h4>Note:</h4>
<p>If you use <code>$.smoothScroll</code>, do NOT use the <code>body</code> element (<code>document.body</code> or <code>$('body')</code>) alone for the <code>scrollElement</code> option. Probably not a good idea to use <code>document.documentElement</code> (<code>$('html')</code>) by itself either.</p>
<h3>$.fn.scrollable</h3>
<ul>
<li>Selects the matched element(s) that are scrollable. Acts just like a
DOM traversal method such as <code>.find()</code> or <code>.next()</code>.</li>
<li>Uses <code>document.scrollingElement</code> on compatible browsers when the selector is 'html' or 'body' or 'html, body'.</li>
<li>The resulting jQuery set may consist of <strong>zero, one, or multiple</strong>
elements.</li>
</ul>
<h3>$.fn.firstScrollable</h3>
<ul>
<li>Selects the first matched element that is scrollable. Acts just like a
DOM traversal method such as <code>.find()</code> or <code>.next()</code>.</li>
<li>The resulting jQuery set may consist of <strong>zero or one</strong> element.</li>
<li>This method is used <em>internally</em> by the plugin to determine which element
to use for "document" scrolling:
<code>$('html, body').firstScrollable().animate({scrollTop: someNumber}, someSpeed)</code></li>
<li>Uses <code>document.scrollingElement</code> on compatible browsers when the selector is 'html' or 'body' or 'html, body'.</li>
</ul>
<h2>Examples</h2>
<h3>Scroll down one "page" at a time (v2.1+)</h3>
<p>With smoothScroll version 2.1 and later, you can use the "relative string" syntax to scroll an element or the document a certain number of pixels relative to its current position. The following code will scroll the document down one page at a time when the user clicks the ".pagedown" button:</p>
<pre><code class="language-js">$('button.pagedown').on('click', function() {
$.smoothScroll('+=' + $(window).height());
});
</code></pre>
<h3>Smooth scrolling on page load</h3>
<p>If you want to scroll to an element when the page loads, use <code>$.smoothScroll()</code> in a script at the end of the body or use <code>$(document).ready()</code>. To prevent the browser from automatically scrolling to the element on its own, your link on page 1 will need to include a fragment identifier that does <em>not</em> match an element id on page 2. To ensure that users without JavaScript get to the same element, you should modify the link's hash on page 1 with JavaScript. Your script on page 2 will then modify it back to the correct one when you call <code>$.smoothScroll()</code>.</p>
<p>For example, let's say you want to smooth scroll to <code><div id="scrolltome"></div></code> on page-2.html. For page-1.html, your script might do the following:</p>
<pre><code class="language-js">$('a[href="page-2.html#scrolltome"]').attr('href', function() {
var hrefParts = this.href.split(/#/);
hrefParts[1] = 'smoothScroll' + hrefParts[1];
return hrefParts.join('#');
});
</code></pre>
<p>Then for page-2.html, your script would do this:</p>
<pre><code class="language-js">// Call $.smoothScroll if location.hash starts with "#smoothScroll"
var reSmooth = /^#smoothScroll/;
var id;
if (reSmooth.test(location.hash)) {
// Strip the "#smoothScroll" part off (and put "#" back on the beginning)
id = '#' + location.hash.replace(reSmooth, '');
$.smoothScroll({scrollTarget: id});
}
</code></pre>
<h3>Focus element after scrolling to it.</h3>
<p>Imagine you have a link to a form somewhere on the same page. When the user clicks the link, you want the user to be able to begin interacting with that form.</p>
<ul>
<li><p>As of <strong>smoothScroll version 2.2</strong>, the plugin will automatically focus the element if you set the <code>autoFocus</code> option to <code>true</code>.</p>
<pre><code class="language-js">$('div.example').smoothScroll({
autoFocus: true
});
</code></pre>
</li>
<li><p>In the future, versions 3.x and later will have <code>autoFocus</code> set to true <strong>by default</strong>.</p>
</li>
<li><p>If you are using the low-level <code>$.smoothScroll</code> method, <code>autoFocus</code> will only work if you've also provided a value for the <code>scrollTarget</code> option.</p>
</li>
<li><p>Prior to version 2.2, you can use the <code>afterScroll</code> callback function. Here is an example that focuses the first input within the form after scrolling to the form:</p>
</li>
</ul>
<pre><code class="language-js">$('a.example').smoothScroll({
afterScroll: function(options) {
$(options.scrollTarget).find('input')[0].focus();
}
});
</code></pre>
<p>For accessibility reasons, it might make sense to focus any element you scroll to, even if it's not a natively focusable element. To do so, you could add a <code>tabIndex</code> attribute to the target element (this, again, is for versions prior to 2.2):</p>
<pre><code class="language-js">$('div.example').smoothScroll({
afterScroll: function(options) {
var $tgt = $(options.scrollTarget);
$tgt[0].focus();
if (!$tgt.is(document.activeElement)) {
$tgt.attr('tabIndex', '-1');
$tgt[0].focus();
}
}
});
</code></pre>
<h2>Notes</h2>
<ul>
<li>To determine where to scroll the page, the <code>$.fn.smoothScroll</code> method looks
for an element with an <em>id</em> attribute that matches the <code><a></code> element's hash.
It does <em>not</em> look at the element's <em>name</em> attribute. If you want a clicked link
to scroll to a "named anchor" (e.g. <code><a name="foo"></code>), you'll need to use the
<code>$.smoothScroll</code> method instead.</li>
<li>The plugin's <code>$.fn.smoothScroll</code> and <code>$.smoothScroll</code> methods use the
<code>$.fn.firstScrollable</code> DOM traversal method (also defined by this plugin)
to determine which element is scrollable. If no elements are scrollable,
these methods return a jQuery object containing an empty array, just like
all of jQuery's other DOM traversal methods. Any further chained methods,
therefore, will be called against no elements (which, in most cases,
means that nothing will happen).</li>
</ul>
<h2>Contributing</h2>
<p>Thank you! Please consider the following when working on this repo before you submit a pull request:</p>
<ul>
<li>For code changes, please work on the "source" file: <code>src/jquery.smooth-scroll.js</code>.</li>
<li>Style conventions are set in the <code>eslint</code> grunt file options. To be sure your additions comply, run <code>grunt lint</code> from the command line.</li>
<li>If possible, please use Tim Pope's <a href="http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html">git commit message style</a>. Multiple commits in a pull request will be squashed into a single commit. I may adjust the message for clarity, style, or grammar. I manually commit all merged PRs using the <code>--author</code> flag to ensure that proper authorship (yours) is maintained.</li>
</ul>
</body>
</html>