Skip to content

Commit 3ab34cc

Browse files
committed
Merge branch 'master' into gh-pages
2 parents c98dd30 + f0de324 commit 3ab34cc

File tree

11 files changed

+207
-29
lines changed

11 files changed

+207
-29
lines changed

Gruntfile.js

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*global module:false*/
22

33
module.exports = function(grunt) {
4-
4+
var pkg = grunt.file.readJSON('package.json');
55
var marked = require('marked');
66
var hl = require('node-syntaxhighlighter');
77

@@ -18,7 +18,7 @@ module.exports = function(grunt) {
1818
// Project configuration.
1919
grunt.initConfig({
2020
pluginName: 'smooth-scroll',
21-
pkg: grunt.file.readJSON('package.json'),
21+
pkg: pkg,
2222
meta: {
2323
banner: '/*!<%= "\\n" %>' +
2424
' * <%= pkg.title %> - v<%= pkg.version %> - ' +
@@ -103,21 +103,22 @@ module.exports = function(grunt) {
103103
}
104104
},
105105
version: {
106-
107-
files: {
108-
src: [
109-
'package.json',
110-
'src/jquery.<%= pluginName %>.js',
111-
'jquery.<%= pluginName %>.js'
112-
]
106+
src: {
107+
src: ['src/jquery.<%= pluginName %>.js']
113108
},
114-
banner: {
115-
src: ['jquery.<%= pluginName %>.js'],
109+
banners: {
110+
pkg: pkg,
111+
src: [
112+
'jquery.<%= pluginName %>.js',
113+
'jquery.<%= pluginName %>.min.js'
114+
],
116115
options: {
117116
prefix: '- v'
118117
}
119118
},
120-
119+
package: {
120+
src: ['package.json']
121+
},
121122
}
122123
});
123124

@@ -130,11 +131,34 @@ module.exports = function(grunt) {
130131
grunt.file.write('index.html', head + doc + foot);
131132
});
132133

134+
grunt.registerTask('updateBower', 'Update bower.json to match package.json', function() {
135+
var pkg = require('./package.json');
136+
var props = ['name', 'main', 'homepage', 'repository', 'dependencies', 'keywords', 'license'];
137+
var json = {
138+
description: 'Easy implementation of smooth scrolling for same-page links'
139+
};
140+
141+
props.forEach(function(item) {
142+
if (pkg[item]) {
143+
json[item] = pkg[item];
144+
}
145+
});
146+
147+
json.authors = [pkg.author];
148+
json.moduleType = ['amd', 'node'];
149+
json.ignore = ['demo/', 'lib/', 'src/', 'test/', '**/.*', 'Gruntfile.js', 'package.json'];
150+
151+
grunt.file.write('bower.json', JSON.stringify(json, null, 2));
152+
});
153+
133154
grunt.registerTask('lint', ['jshint', 'jscs']);
134155
grunt.registerTask('build', ['lint', 'concat', 'version', 'uglify', 'docs']);
135-
grunt.registerTask('patch', ['lint', 'concat', 'version::patch', 'uglify']);
136156
grunt.registerTask('default', ['build']);
137157

158+
['patch', 'minor', 'major'].forEach(function(release) {
159+
grunt.registerTask(release, ['lint', 'version:src:' + release, 'concat', 'uglify', 'version:banners:' + release, 'version:package:' + release]);
160+
});
161+
138162
grunt.loadNpmTasks('grunt-jscs');
139163
grunt.loadNpmTasks('grunt-contrib-jshint');
140164
grunt.loadNpmTasks('grunt-contrib-uglify');

bower.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"description": "Easy implementation of smooth scrolling for same-page links",
3+
"name": "jquery-smooth-scroll",
4+
"main": "jquery.smooth-scroll.js",
5+
"homepage": "https://github.com/kswedberg/jquery-smooth-scroll",
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/kswedberg/jquery-smooth-scroll"
9+
},
10+
"dependencies": {
11+
"jquery": ">=1.4.2"
12+
},
13+
"keywords": [
14+
"jQuery",
15+
"jquery-plugin",
16+
"scroll",
17+
"animation"
18+
],
19+
"license": "MIT",
20+
"authors": [
21+
{
22+
"name": "Karl Swedberg",
23+
"email": "kswedberg@gmail.com",
24+
"url": "http://www.learningjquery.com/"
25+
}
26+
],
27+
"moduleType": [
28+
"amd",
29+
"node"
30+
],
31+
"ignore": [
32+
"demo/",
33+
"lib/",
34+
"src/",
35+
"test/",
36+
"**/.*",
37+
"Gruntfile.js",
38+
"package.json"
39+
]
40+
}

demo/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
<li><a href="#p3">p3</a></li>
8484
<li><a href="#p4">p4</a></li>
8585
<li><a href="#p5">p5</a></li>
86+
<li><a href="#foo">link to nowhere</a></li>
8687
<li><a href="bbq.html#p5">bbq.html#p5</a></li>
8788
</ul>
8889
<p><a href="bbq.html">Try it with jQuery BBQ hashchange support</a></p>

index.html

Lines changed: 20 additions & 4 deletions
Large diffs are not rendered by default.

jquery.smooth-scroll.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* jQuery Smooth Scroll - v1.6.2 - 2016-01-16
2+
* jQuery Smooth Scroll - v1.7.2 - 2016-01-23
33
* https://github.com/kswedberg/jquery-smooth-scroll
44
* Copyright (c) 2016 Karl Swedberg
55
* Licensed MIT
@@ -18,7 +18,7 @@
1818
}
1919
}(function($) {
2020

21-
var version = '1.6.2';
21+
var version = '1.7.2';
2222
var optionOverrides = {};
2323
var defaults = {
2424
exclude: [],
@@ -167,6 +167,10 @@
167167
var pathMatch = thisOpts.scrollTarget || (linkPath === locationPath);
168168
var thisHash = escapeSelector(link.hash);
169169

170+
if (thisHash && !$(thisHash).length) {
171+
include = false;
172+
}
173+
170174
if (!thisOpts.scrollTarget && (!hostMatch || !pathMatch || !thisHash)) {
171175
include = false;
172176
} else {

jquery.smooth-scroll.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "jquery-smooth-scroll",
33
"title": "jQuery Smooth Scroll",
4-
"version": "1.6.2",
4+
"version": "1.7.2",
55
"scripts": {},
66
"main": "jquery.smooth-scroll.js",
77
"author": {

readme.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,62 @@ for `$.smoothScroll`:
138138
`$('html, body').firstScrollable().animate({scrollTop: someNumber},
139139
someSpeed)`
140140

141+
## Examples
142+
143+
### Smooth scrolling on page load
144+
145+
If you want to scroll to an element when the page loads, use `$.smoothScroll()` in a script at the end of the body or use `$(document).ready()`. 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 _not_ 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 `$.smoothScroll()`.
146+
147+
For example, let's say you want to smooth scroll to `<div id="scrolltome"></div>` on page-2.html. For page-1.html, your script might do the following:
148+
149+
```js
150+
$('a[href="page-2.html#scrolltome"]').attr('href', function() {
151+
var hrefParts = this.href.split(/#/);
152+
hrefParts[1] = 'smoothScroll' + hrefParts[1];
153+
return hrefParts.join('#');
154+
});
155+
156+
```
157+
158+
Then for page-2.html, your script would do this:
159+
160+
```js
161+
// Call $.smoothScroll if location.hash starts with "#smoothScroll"
162+
var reSmooth = /^#smoothScroll/;
163+
var id;
164+
if (reSmooth.test(location.hash)) {
165+
// Strip the "#smoothScroll" part off (and put "#" back on the beginning)
166+
id = '#' + location.hash.replace(reSmooth, '');
167+
$.smoothScroll({scrollTarget: id});
168+
}
169+
```
170+
171+
## Focus element after scrolling to it.
172+
173+
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. With the smoothScroll plugin, you can use the `afterScroll` callback function. Here is an example that focuses the first input within the form after scrolling to the form:
174+
175+
```js
176+
$('a.example').smoothScroll({
177+
afterScroll: function(options) {
178+
$(options.scrollTarget).find('input')[0].focus();
179+
}
180+
});
181+
182+
```
183+
184+
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 `tabIndex` attribute to the target element:
185+
186+
```js
187+
$('a.example').smoothScroll({
188+
afterScroll: function(options) {
189+
var $tgt = $(options.scrollTarget);
190+
$tgt.attr('tabIndex', '0');
191+
// Using $tgt[0] allows us to call .focus() on the DOM node itself, not the jQuery collection
192+
$tgt[0].focus();
193+
}
194+
});
195+
```
196+
141197
## Notes
142198

143199
* To determine where to scroll the page, the `$.fn.smoothScroll` method looks

src/jquery.smooth-scroll.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(function($) {
2-
var version = '1.6.2';
2+
var version = '1.7.2';
33
var optionOverrides = {};
44
var defaults = {
55
exclude: [],
@@ -148,6 +148,10 @@
148148
var pathMatch = thisOpts.scrollTarget || (linkPath === locationPath);
149149
var thisHash = escapeSelector(link.hash);
150150

151+
if (thisHash && !$(thisHash).length) {
152+
include = false;
153+
}
154+
151155
if (!thisOpts.scrollTarget && (!hostMatch || !pathMatch || !thisHash)) {
152156
include = false;
153157
} else {

0 commit comments

Comments
 (0)