From 7d1560426a3d00529f19163aab6a67953c7b955b Mon Sep 17 00:00:00 2001 From: Karl Swedberg Date: Sat, 2 Mar 2013 19:22:37 -0500 Subject: [PATCH 01/58] Improve docs: * Add index.html, auto-generated by a grunt task * Note that smooth-scrolling to `` doesn't work with $('a').smoothScroll(); must use $.smoothScroll() inside event handler instead. * Add note about beforeScroll callback * Clean up options object comments --- Gruntfile.js | 30 +++++++-- index.html | 160 ++++++++++++++++++++++++++++++++++++++++++++ lib/tmpl/footer.tpl | 3 + lib/tmpl/header.tpl | 50 ++++++++++++++ package.json | 3 +- readme.md | 68 +++++++++++++------ 6 files changed, 285 insertions(+), 29 deletions(-) create mode 100644 index.html create mode 100644 lib/tmpl/footer.tpl create mode 100644 lib/tmpl/header.tpl diff --git a/Gruntfile.js b/Gruntfile.js index 93a4980..6c9e337 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -2,9 +2,6 @@ module.exports = function(grunt) { - // Path to private settings (used here for user/pwd to rsync to remote site) - - // Because I'm lazy var _ = grunt.util._; @@ -48,7 +45,12 @@ module.exports = function(grunt) { scripts: { files: '<%= jshint.all %>', tasks: ['jshint:all'] + }, + docs: { + files: ['readme.md', 'lib/tmpl/**/*.html'], + tasks: ['docs'] } + }, shell: { rsync: { @@ -77,7 +79,8 @@ module.exports = function(grunt) { eqnull: true, browser: true, globals: { - jQuery: true + jQuery: true, + require: false } } }, @@ -138,8 +141,6 @@ module.exports = function(grunt) { pkg = grunt.file.readJSON(pkgName + '.jquery.json'), json = {}; - - ['name', 'version', 'dependencies'].forEach(function(el) { json[el] = pkg[el]; }); @@ -159,13 +160,28 @@ module.exports = function(grunt) { grunt.log.writeln( "File '" + comp + "' updated." ); }); - grunt.registerTask('build', ['jshint', 'concat', 'version:same', 'component', 'uglify']); + grunt.registerTask('docs', function() { + var marked = require('marked'), + readme = grunt.file.read('readme.md'), + head = grunt.template.process(grunt.file.read('lib/tmpl/header.tpl')), + foot = grunt.file.read('lib/tmpl/footer.tpl'), + doc = marked(readme); + + marked.setOptions({ + gfm: true + }); + + grunt.file.write('index.html', head + doc + foot); + }); + + grunt.registerTask('build', ['jshint', 'concat', 'version:same', 'component', 'uglify', 'docs']); grunt.registerTask('patch', ['jshint', 'concat', 'version:bannerPatch', 'version:patch', 'component', 'uglify']); grunt.registerTask('default', ['build']); grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.loadNpmTasks('grunt-contrib-concat'); + grunt.loadNpmTasks('grunt-contrib-watch'); grunt.loadNpmTasks('grunt-version'); grunt.loadNpmTasks('grunt-shell'); }; diff --git a/index.html b/index.html new file mode 100644 index 0000000..96b0bf0 --- /dev/null +++ b/index.html @@ -0,0 +1,160 @@ + + + + + + jQuery Smooth Scroll Plugin + + + + +
Demo
+

Smooth Scroll Plugin

+

Features

+

$.fn.smoothScroll

+ +

Options

+

The following options, shown with their default values, are available for both $.fn.smoothScroll and $.smoothScroll:

+
{
+  offset: 0,
+
+  // one of 'top' or 'left'
+  direction: 'top',
+
+  // only use if you want to override default behavior
+  scrollTarget: 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: 'swing',
+  speed: 400,
+
+  // coefficient for "auto" speed
+  autoCoefficent: 2
+
+}
+

The options object for $.fn.smoothScroll can take two additional properties: +exclude and excludeWithin. The value for both of these is an array of +selectors, DOM elements or jQuery objects. Default value for both is an +empty array.

+

$.smoothScroll

+ +

Additional Option

+

The following option, in addition to those listed for $.fn.smoothScroll above, is available +for $.smoothScroll:

+
{
+  // jQuery set of elements you wish to scroll.
+  //  if null (default), $('html, body').firstScrollable() is used.
+  scrollElement: null,
+}
+

$.fn.scrollable

+ +

$.fn.firstScrollable

+ +

Notes

+ + + + diff --git a/lib/tmpl/footer.tpl b/lib/tmpl/footer.tpl new file mode 100644 index 0000000..11a09ca --- /dev/null +++ b/lib/tmpl/footer.tpl @@ -0,0 +1,3 @@ + + + diff --git a/lib/tmpl/header.tpl b/lib/tmpl/header.tpl new file mode 100644 index 0000000..392b16f --- /dev/null +++ b/lib/tmpl/header.tpl @@ -0,0 +1,50 @@ + + + + + + jQuery <%= pkg.title %> Plugin + + + + +
Demo
diff --git a/package.json b/package.json index 18e8c14..3ff6cc9 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "grunt-contrib-uglify": "~0.1.1", "grunt-contrib-concat": "~0.1.2", "grunt-shell": "~0.2", - "grunt-version": "~0.1.1" + "grunt-version": "~0.1.1", + "grunt-contrib-watch": "~0.3.1" } } diff --git a/readme.md b/readme.md index a11a1c1..419425d 100644 --- a/readme.md +++ b/readme.md @@ -10,23 +10,42 @@ * Exclude links if they are within a containing element: `$('#container a').smoothScroll({excludeWithin: ['.container2']});` * Exclude links if they match certain conditions: `$('a').smoothScroll({exclude: ['.rough','#chunky']});` * Adjust where the scrolling stops: `$('.backtotop').smoothScroll({offset: -100});` +* Add a callback function that is triggered before the scroll starts: `$('a').smoothScroll({beforeScroll: function() { alert('ready to go!'); }}); * Add a callback function that is triggered after the scroll is complete: `$('a').smoothScroll({afterScroll: function() { alert('we made it!'); }});` -* Add back button support by including a history management plugin such as "Ben Alman's BBQ":http://benalman.com/code/projects/jquery-bbq/docs/files/jquery-ba-bbq-js.html. See demo/bbq.html for an example of how to implement this. +* Add back button support by including a history management plugin such as [Ben Alman's BBQ](http://benalman.com/code/projects/jquery-bbq/docs/files/jquery-ba-bbq-js.html). See [demo/bbq.html](demo/bbq.html) for an example of how to implement this. + #### Options The following options, shown with their default values, are available for both `$.fn.smoothScroll` and `$.smoothScroll`: - { - offset: 0, - direction: 'top', // one of 'top' or 'left' - scrollTarget: null, // only use if you want to override default behavior - afterScroll: null, // function to be called after scrolling occurs. "this" is the triggering element - easing: 'swing', - speed: 400 - } +```javascript +{ + offset: 0, + + // one of 'top' or 'left' + direction: 'top', + + // only use if you want to override default behavior + scrollTarget: 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: 'swing', + speed: 400, + + // coefficient for "auto" speed + autoCoefficent: 2 + +} +``` -The options map for `$.fn.smoothScroll` can take two additional properties: +The options object for `$.fn.smoothScroll` can take two additional properties: `exclude` and `excludeWithin`. The value for both of these is an array of selectors, DOM elements or jQuery objects. Default value for both is an empty array. @@ -40,7 +59,7 @@ empty array. * Doesn't automatically fire, so you need to bind it to some other user interaction. For example: - $('button.scrollsomething').click(function() { + $('button.scrollsomething').on('click', function() { $.smoothScroll({ scrollElement: $('div.scrollme'), scrollTarget: '#findme' @@ -49,20 +68,22 @@ empty array. }); * The `$.smoothScroll` method can take one or two arguments. - * If the first argument is a number, the document is scrolled to that - position. If it's an options map, those options determine how the + * If the first argument is a number, 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. * If a number is provided as the second argument, it will override whatever may have been set for the `scrollTarget` option. #### Additional Option -The following option, in addition to those listed above, is available +The following option, in addition to those listed for `$.fn.smoothScroll` above, is available for `$.smoothScroll`: - { - // jQuery set of elements you wish to scroll. - // if null (default), $('html, body').firstScrollable() is used. - scrollElement: null, - } +```javascript +{ + // jQuery set of elements you wish to scroll. + // if null (default), $('html, body').firstScrollable() is used. + scrollElement: null, +} +``` ### $.fn.scrollable @@ -81,9 +102,14 @@ for `$.smoothScroll`: `$('html, body').firstScrollable().animate({scrollTop: someNumber}, someSpeed)` -## Note +## Notes -* The plugin's `$.fn.smoothScroll` and `$.smoothScroll` methods use the +* To determine where to scroll the page, the `$.fn.smoothScroll` method looks +for an element with an _id_ attribute that matches the `` element's hash. +It does _not_ look at the element's _name_ attribute. If you want a clicked link +to scroll to a "named anchor" (e.g. ``), you'll need to use the +`$.smoothScroll` method instead. +* The plugin's `$.fn.smoothScroll` and `$.smoothScroll` methods use the `$.fn.firstScrollable` 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 From bf530038525d946ce7a4154076c363abfdeb4acf Mon Sep 17 00:00:00 2001 From: Karl Swedberg Date: Sat, 2 Mar 2013 19:23:13 -0500 Subject: [PATCH 02/58] Clean up defaults object comments --- jquery.smooth-scroll.js | 32 +++++++++++++++++++++++--------- jquery.smooth-scroll.min.js | 4 ++-- src/jquery.smooth-scroll.js | 28 +++++++++++++++++++++------- 3 files changed, 46 insertions(+), 18 deletions(-) diff --git a/jquery.smooth-scroll.js b/jquery.smooth-scroll.js index 56c6d2e..c8a6cb1 100644 --- a/jquery.smooth-scroll.js +++ b/jquery.smooth-scroll.js @@ -1,8 +1,8 @@ /*! - * Smooth Scroll - v1.4.10 - 2013-02-20 + * Smooth Scroll - v1.4.10 - 2013-03-02 * https://github.com/kswedberg/jquery-smooth-scroll * Copyright (c) 2013 Karl Swedberg - * Licensed MIT (/blob/master/LICENSE-MIT) + * Licensed MIT (https://github.com/kswedberg/jquery-smooth-scroll/blob/master/LICENSE-MIT) */ (function($) { @@ -12,15 +12,29 @@ var version = '1.4.10', exclude: [], excludeWithin:[], offset: 0, - direction: 'top', // one of 'top' or 'left' - scrollElement: null, // jQuery set of elements you wish to scroll (for $.smoothScroll). - // if null (default), $('html, body').firstScrollable() is used. - scrollTarget: null, // only use if you want to override default behavior - beforeScroll: function() {}, // fn(opts) function to be called before scrolling occurs. "this" is the element(s) being scrolled - afterScroll: function() {}, // fn(opts) function to be called after scrolling occurs. "this" is the triggering element + + // one of 'top' or 'left' + direction: 'top', + + // jQuery set of elements you wish to scroll (for $.smoothScroll). + // if null (default), $('html, body').firstScrollable() is used. + scrollElement: null, + + // only use if you want to override default behavior + scrollTarget: 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: 'swing', speed: 400, - autoCoefficent: 2 // coefficient for "auto" speed + + // coefficient for "auto" speed + autoCoefficent: 2 }, getScrollable = function(opts) { diff --git a/jquery.smooth-scroll.min.js b/jquery.smooth-scroll.min.js index 9c71406..2af596e 100644 --- a/jquery.smooth-scroll.min.js +++ b/jquery.smooth-scroll.min.js @@ -1,7 +1,7 @@ /*! - * Smooth Scroll - v1.4.10 - 2013-02-20 + * Smooth Scroll - v1.4.10 - 2013-03-02 * https://github.com/kswedberg/jquery-smooth-scroll * Copyright (c) 2013 Karl Swedberg - * Licensed MIT (/blob/master/LICENSE-MIT) + * Licensed MIT (https://github.com/kswedberg/jquery-smooth-scroll/blob/master/LICENSE-MIT) */ (function(l){function t(l){return l.replace(/(:|\.)/g,"\\$1")}var e="1.4.10",o={exclude:[],excludeWithin:[],offset:0,direction:"top",scrollElement:null,scrollTarget:null,beforeScroll:function(){},afterScroll:function(){},easing:"swing",speed:400,autoCoefficent:2},r=function(t){var e=[],o=!1,r=t.dir&&"left"==t.dir?"scrollLeft":"scrollTop";return this.each(function(){if(this!=document&&this!=window){var t=l(this);t[r]()>0?e.push(this):(t[r](1),o=t[r]()>0,o&&e.push(this),t[r](0))}}),e.length||this.each(function(){"BODY"===this.nodeName&&(e=[this])}),"first"===t.el&&e.length>1&&(e=[e[0]]),e};l.fn.extend({scrollable:function(l){var t=r.call(this,{dir:l});return this.pushStack(t)},firstScrollable:function(l){var t=r.call(this,{el:"first",dir:l});return this.pushStack(t)},smoothScroll:function(e){e=e||{};var o=l.extend({},l.fn.smoothScroll.defaults,e),r=l.smoothScroll.filterPath(location.pathname);return this.unbind("click.smoothscroll").bind("click.smoothscroll",function(e){var n=this,s=l(this),c=o.exclude,i=o.excludeWithin,a=0,f=0,h=!0,u={},d=location.hostname===n.hostname||!n.hostname,m=o.scrollTarget||(l.smoothScroll.filterPath(n.pathname)||r)===r,p=t(n.hash);if(o.scrollTarget||d&&m&&p){for(;h&&c.length>a;)s.is(t(c[a++]))&&(h=!1);for(;h&&i.length>f;)s.closest(i[f++]).length&&(h=!1)}else h=!1;h&&(e.preventDefault(),l.extend(u,o,{scrollTarget:o.scrollTarget||p,link:n}),l.smoothScroll(u))}),this}}),l.smoothScroll=function(t,e){var o,r,n,s,c=0,i="offset",a="scrollTop",f={},h={};"number"==typeof t?(o=l.fn.smoothScroll.defaults,n=t):(o=l.extend({},l.fn.smoothScroll.defaults,t||{}),o.scrollElement&&(i="position","static"==o.scrollElement.css("position")&&o.scrollElement.css("position","relative"))),o=l.extend({link:null},o),a="left"==o.direction?"scrollLeft":a,o.scrollElement?(r=o.scrollElement,c=r[a]()):r=l("html, body").firstScrollable(),o.beforeScroll.call(r,o),n="number"==typeof t?t:e||l(o.scrollTarget)[i]()&&l(o.scrollTarget)[i]()[o.direction]||0,f[a]=n+c+o.offset,s=o.speed,"auto"===s&&(s=f[a]||r.scrollTop(),s/=o.autoCoefficent),h={duration:s,easing:o.easing,complete:function(){o.afterScroll.call(o.link,o)}},o.step&&(h.step=o.step),r.length?r.stop().animate(f,h):o.afterScroll.call(o.link,o)},l.smoothScroll.version=e,l.smoothScroll.filterPath=function(l){return l.replace(/^\//,"").replace(/(index|default).[a-zA-Z]{3,4}$/,"").replace(/\/$/,"")},l.fn.smoothScroll.defaults=o})(jQuery); \ No newline at end of file diff --git a/src/jquery.smooth-scroll.js b/src/jquery.smooth-scroll.js index 568daf4..f3ab32d 100644 --- a/src/jquery.smooth-scroll.js +++ b/src/jquery.smooth-scroll.js @@ -5,15 +5,29 @@ var version = '1.4.10', exclude: [], excludeWithin:[], offset: 0, - direction: 'top', // one of 'top' or 'left' - scrollElement: null, // jQuery set of elements you wish to scroll (for $.smoothScroll). - // if null (default), $('html, body').firstScrollable() is used. - scrollTarget: null, // only use if you want to override default behavior - beforeScroll: function() {}, // fn(opts) function to be called before scrolling occurs. "this" is the element(s) being scrolled - afterScroll: function() {}, // fn(opts) function to be called after scrolling occurs. "this" is the triggering element + + // one of 'top' or 'left' + direction: 'top', + + // jQuery set of elements you wish to scroll (for $.smoothScroll). + // if null (default), $('html, body').firstScrollable() is used. + scrollElement: null, + + // only use if you want to override default behavior + scrollTarget: 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: 'swing', speed: 400, - autoCoefficent: 2 // coefficient for "auto" speed + + // coefficient for "auto" speed + autoCoefficent: 2 }, getScrollable = function(opts) { From 2e2206dfa4011975b4dc4c3f43be639b6567cc9a Mon Sep 17 00:00:00 2001 From: Karl Swedberg Date: Mon, 15 Jul 2013 16:46:11 -0400 Subject: [PATCH 03/58] Rename (deprecated) component.json to bower.json --- Gruntfile.js | 12 ++++++------ component.json => bower.json | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) rename component.json => bower.json (89%) diff --git a/Gruntfile.js b/Gruntfile.js index 6c9e337..99e7e42 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -7,7 +7,7 @@ module.exports = function(grunt) { // Project configuration. grunt.initConfig({ - component: './component.json', + bower: './bower.json', pkg: grunt.file.readJSON('smooth-scroll.jquery.json'), meta: { banner: '/*!<%= "\\n" %>' + @@ -135,8 +135,8 @@ module.exports = function(grunt) { grunt.registerTask( 'deploy', ['setshell:rsync', 'shell:rsync']); - grunt.registerTask( 'component', 'Update component.json', function() { - var comp = grunt.config('component'), + grunt.registerTask( 'bower', 'Update bower.json', function() { + var comp = grunt.config('bower'), pkgName = grunt.config('pkg').name, pkg = grunt.file.readJSON(pkgName + '.jquery.json'), json = {}; @@ -160,7 +160,7 @@ module.exports = function(grunt) { grunt.log.writeln( "File '" + comp + "' updated." ); }); - grunt.registerTask('docs', function() { + grunt.registerTask('docs', 'Convert readme.md to html and concat with header and footer for index.html', function() { var marked = require('marked'), readme = grunt.file.read('readme.md'), head = grunt.template.process(grunt.file.read('lib/tmpl/header.tpl')), @@ -174,8 +174,8 @@ module.exports = function(grunt) { grunt.file.write('index.html', head + doc + foot); }); - grunt.registerTask('build', ['jshint', 'concat', 'version:same', 'component', 'uglify', 'docs']); - grunt.registerTask('patch', ['jshint', 'concat', 'version:bannerPatch', 'version:patch', 'component', 'uglify']); + grunt.registerTask('build', ['jshint', 'concat', 'version:same', 'bower', 'uglify', 'docs']); + grunt.registerTask('patch', ['jshint', 'concat', 'version:bannerPatch', 'version:patch', 'bower', 'uglify']); grunt.registerTask('default', ['build']); grunt.loadNpmTasks('grunt-contrib-jshint'); diff --git a/component.json b/bower.json similarity index 89% rename from component.json rename to bower.json index d2299fd..cb4b9d2 100644 --- a/component.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "jquery.smooth-scroll", - "version": "1.4.10", + "version": "1.4.11", "dependencies": { "jquery": ">=1.3" }, From eac3d0246cb5b634344b4bd440a3cab726239dc1 Mon Sep 17 00:00:00 2001 From: Karl Swedberg Date: Mon, 15 Jul 2013 16:47:30 -0400 Subject: [PATCH 04/58] Add preventDefault option and bump to 1.4.11. * Closes #44 --- index.html | 5 ++++- jquery.smooth-scroll.js | 14 ++++++++++---- jquery.smooth-scroll.min.js | 4 ++-- package.json | 5 +++-- readme.md | 5 ++++- smooth-scroll.jquery.json | 2 +- src/jquery.smooth-scroll.js | 12 +++++++++--- 7 files changed, 33 insertions(+), 14 deletions(-) diff --git a/index.html b/index.html index 96b0bf0..05a3330 100644 --- a/index.html +++ b/index.html @@ -84,7 +84,10 @@

Options

speed: 400, // coefficient for "auto" speed - autoCoefficent: 2 + autoCoefficent: 2, + + // $.fn.smoothScroll only: whether to prevent the default click action + preventDefault: true }

The options object for $.fn.smoothScroll can take two additional properties: diff --git a/jquery.smooth-scroll.js b/jquery.smooth-scroll.js index c8a6cb1..fe18d1d 100644 --- a/jquery.smooth-scroll.js +++ b/jquery.smooth-scroll.js @@ -1,5 +1,5 @@ /*! - * Smooth Scroll - v1.4.10 - 2013-03-02 + * Smooth Scroll - v1.4.11 - 2013-07-15 * https://github.com/kswedberg/jquery-smooth-scroll * Copyright (c) 2013 Karl Swedberg * Licensed MIT (https://github.com/kswedberg/jquery-smooth-scroll/blob/master/LICENSE-MIT) @@ -7,7 +7,7 @@ (function($) { -var version = '1.4.10', +var version = '1.4.11', defaults = { exclude: [], excludeWithin:[], @@ -34,7 +34,10 @@ var version = '1.4.10', speed: 400, // coefficient for "auto" speed - autoCoefficent: 2 + autoCoefficent: 2, + + // $.fn.smoothScroll only: whether to prevent the default click action + preventDefault: true }, getScrollable = function(opts) { @@ -126,7 +129,10 @@ $.fn.extend({ } if ( include ) { - event.preventDefault(); + + if ( opts.preventDefault ) { + event.preventDefault(); + } $.extend( clickOpts, opts, { scrollTarget: opts.scrollTarget || thisHash, diff --git a/jquery.smooth-scroll.min.js b/jquery.smooth-scroll.min.js index 2af596e..0853d90 100644 --- a/jquery.smooth-scroll.min.js +++ b/jquery.smooth-scroll.min.js @@ -1,7 +1,7 @@ /*! - * Smooth Scroll - v1.4.10 - 2013-03-02 + * Smooth Scroll - v1.4.11 - 2013-07-15 * https://github.com/kswedberg/jquery-smooth-scroll * Copyright (c) 2013 Karl Swedberg * Licensed MIT (https://github.com/kswedberg/jquery-smooth-scroll/blob/master/LICENSE-MIT) */ -(function(l){function t(l){return l.replace(/(:|\.)/g,"\\$1")}var e="1.4.10",o={exclude:[],excludeWithin:[],offset:0,direction:"top",scrollElement:null,scrollTarget:null,beforeScroll:function(){},afterScroll:function(){},easing:"swing",speed:400,autoCoefficent:2},r=function(t){var e=[],o=!1,r=t.dir&&"left"==t.dir?"scrollLeft":"scrollTop";return this.each(function(){if(this!=document&&this!=window){var t=l(this);t[r]()>0?e.push(this):(t[r](1),o=t[r]()>0,o&&e.push(this),t[r](0))}}),e.length||this.each(function(){"BODY"===this.nodeName&&(e=[this])}),"first"===t.el&&e.length>1&&(e=[e[0]]),e};l.fn.extend({scrollable:function(l){var t=r.call(this,{dir:l});return this.pushStack(t)},firstScrollable:function(l){var t=r.call(this,{el:"first",dir:l});return this.pushStack(t)},smoothScroll:function(e){e=e||{};var o=l.extend({},l.fn.smoothScroll.defaults,e),r=l.smoothScroll.filterPath(location.pathname);return this.unbind("click.smoothscroll").bind("click.smoothscroll",function(e){var n=this,s=l(this),c=o.exclude,i=o.excludeWithin,a=0,f=0,h=!0,u={},d=location.hostname===n.hostname||!n.hostname,m=o.scrollTarget||(l.smoothScroll.filterPath(n.pathname)||r)===r,p=t(n.hash);if(o.scrollTarget||d&&m&&p){for(;h&&c.length>a;)s.is(t(c[a++]))&&(h=!1);for(;h&&i.length>f;)s.closest(i[f++]).length&&(h=!1)}else h=!1;h&&(e.preventDefault(),l.extend(u,o,{scrollTarget:o.scrollTarget||p,link:n}),l.smoothScroll(u))}),this}}),l.smoothScroll=function(t,e){var o,r,n,s,c=0,i="offset",a="scrollTop",f={},h={};"number"==typeof t?(o=l.fn.smoothScroll.defaults,n=t):(o=l.extend({},l.fn.smoothScroll.defaults,t||{}),o.scrollElement&&(i="position","static"==o.scrollElement.css("position")&&o.scrollElement.css("position","relative"))),o=l.extend({link:null},o),a="left"==o.direction?"scrollLeft":a,o.scrollElement?(r=o.scrollElement,c=r[a]()):r=l("html, body").firstScrollable(),o.beforeScroll.call(r,o),n="number"==typeof t?t:e||l(o.scrollTarget)[i]()&&l(o.scrollTarget)[i]()[o.direction]||0,f[a]=n+c+o.offset,s=o.speed,"auto"===s&&(s=f[a]||r.scrollTop(),s/=o.autoCoefficent),h={duration:s,easing:o.easing,complete:function(){o.afterScroll.call(o.link,o)}},o.step&&(h.step=o.step),r.length?r.stop().animate(f,h):o.afterScroll.call(o.link,o)},l.smoothScroll.version=e,l.smoothScroll.filterPath=function(l){return l.replace(/^\//,"").replace(/(index|default).[a-zA-Z]{3,4}$/,"").replace(/\/$/,"")},l.fn.smoothScroll.defaults=o})(jQuery); \ No newline at end of file +(function(l){function t(l){return l.replace(/(:|\.)/g,"\\$1")}var e="1.4.11",o={exclude:[],excludeWithin:[],offset:0,direction:"top",scrollElement:null,scrollTarget:null,beforeScroll:function(){},afterScroll:function(){},easing:"swing",speed:400,autoCoefficent:2,preventDefault:!0},r=function(t){var e=[],o=!1,r=t.dir&&"left"==t.dir?"scrollLeft":"scrollTop";return this.each(function(){if(this!=document&&this!=window){var t=l(this);t[r]()>0?e.push(this):(t[r](1),o=t[r]()>0,o&&e.push(this),t[r](0))}}),e.length||this.each(function(){"BODY"===this.nodeName&&(e=[this])}),"first"===t.el&&e.length>1&&(e=[e[0]]),e};l.fn.extend({scrollable:function(l){var t=r.call(this,{dir:l});return this.pushStack(t)},firstScrollable:function(l){var t=r.call(this,{el:"first",dir:l});return this.pushStack(t)},smoothScroll:function(e){e=e||{};var o=l.extend({},l.fn.smoothScroll.defaults,e),r=l.smoothScroll.filterPath(location.pathname);return this.unbind("click.smoothscroll").bind("click.smoothscroll",function(e){var n=this,s=l(this),c=o.exclude,i=o.excludeWithin,a=0,f=0,h=!0,u={},d=location.hostname===n.hostname||!n.hostname,m=o.scrollTarget||(l.smoothScroll.filterPath(n.pathname)||r)===r,p=t(n.hash);if(o.scrollTarget||d&&m&&p){for(;h&&c.length>a;)s.is(t(c[a++]))&&(h=!1);for(;h&&i.length>f;)s.closest(i[f++]).length&&(h=!1)}else h=!1;h&&(o.preventDefault&&e.preventDefault(),l.extend(u,o,{scrollTarget:o.scrollTarget||p,link:n}),l.smoothScroll(u))}),this}}),l.smoothScroll=function(t,e){var o,r,n,s,c=0,i="offset",a="scrollTop",f={},h={};"number"==typeof t?(o=l.fn.smoothScroll.defaults,n=t):(o=l.extend({},l.fn.smoothScroll.defaults,t||{}),o.scrollElement&&(i="position","static"==o.scrollElement.css("position")&&o.scrollElement.css("position","relative"))),o=l.extend({link:null},o),a="left"==o.direction?"scrollLeft":a,o.scrollElement?(r=o.scrollElement,c=r[a]()):r=l("html, body").firstScrollable(),o.beforeScroll.call(r,o),n="number"==typeof t?t:e||l(o.scrollTarget)[i]()&&l(o.scrollTarget)[i]()[o.direction]||0,f[a]=n+c+o.offset,s=o.speed,"auto"===s&&(s=f[a]||r.scrollTop(),s/=o.autoCoefficent),h={duration:s,easing:o.easing,complete:function(){o.afterScroll.call(o.link,o)}},o.step&&(h.step=o.step),r.length?r.stop().animate(f,h):o.afterScroll.call(o.link,o)},l.smoothScroll.version=e,l.smoothScroll.filterPath=function(l){return l.replace(/^\//,"").replace(/(index|default).[a-zA-Z]{3,4}$/,"").replace(/\/$/,"")},l.fn.smoothScroll.defaults=o})(jQuery); \ No newline at end of file diff --git a/package.json b/package.json index 3ff6cc9..f362263 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "smooth-scroll", - "version": "1.4.10", + "version": "1.4.11", "scripts": { "test": "./node_modules/.bin/grunt" }, @@ -13,5 +13,6 @@ "grunt-shell": "~0.2", "grunt-version": "~0.1.1", "grunt-contrib-watch": "~0.3.1" - } + }, + "readmeFilename": "readme.md" } diff --git a/readme.md b/readme.md index 419425d..5fe4138 100644 --- a/readme.md +++ b/readme.md @@ -40,7 +40,10 @@ The following options, shown with their default values, are available for both ` speed: 400, // coefficient for "auto" speed - autoCoefficent: 2 + autoCoefficent: 2, + + // $.fn.smoothScroll only: whether to prevent the default click action + preventDefault: true } ``` diff --git a/smooth-scroll.jquery.json b/smooth-scroll.jquery.json index 873404e..c52eac0 100644 --- a/smooth-scroll.jquery.json +++ b/smooth-scroll.jquery.json @@ -1,6 +1,6 @@ { "name": "smooth-scroll", - "version": "1.4.10", + "version": "1.4.11", "title": "Smooth Scroll", "description": "Easy implementation of smooth scrolling for same-page links", "author": { diff --git a/src/jquery.smooth-scroll.js b/src/jquery.smooth-scroll.js index f3ab32d..9b89395 100644 --- a/src/jquery.smooth-scroll.js +++ b/src/jquery.smooth-scroll.js @@ -1,6 +1,6 @@ (function($) { -var version = '1.4.10', +var version = '1.4.11', defaults = { exclude: [], excludeWithin:[], @@ -27,7 +27,10 @@ var version = '1.4.10', speed: 400, // coefficient for "auto" speed - autoCoefficent: 2 + autoCoefficent: 2, + + // $.fn.smoothScroll only: whether to prevent the default click action + preventDefault: true }, getScrollable = function(opts) { @@ -119,7 +122,10 @@ $.fn.extend({ } if ( include ) { - event.preventDefault(); + + if ( opts.preventDefault ) { + event.preventDefault(); + } $.extend( clickOpts, opts, { scrollTarget: opts.scrollTarget || thisHash, From 38a975a554c3459c5d93aec63839cd669d6d0e3f Mon Sep 17 00:00:00 2001 From: Karl Swedberg Date: Tue, 16 Jul 2013 09:54:03 -0400 Subject: [PATCH 05/58] Add syntax highlighting for index.html --- Gruntfile.js | 18 +++++++++---- index.html | 65 +++++++++++++++++++-------------------------- lib/tmpl/header.tpl | 24 +++++++++++++++++ package.json | 3 ++- 4 files changed, 66 insertions(+), 44 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 99e7e42..4999080 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -4,6 +4,18 @@ module.exports = function(grunt) { // Because I'm lazy var _ = grunt.util._; + var marked = require('marked'); + // var hl = require('highlight').Highlight; + var hl = require('node-syntaxhighlighter'); + marked.setOptions({ + highlight: function(code, lang) { + lang = lang || 'javascript'; + lang = hl.getLanguage(lang); + + return hl.highlight(code, lang); + }, + gfm: true + }); // Project configuration. grunt.initConfig({ @@ -161,15 +173,11 @@ module.exports = function(grunt) { }); grunt.registerTask('docs', 'Convert readme.md to html and concat with header and footer for index.html', function() { - var marked = require('marked'), - readme = grunt.file.read('readme.md'), + var readme = grunt.file.read('readme.md'), head = grunt.template.process(grunt.file.read('lib/tmpl/header.tpl')), foot = grunt.file.read('lib/tmpl/footer.tpl'), doc = marked(readme); - marked.setOptions({ - gfm: true - }); grunt.file.write('index.html', head + doc + foot); }); diff --git a/index.html b/index.html index 05a3330..54aced3 100644 --- a/index.html +++ b/index.html @@ -32,6 +32,30 @@ 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; @@ -64,32 +88,7 @@

$.fn.smoothScroll

Options

The following options, shown with their default values, are available for both $.fn.smoothScroll and $.smoothScroll:

-
{
-  offset: 0,
-
-  // one of 'top' or 'left'
-  direction: 'top',
-
-  // only use if you want to override default behavior
-  scrollTarget: 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: 'swing',
-  speed: 400,
-
-  // coefficient for "auto" speed
-  autoCoefficent: 2,
-
-  // $.fn.smoothScroll only: whether to prevent the default click action
-  preventDefault: true
-
-}
+
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
{
  offset: 0,
 
  // one of 'top' or 'left'
  direction: 'top',
 
  // only use if you want to override default behavior
  scrollTarget: 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: 'swing',
  speed: 400,
 
  // coefficient for "auto" speed
  autoCoefficent: 2,
 
  // $.fn.smoothScroll only: whether to prevent the default click action
  preventDefault: true
 
}

The options object for $.fn.smoothScroll can take two additional properties: exclude and excludeWithin. The value for both of these is an array of selectors, DOM elements or jQuery objects. Default value for both is an @@ -101,13 +100,7 @@

$.smoothScroll

document.body)
  • Doesn't automatically fire, so you need to bind it to some other user interaction. For example:

    -
      $('button.scrollsomething').on('click', function() {
    -    $.smoothScroll({
    -      scrollElement: $('div.scrollme'),
    -      scrollTarget: '#findme'
    -    });
    -    return false;
    -  });
    +
    1
    2
    3
    4
    5
    6
    7
    $('button.scrollsomething').on('click', function() {
      $.smoothScroll({
        scrollElement: $('div.scrollme'),
        scrollTarget: '#findme'
      });
      return false;
    });
  • The $.smoothScroll method can take one or two arguments.

    Or, try it with hashchange support

    +

    Toggle scrolling speed for the p1 link

    diff --git a/src/jquery.smooth-scroll.js b/src/jquery.smooth-scroll.js index e270ed5..df6b19d 100644 --- a/src/jquery.smooth-scroll.js +++ b/src/jquery.smooth-scroll.js @@ -1,5 +1,6 @@ (function($) { var version = '1.4.12', + optionOverrides = {}, defaults = { exclude: [], excludeWithin:[], @@ -86,8 +87,21 @@ $.fn.extend({ return this.pushStack(scrl); }, - smoothScroll: function(options) { + smoothScroll: function(options, extra) { options = options || {}; + + if ( options === 'options' ) { + if ( !extra ) { + return this.first().data('ssOpts'); + } + return this.each(function() { + var $this = $(this), + opts = $.extend($this.data('ssOpts') || {}, extra); + + $(this).data('ssOpts', opts); + }); + } + var opts = $.extend({}, $.fn.smoothScroll.defaults, options), locationPath = $.smoothScroll.filterPath(location.pathname); @@ -96,16 +110,17 @@ $.fn.extend({ .bind('click.smoothscroll', function(event) { var link = this, $link = $(this), + thisOpts = $.extend({}, opts, $link.data('ssOpts') || {}), exclude = opts.exclude, - excludeWithin = opts.excludeWithin, + excludeWithin = thisOpts.excludeWithin, elCounter = 0, ewlCounter = 0, include = true, clickOpts = {}, hostMatch = ((location.hostname === link.hostname) || !link.hostname), - pathMatch = opts.scrollTarget || ( $.smoothScroll.filterPath(link.pathname) || locationPath ) === locationPath, + pathMatch = thisOpts.scrollTarget || ( $.smoothScroll.filterPath(link.pathname) || locationPath ) === locationPath, thisHash = escapeSelector(link.hash); - if ( !opts.scrollTarget && (!hostMatch || !pathMatch || !thisHash) ) { + if ( !thisOpts.scrollTarget && (!hostMatch || !pathMatch || !thisHash) ) { include = false; } else { while (include && elCounter < exclude.length) { @@ -122,15 +137,14 @@ $.fn.extend({ if ( include ) { - if ( opts.preventDefault ) { + if ( thisOpts.preventDefault ) { event.preventDefault(); } - $.extend( clickOpts, opts, { - scrollTarget: opts.scrollTarget || thisHash, + $.extend( clickOpts, thisOpts, { + scrollTarget: thisOpts.scrollTarget || thisHash, link: link }); - $.smoothScroll( clickOpts ); } }); @@ -140,6 +154,9 @@ $.fn.extend({ }); $.smoothScroll = function(options, px) { + if ( options === 'options' && typeof px === 'object' ) { + return $.extend(optionOverrides, px); + } var opts, $scroller, scrollTargetOffset, speed, scrollerOffset = 0, offPos = 'offset', @@ -148,12 +165,11 @@ $.smoothScroll = function(options, px) { aniOpts = {}, scrollprops = []; - if (typeof options === 'number') { - opts = $.fn.smoothScroll.defaults; + opts = $.extend({link: null}, $.fn.smoothScroll.defaults, optionOverrides); scrollTargetOffset = options; } else { - opts = $.extend({}, $.fn.smoothScroll.defaults, options || {}); + opts = $.extend({link: null}, $.fn.smoothScroll.defaults, options || {}, optionOverrides); if (opts.scrollElement) { offPos = 'position'; if (opts.scrollElement.css('position') == 'static') { @@ -162,7 +178,6 @@ $.smoothScroll = function(options, px) { } } - opts = $.extend({link: null}, opts); scrollDir = opts.direction == 'left' ? 'scrollLeft' : scrollDir; if ( opts.scrollElement ) { From ffdc2200f41cc02b38f0434a98676d0f97972137 Mon Sep 17 00:00:00 2001 From: Karl Swedberg Date: Sat, 2 Nov 2013 18:13:15 -0400 Subject: [PATCH 10/58] Build files and bump version --- Gruntfile.js | 4 +++- bower.json | 2 +- jquery.smooth-scroll.js | 44 +++++++++++++++++++++++++------------ jquery.smooth-scroll.min.js | 4 ++-- package.json | 2 +- smooth-scroll.jquery.json | 2 +- src/jquery.smooth-scroll.js | 2 +- 7 files changed, 39 insertions(+), 21 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 4999080..08b18d3 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -67,7 +67,9 @@ module.exports = function(grunt) { shell: { rsync: { // command is set by setshell:rsync. - stdout: true + options: { + stdout: true + } } }, setshell: { diff --git a/bower.json b/bower.json index 4f80b4c..ab25067 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "jquery.smooth-scroll", - "version": "1.4.12", + "version": "1.4.13", "dependencies": { "jquery": ">=1.3" }, diff --git a/jquery.smooth-scroll.js b/jquery.smooth-scroll.js index 0ba2abc..7d527c3 100644 --- a/jquery.smooth-scroll.js +++ b/jquery.smooth-scroll.js @@ -1,12 +1,13 @@ /*! - * Smooth Scroll - v1.4.12 - 2013-09-19 + * Smooth Scroll - v1.4.13 - 2013-11-02 * https://github.com/kswedberg/jquery-smooth-scroll * Copyright (c) 2013 Karl Swedberg * Licensed MIT (https://github.com/kswedberg/jquery-smooth-scroll/blob/master/LICENSE-MIT) */ (function($) { -var version = '1.4.12', +var version = '1.4.13', + optionOverrides = {}, defaults = { exclude: [], excludeWithin:[], @@ -43,6 +44,7 @@ var version = '1.4.12', var scrollable = [], scrolled = false, dir = opts.dir && opts.dir == 'left' ? 'scrollLeft' : 'scrollTop'; + this.each(function() { if (this == document || this == window) { return; } @@ -92,8 +94,21 @@ $.fn.extend({ return this.pushStack(scrl); }, - smoothScroll: function(options) { + smoothScroll: function(options, extra) { options = options || {}; + + if ( options === 'options' ) { + if ( !extra ) { + return this.first().data('ssOpts'); + } + return this.each(function() { + var $this = $(this), + opts = $.extend($this.data('ssOpts') || {}, extra); + + $(this).data('ssOpts', opts); + }); + } + var opts = $.extend({}, $.fn.smoothScroll.defaults, options), locationPath = $.smoothScroll.filterPath(location.pathname); @@ -102,16 +117,17 @@ $.fn.extend({ .bind('click.smoothscroll', function(event) { var link = this, $link = $(this), + thisOpts = $.extend({}, opts, $link.data('ssOpts') || {}), exclude = opts.exclude, - excludeWithin = opts.excludeWithin, + excludeWithin = thisOpts.excludeWithin, elCounter = 0, ewlCounter = 0, include = true, clickOpts = {}, hostMatch = ((location.hostname === link.hostname) || !link.hostname), - pathMatch = opts.scrollTarget || ( $.smoothScroll.filterPath(link.pathname) || locationPath ) === locationPath, + pathMatch = thisOpts.scrollTarget || ( $.smoothScroll.filterPath(link.pathname) || locationPath ) === locationPath, thisHash = escapeSelector(link.hash); - if ( !opts.scrollTarget && (!hostMatch || !pathMatch || !thisHash) ) { + if ( !thisOpts.scrollTarget && (!hostMatch || !pathMatch || !thisHash) ) { include = false; } else { while (include && elCounter < exclude.length) { @@ -128,15 +144,14 @@ $.fn.extend({ if ( include ) { - if ( opts.preventDefault ) { + if ( thisOpts.preventDefault ) { event.preventDefault(); } - $.extend( clickOpts, opts, { - scrollTarget: opts.scrollTarget || thisHash, + $.extend( clickOpts, thisOpts, { + scrollTarget: thisOpts.scrollTarget || thisHash, link: link }); - $.smoothScroll( clickOpts ); } }); @@ -146,6 +161,9 @@ $.fn.extend({ }); $.smoothScroll = function(options, px) { + if ( options === 'options' && typeof px === 'object' ) { + return $.extend(optionOverrides, px); + } var opts, $scroller, scrollTargetOffset, speed, scrollerOffset = 0, offPos = 'offset', @@ -154,12 +172,11 @@ $.smoothScroll = function(options, px) { aniOpts = {}, scrollprops = []; - if (typeof options === 'number') { - opts = $.fn.smoothScroll.defaults; + opts = $.extend({link: null}, $.fn.smoothScroll.defaults, optionOverrides); scrollTargetOffset = options; } else { - opts = $.extend({}, $.fn.smoothScroll.defaults, options || {}); + opts = $.extend({link: null}, $.fn.smoothScroll.defaults, options || {}, optionOverrides); if (opts.scrollElement) { offPos = 'position'; if (opts.scrollElement.css('position') == 'static') { @@ -168,7 +185,6 @@ $.smoothScroll = function(options, px) { } } - opts = $.extend({link: null}, opts); scrollDir = opts.direction == 'left' ? 'scrollLeft' : scrollDir; if ( opts.scrollElement ) { diff --git a/jquery.smooth-scroll.min.js b/jquery.smooth-scroll.min.js index d734d41..e0cf64a 100644 --- a/jquery.smooth-scroll.min.js +++ b/jquery.smooth-scroll.min.js @@ -1,7 +1,7 @@ /*! - * Smooth Scroll - v1.4.12 - 2013-09-19 + * Smooth Scroll - v1.4.13 - 2013-11-02 * https://github.com/kswedberg/jquery-smooth-scroll * Copyright (c) 2013 Karl Swedberg * Licensed MIT (https://github.com/kswedberg/jquery-smooth-scroll/blob/master/LICENSE-MIT) */ -(function(t){function l(t){return t.replace(/(:|\.)/g,"\\$1")}var e="1.4.12",o={exclude:[],excludeWithin:[],offset:0,direction:"top",scrollElement:null,scrollTarget:null,beforeScroll:function(){},afterScroll:function(){},easing:"swing",speed:400,autoCoefficent:2,preventDefault:!0},n=function(l){var e=[],o=!1,n=l.dir&&"left"==l.dir?"scrollLeft":"scrollTop";return this.each(function(){if(this!=document&&this!=window){var l=t(this);l[n]()>0?e.push(this):(l[n](1),o=l[n]()>0,o&&e.push(this),l[n](0))}}),e.length||this.each(function(){"BODY"===this.nodeName&&(e=[this])}),"first"===l.el&&e.length>1&&(e=[e[0]]),e};t.fn.extend({scrollable:function(t){var l=n.call(this,{dir:t});return this.pushStack(l)},firstScrollable:function(t){var l=n.call(this,{el:"first",dir:t});return this.pushStack(l)},smoothScroll:function(e){e=e||{};var o=t.extend({},t.fn.smoothScroll.defaults,e),n=t.smoothScroll.filterPath(location.pathname);return this.unbind("click.smoothscroll").bind("click.smoothscroll",function(e){var r=this,s=t(this),c=o.exclude,i=o.excludeWithin,a=0,f=0,h=!0,u={},d=location.hostname===r.hostname||!r.hostname,m=o.scrollTarget||(t.smoothScroll.filterPath(r.pathname)||n)===n,p=l(r.hash);if(o.scrollTarget||d&&m&&p){for(;h&&c.length>a;)s.is(l(c[a++]))&&(h=!1);for(;h&&i.length>f;)s.closest(i[f++]).length&&(h=!1)}else h=!1;h&&(o.preventDefault&&e.preventDefault(),t.extend(u,o,{scrollTarget:o.scrollTarget||p,link:r}),t.smoothScroll(u))}),this}}),t.smoothScroll=function(l,e){var o,n,r,s,c=0,i="offset",a="scrollTop",f={},h={};"number"==typeof l?(o=t.fn.smoothScroll.defaults,r=l):(o=t.extend({},t.fn.smoothScroll.defaults,l||{}),o.scrollElement&&(i="position","static"==o.scrollElement.css("position")&&o.scrollElement.css("position","relative"))),o=t.extend({link:null},o),a="left"==o.direction?"scrollLeft":a,o.scrollElement?(n=o.scrollElement,/^(?:HTML|BODY)$/.test(n[0].nodeName)||(c=n[a]())):n=t("html, body").firstScrollable(o.direction),o.beforeScroll.call(n,o),r="number"==typeof l?l:e||t(o.scrollTarget)[i]()&&t(o.scrollTarget)[i]()[o.direction]||0,f[a]=r+c+o.offset,s=o.speed,"auto"===s&&(s=f[a]||n.scrollTop(),s/=o.autoCoefficent),h={duration:s,easing:o.easing,complete:function(){o.afterScroll.call(o.link,o)}},o.step&&(h.step=o.step),n.length?n.stop().animate(f,h):o.afterScroll.call(o.link,o)},t.smoothScroll.version=e,t.smoothScroll.filterPath=function(t){return t.replace(/^\//,"").replace(/(?:index|default).[a-zA-Z]{3,4}$/,"").replace(/\/$/,"")},t.fn.smoothScroll.defaults=o})(jQuery); \ No newline at end of file +(function(t){function e(t){return t.replace(/(:|\.)/g,"\\$1")}var l="1.4.13",o={},s={exclude:[],excludeWithin:[],offset:0,direction:"top",scrollElement:null,scrollTarget:null,beforeScroll:function(){},afterScroll:function(){},easing:"swing",speed:400,autoCoefficent:2,preventDefault:!0},n=function(e){var l=[],o=!1,s=e.dir&&"left"==e.dir?"scrollLeft":"scrollTop";return this.each(function(){if(this!=document&&this!=window){var e=t(this);e[s]()>0?l.push(this):(e[s](1),o=e[s]()>0,o&&l.push(this),e[s](0))}}),l.length||this.each(function(){"BODY"===this.nodeName&&(l=[this])}),"first"===e.el&&l.length>1&&(l=[l[0]]),l};t.fn.extend({scrollable:function(t){var e=n.call(this,{dir:t});return this.pushStack(e)},firstScrollable:function(t){var e=n.call(this,{el:"first",dir:t});return this.pushStack(e)},smoothScroll:function(l,o){if(l=l||{},"options"===l)return o?this.each(function(){var e=t(this),l=t.extend(e.data("ssOpts")||{},o);t(this).data("ssOpts",l)}):this.first().data("ssOpts");var s=t.extend({},t.fn.smoothScroll.defaults,l),n=t.smoothScroll.filterPath(location.pathname);return this.unbind("click.smoothscroll").bind("click.smoothscroll",function(l){var o=this,r=t(this),i=t.extend({},s,r.data("ssOpts")||{}),c=s.exclude,a=i.excludeWithin,f=0,h=0,u=!0,d={},p=location.hostname===o.hostname||!o.hostname,m=i.scrollTarget||(t.smoothScroll.filterPath(o.pathname)||n)===n,S=e(o.hash);if(i.scrollTarget||p&&m&&S){for(;u&&c.length>f;)r.is(e(c[f++]))&&(u=!1);for(;u&&a.length>h;)r.closest(a[h++]).length&&(u=!1)}else u=!1;u&&(i.preventDefault&&l.preventDefault(),t.extend(d,i,{scrollTarget:i.scrollTarget||S,link:o}),t.smoothScroll(d))}),this}}),t.smoothScroll=function(e,l){if("options"===e&&"object"==typeof l)return t.extend(o,l);var s,n,r,i,c=0,a="offset",f="scrollTop",h={},u={};"number"==typeof e?(s=t.extend({link:null},t.fn.smoothScroll.defaults,o),r=e):(s=t.extend({link:null},t.fn.smoothScroll.defaults,e||{},o),s.scrollElement&&(a="position","static"==s.scrollElement.css("position")&&s.scrollElement.css("position","relative"))),f="left"==s.direction?"scrollLeft":f,s.scrollElement?(n=s.scrollElement,/^(?:HTML|BODY)$/.test(n[0].nodeName)||(c=n[f]())):n=t("html, body").firstScrollable(s.direction),s.beforeScroll.call(n,s),r="number"==typeof e?e:l||t(s.scrollTarget)[a]()&&t(s.scrollTarget)[a]()[s.direction]||0,h[f]=r+c+s.offset,i=s.speed,"auto"===i&&(i=h[f]||n.scrollTop(),i/=s.autoCoefficent),u={duration:i,easing:s.easing,complete:function(){s.afterScroll.call(s.link,s)}},s.step&&(u.step=s.step),n.length?n.stop().animate(h,u):s.afterScroll.call(s.link,s)},t.smoothScroll.version=l,t.smoothScroll.filterPath=function(t){return t.replace(/^\//,"").replace(/(?:index|default).[a-zA-Z]{3,4}$/,"").replace(/\/$/,"")},t.fn.smoothScroll.defaults=s})(jQuery); \ No newline at end of file diff --git a/package.json b/package.json index a09204d..c0aafbf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "smooth-scroll", - "version": "1.4.12", + "version": "1.4.13", "scripts": { "test": "./node_modules/.bin/grunt" }, diff --git a/smooth-scroll.jquery.json b/smooth-scroll.jquery.json index 2e9731f..723226c 100644 --- a/smooth-scroll.jquery.json +++ b/smooth-scroll.jquery.json @@ -1,6 +1,6 @@ { "name": "smooth-scroll", - "version": "1.4.12", + "version": "1.4.13", "title": "Smooth Scroll", "description": "Easy implementation of smooth scrolling for same-page links", "author": { diff --git a/src/jquery.smooth-scroll.js b/src/jquery.smooth-scroll.js index df6b19d..b8965c9 100644 --- a/src/jquery.smooth-scroll.js +++ b/src/jquery.smooth-scroll.js @@ -1,5 +1,5 @@ (function($) { -var version = '1.4.12', +var version = '1.4.13', optionOverrides = {}, defaults = { exclude: [], From 037a27be10df3d862a8c42cbba1a72a20ae57626 Mon Sep 17 00:00:00 2001 From: Karl Swedberg Date: Fri, 21 Feb 2014 21:49:00 -0500 Subject: [PATCH 11/58] Update jquery.js --- demo/bbq-fixed.html | 3 +- demo/bbq.html | 3 +- demo/fixed-fix.html | 4 +- demo/fixed.html | 4 +- demo/index.html | 3 +- lib/jquery/jquery.js | 14251 ++++++++++++++++++++++------------------- 6 files changed, 7597 insertions(+), 6671 deletions(-) diff --git a/demo/bbq-fixed.html b/demo/bbq-fixed.html index 4a657e3..2bb41b1 100644 --- a/demo/bbq-fixed.html +++ b/demo/bbq-fixed.html @@ -46,8 +46,7 @@

    Smooth Scroll jQuery Plugin with Back Button Support

    - - + - + - - + - - + - + + + + + + diff --git a/test/tests.js b/test/tests.js new file mode 100644 index 0000000..321cc49 --- /dev/null +++ b/test/tests.js @@ -0,0 +1,24 @@ +/* globals QUnit: false */ + +QUnit.module('filterPath'); + +QUnit.test( 'Link paths not location path', function( assert ) { + var locationPath = $.smoothScroll.filterPath(location.pathname); + $('a[href*=#]').each(function() { + assert.notEqual(locationPath, this.pathname); + }); +}); + +QUnit.module('scrollable'); + +QUnit.test( 'Returns first scrollable element (html,body)', function( assert ) { + var scrollable = $('html,body').firstScrollable(); + assert.equal(scrollable.length, 1, 'One scrollable element is returned'); + assert.equal(scrollable[0], document.body, 'Scrollable element is '); +}); + +QUnit.test( 'Returns scrollable element (div#scrollable)', function( assert ) { + var scrollable = $('#scrollable').scrollable(); + assert.equal(scrollable.length, 1, 'One scrollable element is returned'); + assert.equal(scrollable[0].id, 'scrollable', 'Scrollable element is
    '); +}); From f2b800cec21e9f0f56880e81088d071daa0bc3c6 Mon Sep 17 00:00:00 2001 From: Karl Swedberg Date: Tue, 8 Sep 2015 12:16:09 -0400 Subject: [PATCH 34/58] Remove smooth-scroll.jquery.json. No longer used. --- Gruntfile.js | 21 ++------------------- smooth-scroll.jquery.json | 33 --------------------------------- 2 files changed, 2 insertions(+), 52 deletions(-) delete mode 100644 smooth-scroll.jquery.json diff --git a/Gruntfile.js b/Gruntfile.js index 6e3e44e..6a81bcf 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -123,33 +123,16 @@ module.exports = function(grunt) { var pkg = grunt.file.readJSON('package.json'), pkgBasename = grunt.config('pluginName'), bowerFile = grunt.config('bower'), - bower = grunt.file.readJSON(bowerFile), - jqConfigFile = pkgBasename + '.jquery.json', - jqConfig = grunt.file.readJSON(jqConfigFile); + bower = grunt.file.readJSON(bowerFile); - ['main', 'version', 'dependencies', 'keywords'].forEach(function(el) { + ['main', 'dependencies', 'keywords'].forEach(function(el) { bower[el] = pkg[el]; - jqConfig[el] = pkg[el]; }); - ['author', 'repository', 'homepage', 'docs', 'bugs', 'demo', 'licenses'].forEach(function(el) { - jqConfig[el] = pkg[el]; - }); - - jqConfig.keywords.shift(); - - jqConfig.name = pkgBasename; bower.name = 'jquery-' + pkgBasename; grunt.file.write( bowerFile, JSON.stringify(bower, null, 2) + '\n'); grunt.log.writeln( 'File "' + bowerFile + '" updated."' ); - - while ( /jquery/i.test(jqConfig.keywords[0]) ) { - jqConfig.keywords.shift(); - } - - grunt.file.write( jqConfigFile, JSON.stringify(jqConfig, null, 2) + '\n'); - grunt.log.writeln( 'File "' + jqConfigFile + '" updated."' ); }); grunt.registerTask('docs', 'Convert readme.md to html and concat with header and footer for index.html', function() { diff --git a/smooth-scroll.jquery.json b/smooth-scroll.jquery.json deleted file mode 100644 index bffc110..0000000 --- a/smooth-scroll.jquery.json +++ /dev/null @@ -1,33 +0,0 @@ -{ - "name": "smooth-scroll", - "version": "1.5.5", - "title": "Smooth Scroll", - "description": "Easy implementation of smooth scrolling for same-page links", - "author": { - "name": "Karl Swedberg", - "email": "kswedberg@gmail.com", - "url": "http://www.learningjquery.com/" - }, - "repository": { - "type": "git", - "url": "https://github.com/kswedberg/jquery-smooth-scroll" - }, - "homepage": "https://github.com/kswedberg/jquery-smooth-scroll", - "docs": "https://github.com/kswedberg/jquery-smooth-scroll", - "bugs": "https://github.com/kswedberg/jquery-smooth-scroll/issues", - "demo": "http://plugins.learningjquery.com/smooth-scroll/demo/", - "licenses": [ - { - "type": "MIT", - "url": "https://github.com/kswedberg/jquery-smooth-scroll/blob/master/LICENSE-MIT" - } - ], - "dependencies": { - "jquery": ">=1.3" - }, - "keywords": [ - "scroll", - "animation" - ], - "main": "jquery.smooth-scroll.js" -} From 6478c4367926bf17cc9ac4e86fb22374659eb30f Mon Sep 17 00:00:00 2001 From: Karl Swedberg Date: Tue, 8 Sep 2015 12:18:21 -0400 Subject: [PATCH 35/58] Add check for `document.scrollingElement` * See https://dev.opera.com/articles/fixing-the-scrolltop-bug/ --- jquery.smooth-scroll.js | 17 +++++++++++++---- jquery.smooth-scroll.min.js | 4 ++-- package.json | 2 +- src/jquery.smooth-scroll.js | 15 ++++++++++++--- 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/jquery.smooth-scroll.js b/jquery.smooth-scroll.js index 0ecc899..e95bb36 100644 --- a/jquery.smooth-scroll.js +++ b/jquery.smooth-scroll.js @@ -1,5 +1,5 @@ /*! - * jQuery Smooth Scroll - v1.5.5 - 2015-02-19 + * jQuery Smooth Scroll - v1.5.6 - 2015-09-08 * https://github.com/kswedberg/jquery-smooth-scroll * Copyright (c) 2015 Karl Swedberg * Licensed MIT (https://github.com/kswedberg/jquery-smooth-scroll/blob/master/LICENSE-MIT) @@ -18,7 +18,7 @@ } }(function ($) { - var version = '1.5.5', + var version = '1.5.6', optionOverrides = {}, defaults = { exclude: [], @@ -58,9 +58,18 @@ dir = opts.dir && opts.dir === 'left' ? 'scrollLeft' : 'scrollTop'; this.each(function() { - - if (this === document || this === window) { return; } var el = $(this); + + if (this === document || this === window) { + return; + } + + if ( document.scrollingElement && (this === document.documentElement || this === document.body) ) { + scrollable.push(document.scrollingElement); + + return false; + } + if ( el[dir]() > 0 ) { scrollable.push(this); } else { diff --git a/jquery.smooth-scroll.min.js b/jquery.smooth-scroll.min.js index a2b55a5..2e70391 100644 --- a/jquery.smooth-scroll.min.js +++ b/jquery.smooth-scroll.min.js @@ -1,7 +1,7 @@ /*! - * jQuery Smooth Scroll - v1.5.5 - 2015-02-19 + * jQuery Smooth Scroll - v1.5.6 - 2015-09-08 * https://github.com/kswedberg/jquery-smooth-scroll * Copyright (c) 2015 Karl Swedberg * Licensed MIT (https://github.com/kswedberg/jquery-smooth-scroll/blob/master/LICENSE-MIT) */ -(function(t){"function"==typeof define&&define.amd?define(["jquery"],t):"object"==typeof module&&module.exports?t(require("jquery")):t(jQuery)})(function(t){function e(t){return t.replace(/(:|\.|\/)/g,"\\$1")}var l="1.5.5",o={},n={exclude:[],excludeWithin:[],offset:0,direction:"top",scrollElement:null,scrollTarget:null,beforeScroll:function(){},afterScroll:function(){},easing:"swing",speed:400,autoCoefficient:2,preventDefault:!0},s=function(e){var l=[],o=!1,n=e.dir&&"left"===e.dir?"scrollLeft":"scrollTop";return this.each(function(){if(this!==document&&this!==window){var e=t(this);e[n]()>0?l.push(this):(e[n](1),o=e[n]()>0,o&&l.push(this),e[n](0))}}),l.length||this.each(function(){"BODY"===this.nodeName&&(l=[this])}),"first"===e.el&&l.length>1&&(l=[l[0]]),l};t.fn.extend({scrollable:function(t){var e=s.call(this,{dir:t});return this.pushStack(e)},firstScrollable:function(t){var e=s.call(this,{el:"first",dir:t});return this.pushStack(e)},smoothScroll:function(l,o){if(l=l||{},"options"===l)return o?this.each(function(){var e=t(this),l=t.extend(e.data("ssOpts")||{},o);t(this).data("ssOpts",l)}):this.first().data("ssOpts");var n=t.extend({},t.fn.smoothScroll.defaults,l),s=t.smoothScroll.filterPath(location.pathname);return this.unbind("click.smoothscroll").bind("click.smoothscroll",function(l){var o=this,r=t(this),i=t.extend({},n,r.data("ssOpts")||{}),c=n.exclude,a=i.excludeWithin,f=0,h=0,u=!0,d={},p=location.hostname===o.hostname||!o.hostname,m=i.scrollTarget||t.smoothScroll.filterPath(o.pathname)===s,S=e(o.hash);if(i.scrollTarget||p&&m&&S){for(;u&&c.length>f;)r.is(e(c[f++]))&&(u=!1);for(;u&&a.length>h;)r.closest(a[h++]).length&&(u=!1)}else u=!1;u&&(i.preventDefault&&l.preventDefault(),t.extend(d,i,{scrollTarget:i.scrollTarget||S,link:o}),t.smoothScroll(d))}),this}}),t.smoothScroll=function(e,l){if("options"===e&&"object"==typeof l)return t.extend(o,l);var n,s,r,i,c,a=0,f="offset",h="scrollTop",u={},d={};"number"==typeof e?(n=t.extend({link:null},t.fn.smoothScroll.defaults,o),r=e):(n=t.extend({link:null},t.fn.smoothScroll.defaults,e||{},o),n.scrollElement&&(f="position","static"===n.scrollElement.css("position")&&n.scrollElement.css("position","relative"))),h="left"===n.direction?"scrollLeft":h,n.scrollElement?(s=n.scrollElement,/^(?:HTML|BODY)$/.test(s[0].nodeName)||(a=s[h]())):s=t("html, body").firstScrollable(n.direction),n.beforeScroll.call(s,n),r="number"==typeof e?e:l||t(n.scrollTarget)[f]()&&t(n.scrollTarget)[f]()[n.direction]||0,u[h]=r+a+n.offset,i=n.speed,"auto"===i&&(c=u[h]-s.scrollTop(),0>c&&(c*=-1),i=c/n.autoCoefficient),d={duration:i,easing:n.easing,complete:function(){n.afterScroll.call(n.link,n)}},n.step&&(d.step=n.step),s.length?s.stop().animate(u,d):n.afterScroll.call(n.link,n)},t.smoothScroll.version=l,t.smoothScroll.filterPath=function(t){return t=t||"",t.replace(/^\//,"").replace(/(?:index|default).[a-zA-Z]{3,4}$/,"").replace(/\/$/,"")},t.fn.smoothScroll.defaults=n}); \ No newline at end of file +(function(t){"function"==typeof define&&define.amd?define(["jquery"],t):"object"==typeof module&&module.exports?t(require("jquery")):t(jQuery)})(function(t){function e(t){return t.replace(/(:|\.|\/)/g,"\\$1")}var l="1.5.6",o={},n={exclude:[],excludeWithin:[],offset:0,direction:"top",scrollElement:null,scrollTarget:null,beforeScroll:function(){},afterScroll:function(){},easing:"swing",speed:400,autoCoefficient:2,preventDefault:!0},s=function(e){var l=[],o=!1,n=e.dir&&"left"===e.dir?"scrollLeft":"scrollTop";return this.each(function(){var e=t(this);if(this!==document&&this!==window)return!document.scrollingElement||this!==document.documentElement&&this!==document.body?(e[n]()>0?l.push(this):(e[n](1),o=e[n]()>0,o&&l.push(this),e[n](0)),void 0):(l.push(document.scrollingElement),!1)}),l.length||this.each(function(){"BODY"===this.nodeName&&(l=[this])}),"first"===e.el&&l.length>1&&(l=[l[0]]),l};t.fn.extend({scrollable:function(t){var e=s.call(this,{dir:t});return this.pushStack(e)},firstScrollable:function(t){var e=s.call(this,{el:"first",dir:t});return this.pushStack(e)},smoothScroll:function(l,o){if(l=l||{},"options"===l)return o?this.each(function(){var e=t(this),l=t.extend(e.data("ssOpts")||{},o);t(this).data("ssOpts",l)}):this.first().data("ssOpts");var n=t.extend({},t.fn.smoothScroll.defaults,l),s=t.smoothScroll.filterPath(location.pathname);return this.unbind("click.smoothscroll").bind("click.smoothscroll",function(l){var o=this,r=t(this),i=t.extend({},n,r.data("ssOpts")||{}),c=n.exclude,a=i.excludeWithin,f=0,u=0,h=!0,d={},m=location.hostname===o.hostname||!o.hostname,p=i.scrollTarget||t.smoothScroll.filterPath(o.pathname)===s,g=e(o.hash);if(i.scrollTarget||m&&p&&g){for(;h&&c.length>f;)r.is(e(c[f++]))&&(h=!1);for(;h&&a.length>u;)r.closest(a[u++]).length&&(h=!1)}else h=!1;h&&(i.preventDefault&&l.preventDefault(),t.extend(d,i,{scrollTarget:i.scrollTarget||g,link:o}),t.smoothScroll(d))}),this}}),t.smoothScroll=function(e,l){if("options"===e&&"object"==typeof l)return t.extend(o,l);var n,s,r,i,c,a=0,f="offset",u="scrollTop",h={},d={};"number"==typeof e?(n=t.extend({link:null},t.fn.smoothScroll.defaults,o),r=e):(n=t.extend({link:null},t.fn.smoothScroll.defaults,e||{},o),n.scrollElement&&(f="position","static"===n.scrollElement.css("position")&&n.scrollElement.css("position","relative"))),u="left"===n.direction?"scrollLeft":u,n.scrollElement?(s=n.scrollElement,/^(?:HTML|BODY)$/.test(s[0].nodeName)||(a=s[u]())):s=t("html, body").firstScrollable(n.direction),n.beforeScroll.call(s,n),r="number"==typeof e?e:l||t(n.scrollTarget)[f]()&&t(n.scrollTarget)[f]()[n.direction]||0,h[u]=r+a+n.offset,i=n.speed,"auto"===i&&(c=h[u]-s.scrollTop(),0>c&&(c*=-1),i=c/n.autoCoefficient),d={duration:i,easing:n.easing,complete:function(){n.afterScroll.call(n.link,n)}},n.step&&(d.step=n.step),s.length?s.stop().animate(h,d):n.afterScroll.call(n.link,n)},t.smoothScroll.version=l,t.smoothScroll.filterPath=function(t){return t=t||"",t.replace(/^\//,"").replace(/(?:index|default).[a-zA-Z]{3,4}$/,"").replace(/\/$/,"")},t.fn.smoothScroll.defaults=n}); \ No newline at end of file diff --git a/package.json b/package.json index a07e495..2a8c4c3 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "jquery-smooth-scroll", "title": "jQuery Smooth Scroll", - "version": "1.5.5", + "version": "1.5.6", "scripts": {}, "main": "jquery.smooth-scroll.js", "author": { diff --git a/src/jquery.smooth-scroll.js b/src/jquery.smooth-scroll.js index 28ac4db..722bc8f 100644 --- a/src/jquery.smooth-scroll.js +++ b/src/jquery.smooth-scroll.js @@ -1,5 +1,5 @@ (function($) { - var version = '1.5.5', + var version = '1.5.6', optionOverrides = {}, defaults = { exclude: [], @@ -39,9 +39,18 @@ dir = opts.dir && opts.dir === 'left' ? 'scrollLeft' : 'scrollTop'; this.each(function() { - - if (this === document || this === window) { return; } var el = $(this); + + if (this === document || this === window) { + return; + } + + if ( document.scrollingElement && (this === document.documentElement || this === document.body) ) { + scrollable.push(document.scrollingElement); + + return false; + } + if ( el[dir]() > 0 ) { scrollable.push(this); } else { From ba978e149e0f7db2a56574faed57ef9e23b1c4b0 Mon Sep 17 00:00:00 2001 From: Karl Swedberg Date: Sun, 15 Nov 2015 15:30:36 -0500 Subject: [PATCH 36/58] Bower is dead. Long live npm. --- Gruntfile.js | 35 +++++++++-------------------------- bower.json | 20 -------------------- readme.md | 6 ------ 3 files changed, 9 insertions(+), 52 deletions(-) delete mode 100644 bower.json diff --git a/Gruntfile.js b/Gruntfile.js index 6a81bcf..8177b45 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -18,7 +18,6 @@ module.exports = function(grunt) { // Project configuration. grunt.initConfig({ pluginName: 'smooth-scroll', - bower: './bower.json', pkg: grunt.file.readJSON('package.json'), meta: { banner: '/*!<%= "\\n" %>' + @@ -32,7 +31,7 @@ module.exports = function(grunt) { '<%= "\\n" %>' + ' */' + '<%= "\\n\\n" %>' }, - concat: { + concat: { all: { src: ['src/jquery.<%= pluginName %>.js'], dest: 'jquery.<%= pluginName %>.js' @@ -41,8 +40,8 @@ module.exports = function(grunt) { stripBanners: true, banner: '<%= meta.banner %>', process: function(src) { - var umdHead = grunt.file.read('lib/tmpl/umdhead.tpl'), - umdFoot = grunt.file.read('lib/tmpl/umdfoot.tpl'); + var umdHead = grunt.file.read('lib/tmpl/umdhead.tpl'); + var umdFoot = grunt.file.read('lib/tmpl/umdfoot.tpl'); src = src .replace('(function($) {', umdHead) @@ -119,33 +118,17 @@ module.exports = function(grunt) { } }); - grunt.registerTask( 'configs', 'Update json configs based on package.json', function() { - var pkg = grunt.file.readJSON('package.json'), - pkgBasename = grunt.config('pluginName'), - bowerFile = grunt.config('bower'), - bower = grunt.file.readJSON(bowerFile); - - ['main', 'dependencies', 'keywords'].forEach(function(el) { - bower[el] = pkg[el]; - }); - - bower.name = 'jquery-' + pkgBasename; - - grunt.file.write( bowerFile, JSON.stringify(bower, null, 2) + '\n'); - grunt.log.writeln( 'File "' + bowerFile + '" updated."' ); - }); - grunt.registerTask('docs', 'Convert readme.md to html and concat with header and footer for index.html', function() { - var readme = grunt.file.read('readme.md'), - head = grunt.template.process( grunt.file.read('lib/tmpl/header.tpl') ), - foot = grunt.file.read('lib/tmpl/footer.tpl'), - doc = marked(readme); + var readme = grunt.file.read('readme.md'); + var head = grunt.template.process(grunt.file.read('lib/tmpl/header.tpl')); + var foot = grunt.file.read('lib/tmpl/footer.tpl'); + var doc = marked(readme); grunt.file.write('index.html', head + doc + foot); }); - grunt.registerTask('build', ['jshint', 'concat', 'version:same', 'configs', 'uglify', 'docs']); - grunt.registerTask('patch', ['jshint', 'concat', 'version:bannerPatch', 'version:patch', 'configs', 'uglify']); + grunt.registerTask('build', ['jshint', 'concat', 'version:same', 'uglify', 'docs']); + grunt.registerTask('patch', ['jshint', 'concat', 'version:bannerPatch', 'version:patch', 'uglify']); grunt.registerTask('default', ['build']); grunt.loadNpmTasks('grunt-contrib-jshint'); diff --git a/bower.json b/bower.json deleted file mode 100644 index 46ac35e..0000000 --- a/bower.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "name": "jquery-smooth-scroll", - "dependencies": { - "jquery": ">=1.3" - }, - "main": "jquery.smooth-scroll.js", - "ignore": [ - "demo/", - "lib/", - "src/", - "Gruntfile.js", - "package.json", - "smooth-scroll.jquery.json" - ], - "keywords": [ - "jquery-plugin", - "scroll", - "animation" - ] -} diff --git a/readme.md b/readme.md index b1de8f0..3a15ac9 100644 --- a/readme.md +++ b/readme.md @@ -12,12 +12,6 @@ Using npm: npm install jquery-smooth-scroll ``` -Using bower: - -```bash -bower install jquery-smooth-scroll -``` - The old-fashioned way: Go to the following URL in your browser and copy/paste the code into your own file: From adcccaa06fafbc2e8628739089d88210b4494b48 Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Wed, 16 Dec 2015 08:13:32 -0500 Subject: [PATCH 37/58] Ensure variable [scrollDir]() is used, not scrollTop(). Use Math.ab() instead of multiply by -1 if negative. --- jquery.smooth-scroll.js | 9 +++------ jquery.smooth-scroll.min.js | 4 ++-- package.json | 2 +- src/jquery.smooth-scroll.js | 7 ++----- 4 files changed, 8 insertions(+), 14 deletions(-) diff --git a/jquery.smooth-scroll.js b/jquery.smooth-scroll.js index e95bb36..c291db6 100644 --- a/jquery.smooth-scroll.js +++ b/jquery.smooth-scroll.js @@ -1,5 +1,5 @@ /*! - * jQuery Smooth Scroll - v1.5.6 - 2015-09-08 + * jQuery Smooth Scroll - v1.5.6 - 2015-12-14 * https://github.com/kswedberg/jquery-smooth-scroll * Copyright (c) 2015 Karl Swedberg * Licensed MIT (https://github.com/kswedberg/jquery-smooth-scroll/blob/master/LICENSE-MIT) @@ -231,12 +231,9 @@ // automatically calculate the speed of the scroll based on distance / coefficient if (speed === 'auto') { - // $scroller.scrollTop() is position before scroll, aniProps[scrollDir] is position after + // $scroller[scrollDir]() is position before scroll, aniProps[scrollDir] is position after // When delta is greater, speed will be greater. - delta = aniProps[scrollDir] - $scroller.scrollTop(); - if(delta < 0) { - delta *= -1; - } + delta = Math.abs(aniProps[scrollDir] - $scroller[scrollDir]()); // Divide the delta by the coefficient speed = delta / opts.autoCoefficient; diff --git a/jquery.smooth-scroll.min.js b/jquery.smooth-scroll.min.js index 2e70391..1d99cdc 100644 --- a/jquery.smooth-scroll.min.js +++ b/jquery.smooth-scroll.min.js @@ -1,7 +1,7 @@ /*! - * jQuery Smooth Scroll - v1.5.6 - 2015-09-08 + * jQuery Smooth Scroll - v1.5.6 - 2015-12-14 * https://github.com/kswedberg/jquery-smooth-scroll * Copyright (c) 2015 Karl Swedberg * Licensed MIT (https://github.com/kswedberg/jquery-smooth-scroll/blob/master/LICENSE-MIT) */ -(function(t){"function"==typeof define&&define.amd?define(["jquery"],t):"object"==typeof module&&module.exports?t(require("jquery")):t(jQuery)})(function(t){function e(t){return t.replace(/(:|\.|\/)/g,"\\$1")}var l="1.5.6",o={},n={exclude:[],excludeWithin:[],offset:0,direction:"top",scrollElement:null,scrollTarget:null,beforeScroll:function(){},afterScroll:function(){},easing:"swing",speed:400,autoCoefficient:2,preventDefault:!0},s=function(e){var l=[],o=!1,n=e.dir&&"left"===e.dir?"scrollLeft":"scrollTop";return this.each(function(){var e=t(this);if(this!==document&&this!==window)return!document.scrollingElement||this!==document.documentElement&&this!==document.body?(e[n]()>0?l.push(this):(e[n](1),o=e[n]()>0,o&&l.push(this),e[n](0)),void 0):(l.push(document.scrollingElement),!1)}),l.length||this.each(function(){"BODY"===this.nodeName&&(l=[this])}),"first"===e.el&&l.length>1&&(l=[l[0]]),l};t.fn.extend({scrollable:function(t){var e=s.call(this,{dir:t});return this.pushStack(e)},firstScrollable:function(t){var e=s.call(this,{el:"first",dir:t});return this.pushStack(e)},smoothScroll:function(l,o){if(l=l||{},"options"===l)return o?this.each(function(){var e=t(this),l=t.extend(e.data("ssOpts")||{},o);t(this).data("ssOpts",l)}):this.first().data("ssOpts");var n=t.extend({},t.fn.smoothScroll.defaults,l),s=t.smoothScroll.filterPath(location.pathname);return this.unbind("click.smoothscroll").bind("click.smoothscroll",function(l){var o=this,r=t(this),i=t.extend({},n,r.data("ssOpts")||{}),c=n.exclude,a=i.excludeWithin,f=0,u=0,h=!0,d={},m=location.hostname===o.hostname||!o.hostname,p=i.scrollTarget||t.smoothScroll.filterPath(o.pathname)===s,g=e(o.hash);if(i.scrollTarget||m&&p&&g){for(;h&&c.length>f;)r.is(e(c[f++]))&&(h=!1);for(;h&&a.length>u;)r.closest(a[u++]).length&&(h=!1)}else h=!1;h&&(i.preventDefault&&l.preventDefault(),t.extend(d,i,{scrollTarget:i.scrollTarget||g,link:o}),t.smoothScroll(d))}),this}}),t.smoothScroll=function(e,l){if("options"===e&&"object"==typeof l)return t.extend(o,l);var n,s,r,i,c,a=0,f="offset",u="scrollTop",h={},d={};"number"==typeof e?(n=t.extend({link:null},t.fn.smoothScroll.defaults,o),r=e):(n=t.extend({link:null},t.fn.smoothScroll.defaults,e||{},o),n.scrollElement&&(f="position","static"===n.scrollElement.css("position")&&n.scrollElement.css("position","relative"))),u="left"===n.direction?"scrollLeft":u,n.scrollElement?(s=n.scrollElement,/^(?:HTML|BODY)$/.test(s[0].nodeName)||(a=s[u]())):s=t("html, body").firstScrollable(n.direction),n.beforeScroll.call(s,n),r="number"==typeof e?e:l||t(n.scrollTarget)[f]()&&t(n.scrollTarget)[f]()[n.direction]||0,h[u]=r+a+n.offset,i=n.speed,"auto"===i&&(c=h[u]-s.scrollTop(),0>c&&(c*=-1),i=c/n.autoCoefficient),d={duration:i,easing:n.easing,complete:function(){n.afterScroll.call(n.link,n)}},n.step&&(d.step=n.step),s.length?s.stop().animate(h,d):n.afterScroll.call(n.link,n)},t.smoothScroll.version=l,t.smoothScroll.filterPath=function(t){return t=t||"",t.replace(/^\//,"").replace(/(?:index|default).[a-zA-Z]{3,4}$/,"").replace(/\/$/,"")},t.fn.smoothScroll.defaults=n}); \ No newline at end of file +!function(a){"function"==typeof define&&define.amd?define(["jquery"],a):a("object"==typeof module&&module.exports?require("jquery"):jQuery)}(function(a){function b(a){return a.replace(/(:|\.|\/)/g,"\\$1")}var c="1.5.6",d={},e={exclude:[],excludeWithin:[],offset:0,direction:"top",scrollElement:null,scrollTarget:null,beforeScroll:function(){},afterScroll:function(){},easing:"swing",speed:400,autoCoefficient:2,preventDefault:!0},f=function(b){var c=[],d=!1,e=b.dir&&"left"===b.dir?"scrollLeft":"scrollTop";return this.each(function(){var b=a(this);if(this!==document&&this!==window)return!document.scrollingElement||this!==document.documentElement&&this!==document.body?void(b[e]()>0?c.push(this):(b[e](1),d=b[e]()>0,d&&c.push(this),b[e](0))):(c.push(document.scrollingElement),!1)}),c.length||this.each(function(){"BODY"===this.nodeName&&(c=[this])}),"first"===b.el&&c.length>1&&(c=[c[0]]),c};a.fn.extend({scrollable:function(a){var b=f.call(this,{dir:a});return this.pushStack(b)},firstScrollable:function(a){var b=f.call(this,{el:"first",dir:a});return this.pushStack(b)},smoothScroll:function(c,d){if(c=c||{},"options"===c)return d?this.each(function(){var b=a(this),c=a.extend(b.data("ssOpts")||{},d);a(this).data("ssOpts",c)}):this.first().data("ssOpts");var e=a.extend({},a.fn.smoothScroll.defaults,c),f=a.smoothScroll.filterPath(location.pathname);return this.unbind("click.smoothscroll").bind("click.smoothscroll",function(c){var d=this,g=a(this),h=a.extend({},e,g.data("ssOpts")||{}),i=e.exclude,j=h.excludeWithin,k=0,l=0,m=!0,n={},o=location.hostname===d.hostname||!d.hostname,p=h.scrollTarget||a.smoothScroll.filterPath(d.pathname)===f,q=b(d.hash);if(h.scrollTarget||o&&p&&q){for(;m&&k Date: Wed, 16 Dec 2015 08:22:25 -0500 Subject: [PATCH 38/58] Release 1.5.7 --- jquery.smooth-scroll.js | 4 ++-- jquery.smooth-scroll.min.js | 4 ++-- package.json | 2 +- src/jquery.smooth-scroll.js | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/jquery.smooth-scroll.js b/jquery.smooth-scroll.js index c291db6..f1508e8 100644 --- a/jquery.smooth-scroll.js +++ b/jquery.smooth-scroll.js @@ -1,5 +1,5 @@ /*! - * jQuery Smooth Scroll - v1.5.6 - 2015-12-14 + * jQuery Smooth Scroll - v1.5.7 - 2015-12-16 * https://github.com/kswedberg/jquery-smooth-scroll * Copyright (c) 2015 Karl Swedberg * Licensed MIT (https://github.com/kswedberg/jquery-smooth-scroll/blob/master/LICENSE-MIT) @@ -18,7 +18,7 @@ } }(function ($) { - var version = '1.5.6', + var version = '1.5.7', optionOverrides = {}, defaults = { exclude: [], diff --git a/jquery.smooth-scroll.min.js b/jquery.smooth-scroll.min.js index 1d99cdc..2dc83a2 100644 --- a/jquery.smooth-scroll.min.js +++ b/jquery.smooth-scroll.min.js @@ -1,7 +1,7 @@ /*! - * jQuery Smooth Scroll - v1.5.6 - 2015-12-14 + * jQuery Smooth Scroll - v1.5.7 - 2015-12-16 * https://github.com/kswedberg/jquery-smooth-scroll * Copyright (c) 2015 Karl Swedberg * Licensed MIT (https://github.com/kswedberg/jquery-smooth-scroll/blob/master/LICENSE-MIT) */ -!function(a){"function"==typeof define&&define.amd?define(["jquery"],a):a("object"==typeof module&&module.exports?require("jquery"):jQuery)}(function(a){function b(a){return a.replace(/(:|\.|\/)/g,"\\$1")}var c="1.5.6",d={},e={exclude:[],excludeWithin:[],offset:0,direction:"top",scrollElement:null,scrollTarget:null,beforeScroll:function(){},afterScroll:function(){},easing:"swing",speed:400,autoCoefficient:2,preventDefault:!0},f=function(b){var c=[],d=!1,e=b.dir&&"left"===b.dir?"scrollLeft":"scrollTop";return this.each(function(){var b=a(this);if(this!==document&&this!==window)return!document.scrollingElement||this!==document.documentElement&&this!==document.body?void(b[e]()>0?c.push(this):(b[e](1),d=b[e]()>0,d&&c.push(this),b[e](0))):(c.push(document.scrollingElement),!1)}),c.length||this.each(function(){"BODY"===this.nodeName&&(c=[this])}),"first"===b.el&&c.length>1&&(c=[c[0]]),c};a.fn.extend({scrollable:function(a){var b=f.call(this,{dir:a});return this.pushStack(b)},firstScrollable:function(a){var b=f.call(this,{el:"first",dir:a});return this.pushStack(b)},smoothScroll:function(c,d){if(c=c||{},"options"===c)return d?this.each(function(){var b=a(this),c=a.extend(b.data("ssOpts")||{},d);a(this).data("ssOpts",c)}):this.first().data("ssOpts");var e=a.extend({},a.fn.smoothScroll.defaults,c),f=a.smoothScroll.filterPath(location.pathname);return this.unbind("click.smoothscroll").bind("click.smoothscroll",function(c){var d=this,g=a(this),h=a.extend({},e,g.data("ssOpts")||{}),i=e.exclude,j=h.excludeWithin,k=0,l=0,m=!0,n={},o=location.hostname===d.hostname||!d.hostname,p=h.scrollTarget||a.smoothScroll.filterPath(d.pathname)===f,q=b(d.hash);if(h.scrollTarget||o&&p&&q){for(;m&&k0?l.push(this):(e[n](1),o=e[n]()>0,o&&l.push(this),e[n](0)),void 0):(l.push(document.scrollingElement),!1)}),l.length||this.each(function(){"BODY"===this.nodeName&&(l=[this])}),"first"===e.el&&l.length>1&&(l=[l[0]]),l};t.fn.extend({scrollable:function(t){var e=s.call(this,{dir:t});return this.pushStack(e)},firstScrollable:function(t){var e=s.call(this,{el:"first",dir:t});return this.pushStack(e)},smoothScroll:function(l,o){if(l=l||{},"options"===l)return o?this.each(function(){var e=t(this),l=t.extend(e.data("ssOpts")||{},o);t(this).data("ssOpts",l)}):this.first().data("ssOpts");var n=t.extend({},t.fn.smoothScroll.defaults,l),s=t.smoothScroll.filterPath(location.pathname);return this.unbind("click.smoothscroll").bind("click.smoothscroll",function(l){var o=this,r=t(this),i=t.extend({},n,r.data("ssOpts")||{}),c=n.exclude,a=i.excludeWithin,f=0,u=0,h=!0,d={},m=location.hostname===o.hostname||!o.hostname,p=i.scrollTarget||t.smoothScroll.filterPath(o.pathname)===s,g=e(o.hash);if(i.scrollTarget||m&&p&&g){for(;h&&c.length>f;)r.is(e(c[f++]))&&(h=!1);for(;h&&a.length>u;)r.closest(a[u++]).length&&(h=!1)}else h=!1;h&&(i.preventDefault&&l.preventDefault(),t.extend(d,i,{scrollTarget:i.scrollTarget||g,link:o}),t.smoothScroll(d))}),this}}),t.smoothScroll=function(e,l){if("options"===e&&"object"==typeof l)return t.extend(o,l);var n,s,r,i,c,a=0,f="offset",u="scrollTop",h={},d={};"number"==typeof e?(n=t.extend({link:null},t.fn.smoothScroll.defaults,o),r=e):(n=t.extend({link:null},t.fn.smoothScroll.defaults,e||{},o),n.scrollElement&&(f="position","static"===n.scrollElement.css("position")&&n.scrollElement.css("position","relative"))),u="left"===n.direction?"scrollLeft":u,n.scrollElement?(s=n.scrollElement,/^(?:HTML|BODY)$/.test(s[0].nodeName)||(a=s[u]())):s=t("html, body").firstScrollable(n.direction),n.beforeScroll.call(s,n),r="number"==typeof e?e:l||t(n.scrollTarget)[f]()&&t(n.scrollTarget)[f]()[n.direction]||0,h[u]=r+a+n.offset,i=n.speed,"auto"===i&&(c=Math.abs(h[u]-s[u]()),i=c/n.autoCoefficient),d={duration:i,easing:n.easing,complete:function(){n.afterScroll.call(n.link,n)}},n.step&&(d.step=n.step),s.length?s.stop().animate(h,d):n.afterScroll.call(n.link,n)},t.smoothScroll.version=l,t.smoothScroll.filterPath=function(t){return t=t||"",t.replace(/^\//,"").replace(/(?:index|default).[a-zA-Z]{3,4}$/,"").replace(/\/$/,"")},t.fn.smoothScroll.defaults=n}); diff --git a/package.json b/package.json index 7fdbb27..3c03f4e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "jquery-smooth-scroll", "title": "jQuery Smooth Scroll", - "version": "1.5.6", + "version": "1.5.7", "scripts": {}, "main": "jquery.smooth-scroll.js", "author": { diff --git a/src/jquery.smooth-scroll.js b/src/jquery.smooth-scroll.js index 3035fb9..63a3437 100644 --- a/src/jquery.smooth-scroll.js +++ b/src/jquery.smooth-scroll.js @@ -1,5 +1,5 @@ (function($) { - var version = '1.5.6', + var version = '1.5.7', optionOverrides = {}, defaults = { exclude: [], From 9f34a80041d082c4d8344a0a8183b014cf378000 Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Tue, 22 Dec 2015 08:11:18 -0500 Subject: [PATCH 39/58] Add optional support for event delegation --- src/jquery.smooth-scroll.js | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/jquery.smooth-scroll.js b/src/jquery.smooth-scroll.js index 63a3437..3e4c75e 100644 --- a/src/jquery.smooth-scroll.js +++ b/src/jquery.smooth-scroll.js @@ -3,12 +3,16 @@ optionOverrides = {}, defaults = { exclude: [], - excludeWithin:[], + excludeWithin: [], offset: 0, // one of 'top' or 'left' direction: 'top', + // if set, bind click events through delegation + // supported since jQuery 1.4.2 + delegateSelector: null, + // jQuery set of elements you wish to scroll (for $.smoothScroll). // if null (default), $('html, body').firstScrollable() is used. scrollElement: null, @@ -113,9 +117,7 @@ var opts = $.extend({}, $.fn.smoothScroll.defaults, options), locationPath = $.smoothScroll.filterPath(location.pathname); - this - .unbind('click.smoothscroll') - .bind('click.smoothscroll', function(event) { + var clickHandler = function(event) { var link = this, $link = $(this), thisOpts = $.extend({}, opts, $link.data('ssOpts') || {}), @@ -156,7 +158,17 @@ $.smoothScroll( clickOpts ); } - }); + }; + + if (options.delegateSelector !== null) { + this + .undelegate(options.delegateSelector, 'click.smoothscroll') + .delegate(options.delegateSelector, 'click.smoothscroll', clickHandler); + } else { + this + .unbind('click.smoothscroll') + .bind('click.smoothscroll', clickHandler); + } return this; } From dfe6c9e8864768170c3511d138a6d2b0646cb80e Mon Sep 17 00:00:00 2001 From: Karl Swedberg Date: Tue, 22 Dec 2015 08:48:26 -0500 Subject: [PATCH 40/58] Update docs and minor bump --- Gruntfile.js | 4 +--- demo/index.html | 7 ++++++- index.html | 11 ++++------- jquery.smooth-scroll.js | 28 ++++++++++++++++++++-------- jquery.smooth-scroll.min.js | 6 +++--- package.json | 11 +++-------- readme.md | 3 +++ src/jquery.smooth-scroll.js | 2 +- 8 files changed, 41 insertions(+), 31 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 8177b45..6b3d26d 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -26,8 +26,7 @@ module.exports = function(grunt) { '<%= pkg.homepage ? " * " + pkg.homepage + "\\n" : "" %>' + ' * Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author.name %>' + '<%= "\\n" %>' + - ' * Licensed <%= _.pluck(pkg.licenses, "type").join(", ") %>' + - ' (<%= _.pluck(pkg.licenses, "url").join(", ") %>)' + + ' * Licensed <%= pkg.license %>' + '<%= "\\n" %>' + ' */' + '<%= "\\n\\n" %>' }, @@ -97,7 +96,6 @@ module.exports = function(grunt) { patch: { src: [ 'package.json', - '<%= pluginName %>.jquery.json', 'src/jquery.<%= pluginName %>.js', 'jquery.<%= pluginName %>.js' ], diff --git a/demo/index.html b/demo/index.html index 6f1d44e..88a4924 100644 --- a/demo/index.html +++ b/demo/index.html @@ -4,6 +4,9 @@ Smooth Scroll jQuery Plugin Demo + + +

    Smooth Scroll jQuery Plugin with Back Button Support

    +
    + +

    p1 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

    +

    p2 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

    + back to nav +

    p3 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

    + back to nav +

    p4 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

    + back to nav +

    p5 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

    + back to nav +

    p6 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

    + back to nav + +
    + + + + + + diff --git a/demo/index.html b/demo/index.html index 88a4924..02b5292 100644 --- a/demo/index.html +++ b/demo/index.html @@ -45,28 +45,29 @@ }); }); - $('#change-speed').on('click', function() { - var $p1 = $('ul.mainnav a').first(), - p1Opts = $p1.smoothScroll('options') || {}; + $('#change-speed').bind('click', function() { + var $p1 = $('ul.mainnav a').first(); + var p1Opts = $p1.smoothScroll('options') || {}; p1Opts.speed = p1Opts.speed === 1400 ? 400 : 1400; $p1.smoothScroll('options', p1Opts); }); - $('button.scrollsomething').click(function() { + $('button.scrollsomething').bind('click', function(event) { + event.preventDefault(); $.smoothScroll({ scrollElement: $('div.scrollme'), scrollTarget: '#findme' }); - return false; }); - $('button.scrollhorz').click(function() { + $('button.scrollhorz').bind('click', function(event) { + event.preventDefault(); $.smoothScroll({ direction: 'left', scrollElement: $('div.scrollme'), scrollTarget: '.horiz' }); - return false; + }); }); @@ -84,7 +85,8 @@
  • p5
  • bbq.html#p5
  • -

    Or, try it with hashchange support

    +

    Try it with jQuery BBQ hashchange support

    +

    Or, native hashchange support (IE9+)

    Toggle scrolling speed for the p1 link

    diff --git a/demo/scroll-behavior-smooth.html b/demo/scroll-behavior-smooth.html new file mode 100644 index 0000000..3379bcb --- /dev/null +++ b/demo/scroll-behavior-smooth.html @@ -0,0 +1,134 @@ + + + + + Smooth Scroll jQuery Plugin Demo + + + + + + + +
    +

    Scroll the Document to one of the following paragraphs:

    + +

    Or, try it with hashchange support

    +

    Toggle scrolling speed for the p1 link

    + + + +
    +
    +

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. +

    +
    + You found me by scrolling horizontally. nice job! +
    +
    +

    Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. +

    +

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. +

    +

    Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. +

    +
    +

    YOU FOUND ME! Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. +

    +
    + You scrolled horizontally and vertically. Awesome! +
    +
    +

    Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. +

    +

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. +

    +

    Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. +

    +
    + +

    p1 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

    +

    p2 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

    +

    p3 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

    +

    p4 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

    +

    p5 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

    +

    p6 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

    + + +
    + + From 3064e264d8426ea5faed902cf8a207f1beba7b8f Mon Sep 17 00:00:00 2001 From: Karl Swedberg Date: Sat, 16 Jan 2016 15:42:08 -0500 Subject: [PATCH 49/58] Fix uglifying. --- Gruntfile.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Gruntfile.js b/Gruntfile.js index bafd536..083776e 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -56,7 +56,8 @@ module.exports = function(grunt) { 'jquery.<%= pluginName %>.min.js': ['<%= concat.all.dest %>'] }, options: { - preserveComments: 'some' + banner: '<%= meta.banner %>', + // preserveComments: /\/\*[\s\S]*/ } } }, From bf6705fa967601181a0fa80b5baddf352b041721 Mon Sep 17 00:00:00 2001 From: Karl Swedberg Date: Sat, 16 Jan 2016 15:42:33 -0500 Subject: [PATCH 50/58] Build and bump to 1.6.2. --- index.html | 15 ++++++++++---- jquery.smooth-scroll.js | 25 +++++++++++++++-------- jquery.smooth-scroll.min.js | 40 ++++--------------------------------- package.json | 2 +- src/jquery.smooth-scroll.js | 2 +- 5 files changed, 34 insertions(+), 50 deletions(-) diff --git a/index.html b/index.html index e6a5401..130b13a 100644 --- a/index.html +++ b/index.html @@ -77,7 +77,7 @@

    Smooth Scroll Plugin

    NPM

    Download

    Using npm:

    -
    1
    npm install jquery-smooth-scroll
    +
    1
    npm install jquery-smooth-scroll

    The old-fashioned way:

    Go to the following URL in your browser and copy/paste the code into your own file: @@ -98,7 +98,7 @@

    $.fn.smoothScroll

    Options

    The following options, shown with their default values, are available for both $.fn.smoothScroll and $.smoothScroll:

    -
    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
    {
      offset: 0,
     
      // one of 'top' or 'left'
      direction: 'top',
     
      // only use if you want to override default behavior
      scrollTarget: null,
     
      // string to use as selector for event delegation (Requires jQuery >=1.4.2)
      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: '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) / autoCoeffic
      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
     
    }
    +
    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
    {
      offset: 0,
     
      // one of 'top' or 'left'
      direction: 'top',
     
      // only use if you want to override default behavior
      scrollTarget: null,
     
      // string to use as selector for event delegation (Requires jQuery >=1.4.2)
      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: '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) / autoCoeffic
      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
     
    }

    The options object for $.fn.smoothScroll can take two additional properties: exclude and excludeWithin. The value for both of these is an array of @@ -115,7 +115,7 @@

    $.smoothScroll

    document.body)
  • Doesn't automatically fire, so you need to bind it to some other user interaction. For example:

    -
    1
    2
    3
    4
    5
    6
    7
    $('button.scrollsomething').on('click', function() {
      $.smoothScroll({
        scrollElement: $('div.scrollme'),
        scrollTarget: '#findme'
      });
      return false;
    });
    +
    1
    2
    3
    4
    5
    6
    7
    $('button.scrollsomething').on('click', function() {
      $.smoothScroll({
        scrollElement: $('div.scrollme'),
        scrollTarget: '#findme'
      });
      return false;
    });
  • The $.smoothScroll method can take one or two arguments.

      @@ -129,7 +129,7 @@

      $.smoothScroll

      Additional Option

      The following option, in addition to those listed for $.fn.smoothScroll above, is available for $.smoothScroll:

      -
      1
      2
      3
      4
      5
      {
        // jQuery set of elements you wish to scroll.
        //  if null (default), $('html, body').firstScrollable() is used.
        scrollElement: null
      }
      +
      1
      2
      3
      4
      5
      {
        // jQuery set of elements you wish to scroll.
        //  if null (default), $('html, body').firstScrollable() is used.
        scrollElement: null
      }

      $.fn.scrollable

        @@ -163,6 +163,13 @@

        Notes

        therefore, will be called against no elements (which, in most cases, means that nothing will happen).
      +

      Contributing

      +

      Thank you! Please consider the following when working on this repo before you submit a pull request:

      +
        +
      • For code changes, please work on the "source" file: src/jquery.smooth-scroll.js.
      • +
      • Style conventions are noted in the jshint grunt file options and the .jscsrc file. To be sure your additions comply, run grunt lint from the command line.
      • +
      • If possible, please use Tim Pope's git commit message style. 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 --author flag to ensure that proper authorship (yours) is maintained.
      • +
      diff --git a/jquery.smooth-scroll.js b/jquery.smooth-scroll.js index e409a96..2e5f9ab 100644 --- a/jquery.smooth-scroll.js +++ b/jquery.smooth-scroll.js @@ -1,7 +1,7 @@ /*! - * jQuery Smooth Scroll - v1.6.1 - 2015-12-26 + * jQuery Smooth Scroll - v1.6.2 - 2016-01-16 * https://github.com/kswedberg/jquery-smooth-scroll - * Copyright (c) 2015 Karl Swedberg + * Copyright (c) 2016 Karl Swedberg * Licensed MIT */ @@ -18,7 +18,7 @@ } }(function($) { - var version = '1.6.1'; + var version = '1.6.2'; var optionOverrides = {}; var defaults = { exclude: [], @@ -89,13 +89,21 @@ } }); - // If no scrollable elements, fall back to , - // if it's in the jQuery collection - // (doing this because Safari sets scrollTop async, - // so can't set it to 1 and immediately get the value.) if (!scrollable.length) { this.each(function() { - if (this.nodeName === 'BODY') { + // If no scrollable elements and has scroll-behavior:smooth because + // "When this property is specified on the root element, it applies to the viewport instead." + // and "The scroll-behavior property of the … body element is *not* propagated to the viewport." + // → https://drafts.csswg.org/cssom-view/#propdef-scroll-behavior + if (this === document.documentElement && $(this).css('scrollBehavior') === 'smooth') { + scrollable = [this]; + } + + // If still no scrollable elements, fall back to , + // if it's in the jQuery collection + // (doing this because Safari sets scrollTop async, + // so can't set it to 1 and immediately get the value.) + if (!scrollable.length && this.nodeName === 'BODY') { scrollable = [this]; } }); @@ -297,3 +305,4 @@ $.fn.smoothScroll.defaults = defaults; })); + diff --git a/jquery.smooth-scroll.min.js b/jquery.smooth-scroll.min.js index c4a3d13..921a61e 100644 --- a/jquery.smooth-scroll.min.js +++ b/jquery.smooth-scroll.min.js @@ -1,40 +1,8 @@ /*! - * jQuery Smooth Scroll - v1.6.1 - 2015-12-26 + * jQuery Smooth Scroll - v1.6.2 - 2016-01-16 * https://github.com/kswedberg/jquery-smooth-scroll - * Copyright (c) 2015 Karl Swedberg + * Copyright (c) 2016 Karl Swedberg * Licensed MIT */ -!function(a){"function"==typeof define&&define.amd? -// AMD. Register as an anonymous module. -define(["jquery"],a):a("object"==typeof module&&module.exports?require("jquery"):jQuery)}(function(a){var b="1.6.1",c={},d={exclude:[],excludeWithin:[],offset:0, -// one of 'top' or 'left' -direction:"top", -// if set, bind click events through delegation -// supported since jQuery 1.4.2 -delegateSelector:null, -// jQuery set of elements you wish to scroll (for $.smoothScroll). -// if null (default), $('html, body').firstScrollable() is used. -scrollElement:null, -// only use if you want to override default behavior -scrollTarget: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:"swing",speed:400, -// coefficient for "auto" speed -autoCoefficient:2, -// $.fn.smoothScroll only: whether to prevent the default click action -preventDefault:!0},e=function(b){var c=[],d=!1,e=b.dir&&"left"===b.dir?"scrollLeft":"scrollTop"; -// If no scrollable elements, fall back to , -// if it's in the jQuery collection -// (doing this because Safari sets scrollTop async, -// so can't set it to 1 and immediately get the value.) -// Use the first scrollable element if we're calling firstScrollable() -return this.each(function(){var b=a(this);if(this!==document&&this!==window) -// if scroll(Top|Left) === 0, nudge the element 1px and see if it moves -// then put it back, of course -return!document.scrollingElement||this!==document.documentElement&&this!==document.body?void(b[e]()>0?c.push(this):(b[e](1),d=b[e]()>0,d&&c.push(this),b[e](0))):(c.push(document.scrollingElement),!1)}),c.length||this.each(function(){"BODY"===this.nodeName&&(c=[this])}),"first"===b.el&&c.length>1&&(c=[c[0]]),c};a.fn.extend({scrollable:function(a){var b=e.call(this,{dir:a});return this.pushStack(b)},firstScrollable:function(a){var b=e.call(this,{el:"first",dir:a});return this.pushStack(b)},smoothScroll:function(b,c){if(b=b||{},"options"===b)return c?this.each(function(){var b=a(this),d=a.extend(b.data("ssOpts")||{},c);a(this).data("ssOpts",d)}):this.first().data("ssOpts");var d=a.extend({},a.fn.smoothScroll.defaults,b),e=function(b){var c=function(a){return a.replace(/(:|\.|\/)/g,"\\$1")},e=this,f=a(this),g=a.extend({},d,f.data("ssOpts")||{}),h=d.exclude,i=g.excludeWithin,j=0,k=0,l=!0,m={},n=a.smoothScroll.filterPath(location.pathname),o=a.smoothScroll.filterPath(e.pathname),p=location.hostname===e.hostname||!e.hostname,q=g.scrollTarget||o===n,r=c(e.hash);if(g.scrollTarget||p&&q&&r){for(;l&&j0?c.push(this):(b[e](1),d=b[e]()>0,d&&c.push(this),b[e](0))):(c.push(document.scrollingElement),!1)}),c.length||this.each(function(){this===document.documentElement&&"smooth"===a(this).css("scrollBehavior")&&(c=[this]),c.length||"BODY"!==this.nodeName||(c=[this])}),"first"===b.el&&c.length>1&&(c=[c[0]]),c};a.fn.extend({scrollable:function(a){var b=e.call(this,{dir:a});return this.pushStack(b)},firstScrollable:function(a){var b=e.call(this,{el:"first",dir:a});return this.pushStack(b)},smoothScroll:function(b,c){if(b=b||{},"options"===b)return c?this.each(function(){var b=a(this),d=a.extend(b.data("ssOpts")||{},c);a(this).data("ssOpts",d)}):this.first().data("ssOpts");var d=a.extend({},a.fn.smoothScroll.defaults,b),e=function(b){var c=function(a){return a.replace(/(:|\.|\/)/g,"\\$1")},e=this,f=a(this),g=a.extend({},d,f.data("ssOpts")||{}),h=d.exclude,i=g.excludeWithin,j=0,k=0,l=!0,m={},n=a.smoothScroll.filterPath(location.pathname),o=a.smoothScroll.filterPath(e.pathname),p=location.hostname===e.hostname||!e.hostname,q=g.scrollTarget||o===n,r=c(e.hash);if(g.scrollTarget||p&&q&&r){for(;l&&j Date: Mon, 18 Jan 2016 08:02:11 -0500 Subject: [PATCH 51/58] Put Bower support (bower.json) back in. Fixes #91 --- Gruntfile.js | 20 ++++++++++++++++++++ bower.json | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 bower.json diff --git a/Gruntfile.js b/Gruntfile.js index 083776e..7b5ba73 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -130,6 +130,26 @@ module.exports = function(grunt) { grunt.file.write('index.html', head + doc + foot); }); + grunt.registerTask('updateBower', 'Update bower.json to match package.json', function() { + var pkg = require('./package.json'); + var props = ['name', 'main', 'homepage', 'repository', 'dependencies', 'keywords', 'license']; + var json = { + description: 'Easy implementation of smooth scrolling for same-page links' + }; + + props.forEach(function(item) { + if (pkg[item]) { + json[item] = pkg[item]; + } + }); + + json.authors = [pkg.author]; + json.moduleType = ['amd', 'node']; + json.ignore = ['demo/', 'lib/', 'src/', 'test/', '**/.*', 'Gruntfile.js', 'package.json']; + + grunt.file.write('bower.json', JSON.stringify(json, null, 2)); + }); + grunt.registerTask('lint', ['jshint', 'jscs']); grunt.registerTask('build', ['lint', 'concat', 'version', 'uglify', 'docs']); grunt.registerTask('patch', ['lint', 'concat', 'version::patch', 'uglify']); diff --git a/bower.json b/bower.json new file mode 100644 index 0000000..0021198 --- /dev/null +++ b/bower.json @@ -0,0 +1,40 @@ +{ + "description": "Easy implementation of smooth scrolling for same-page links", + "name": "jquery-smooth-scroll", + "main": "jquery.smooth-scroll.js", + "homepage": "https://github.com/kswedberg/jquery-smooth-scroll", + "repository": { + "type": "git", + "url": "https://github.com/kswedberg/jquery-smooth-scroll" + }, + "dependencies": { + "jquery": ">=1.4.2" + }, + "keywords": [ + "jQuery", + "jquery-plugin", + "scroll", + "animation" + ], + "license": "MIT", + "authors": [ + { + "name": "Karl Swedberg", + "email": "kswedberg@gmail.com", + "url": "http://www.learningjquery.com/" + } + ], + "moduleType": [ + "amd", + "node" + ], + "ignore": [ + "demo/", + "lib/", + "src/", + "test/", + "**/.*", + "Gruntfile.js", + "package.json" + ] +} \ No newline at end of file From 6902687d40d21a46d26d24cdd46e0293ed4fd848 Mon Sep 17 00:00:00 2001 From: Karl Swedberg Date: Mon, 18 Jan 2016 08:38:31 -0500 Subject: [PATCH 52/58] Fix version from bad tag --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 3a8cf0a..fe0f291 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "jquery-smooth-scroll", "title": "jQuery Smooth Scroll", - "version": "1.6.2", + "version": "1.7.0", "scripts": {}, "main": "jquery.smooth-scroll.js", "author": { From b192367d731a75b041621977e97218172d8f3a17 Mon Sep 17 00:00:00 2001 From: Karl Swedberg Date: Mon, 18 Jan 2016 08:43:13 -0500 Subject: [PATCH 53/58] Bump for bower support and bad tag fix --- jquery.smooth-scroll.js | 5 ++--- jquery.smooth-scroll.min.js | 4 ++-- package.json | 2 +- src/jquery.smooth-scroll.js | 2 +- 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/jquery.smooth-scroll.js b/jquery.smooth-scroll.js index 2e5f9ab..bbc9262 100644 --- a/jquery.smooth-scroll.js +++ b/jquery.smooth-scroll.js @@ -1,5 +1,5 @@ /*! - * jQuery Smooth Scroll - v1.6.2 - 2016-01-16 + * jQuery Smooth Scroll - v1.7.1 - 2016-01-18 * https://github.com/kswedberg/jquery-smooth-scroll * Copyright (c) 2016 Karl Swedberg * Licensed MIT @@ -18,7 +18,7 @@ } }(function($) { - var version = '1.6.2'; + var version = '1.7.1'; var optionOverrides = {}; var defaults = { exclude: [], @@ -305,4 +305,3 @@ $.fn.smoothScroll.defaults = defaults; })); - diff --git a/jquery.smooth-scroll.min.js b/jquery.smooth-scroll.min.js index 921a61e..337051c 100644 --- a/jquery.smooth-scroll.min.js +++ b/jquery.smooth-scroll.min.js @@ -1,8 +1,8 @@ /*! - * jQuery Smooth Scroll - v1.6.2 - 2016-01-16 + * jQuery Smooth Scroll - v1.7.1 - 2016-01-18 * https://github.com/kswedberg/jquery-smooth-scroll * Copyright (c) 2016 Karl Swedberg * Licensed MIT */ -!function(a){"function"==typeof define&&define.amd?define(["jquery"],a):a("object"==typeof module&&module.exports?require("jquery"):jQuery)}(function(a){var b="1.6.2",c={},d={exclude:[],excludeWithin:[],offset:0,direction:"top",delegateSelector:null,scrollElement:null,scrollTarget:null,beforeScroll:function(){},afterScroll:function(){},easing:"swing",speed:400,autoCoefficient:2,preventDefault:!0},e=function(b){var c=[],d=!1,e=b.dir&&"left"===b.dir?"scrollLeft":"scrollTop";return this.each(function(){var b=a(this);if(this!==document&&this!==window)return!document.scrollingElement||this!==document.documentElement&&this!==document.body?void(b[e]()>0?c.push(this):(b[e](1),d=b[e]()>0,d&&c.push(this),b[e](0))):(c.push(document.scrollingElement),!1)}),c.length||this.each(function(){this===document.documentElement&&"smooth"===a(this).css("scrollBehavior")&&(c=[this]),c.length||"BODY"!==this.nodeName||(c=[this])}),"first"===b.el&&c.length>1&&(c=[c[0]]),c};a.fn.extend({scrollable:function(a){var b=e.call(this,{dir:a});return this.pushStack(b)},firstScrollable:function(a){var b=e.call(this,{el:"first",dir:a});return this.pushStack(b)},smoothScroll:function(b,c){if(b=b||{},"options"===b)return c?this.each(function(){var b=a(this),d=a.extend(b.data("ssOpts")||{},c);a(this).data("ssOpts",d)}):this.first().data("ssOpts");var d=a.extend({},a.fn.smoothScroll.defaults,b),e=function(b){var c=function(a){return a.replace(/(:|\.|\/)/g,"\\$1")},e=this,f=a(this),g=a.extend({},d,f.data("ssOpts")||{}),h=d.exclude,i=g.excludeWithin,j=0,k=0,l=!0,m={},n=a.smoothScroll.filterPath(location.pathname),o=a.smoothScroll.filterPath(e.pathname),p=location.hostname===e.hostname||!e.hostname,q=g.scrollTarget||o===n,r=c(e.hash);if(g.scrollTarget||p&&q&&r){for(;l&&j0?c.push(this):(b[e](1),d=b[e]()>0,d&&c.push(this),b[e](0))):(c.push(document.scrollingElement),!1)}),c.length||this.each(function(){this===document.documentElement&&"smooth"===a(this).css("scrollBehavior")&&(c=[this]),c.length||"BODY"!==this.nodeName||(c=[this])}),"first"===b.el&&c.length>1&&(c=[c[0]]),c};a.fn.extend({scrollable:function(a){var b=e.call(this,{dir:a});return this.pushStack(b)},firstScrollable:function(a){var b=e.call(this,{el:"first",dir:a});return this.pushStack(b)},smoothScroll:function(b,c){if(b=b||{},"options"===b)return c?this.each(function(){var b=a(this),d=a.extend(b.data("ssOpts")||{},c);a(this).data("ssOpts",d)}):this.first().data("ssOpts");var d=a.extend({},a.fn.smoothScroll.defaults,b),e=function(b){var c=function(a){return a.replace(/(:|\.|\/)/g,"\\$1")},e=this,f=a(this),g=a.extend({},d,f.data("ssOpts")||{}),h=d.exclude,i=g.excludeWithin,j=0,k=0,l=!0,m={},n=a.smoothScroll.filterPath(location.pathname),o=a.smoothScroll.filterPath(e.pathname),p=location.hostname===e.hostname||!e.hostname,q=g.scrollTarget||o===n,r=c(e.hash);if(g.scrollTarget||p&&q&&r){for(;l&&j Date: Sat, 23 Jan 2016 15:45:39 -0500 Subject: [PATCH 54/58] Ensure links to nonexistent elements don't trigger scroll. Fixes #92. --- src/jquery.smooth-scroll.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/jquery.smooth-scroll.js b/src/jquery.smooth-scroll.js index 486874c..54105e7 100644 --- a/src/jquery.smooth-scroll.js +++ b/src/jquery.smooth-scroll.js @@ -148,6 +148,10 @@ var pathMatch = thisOpts.scrollTarget || (linkPath === locationPath); var thisHash = escapeSelector(link.hash); + if (thisHash && !$(thisHash).length) { + include = false; + } + if (!thisOpts.scrollTarget && (!hostMatch || !pathMatch || !thisHash)) { include = false; } else { From baa0e68a56a69bac7555bda0bc09aa77f9fe40e0 Mon Sep 17 00:00:00 2001 From: Karl Swedberg Date: Sat, 23 Jan 2016 15:47:36 -0500 Subject: [PATCH 55/58] Add link to nonexistent element to demo and tests for avoiding scroll (and for scroll). --- demo/index.html | 1 + test/index.html | 12 ++++++------ test/tests.js | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/demo/index.html b/demo/index.html index 02b5292..03ac6e6 100644 --- a/demo/index.html +++ b/demo/index.html @@ -83,6 +83,7 @@
    • p3
    • p4
    • p5
    • +
    • link to nowhere
    • bbq.html#p5

    Try it with jQuery BBQ hashchange support

    diff --git a/test/index.html b/test/index.html index 46c5fe1..9804f2c 100644 --- a/test/index.html +++ b/test/index.html @@ -10,6 +10,7 @@ width: 200px; } #scrollable { + border: 1px solid red; height: 200px; overflow: auto; } @@ -18,12 +19,11 @@
    - + go to one +
    -

    Bacon ipsum dolor amet andouille tri-tip salami frankfurter fatback venison. Cupim sausage rump boudin pork loin, salami kielbasa chuck jowl venison kevin prosciutto flank ribeye. Alcatra chuck sausage meatball beef ribs brisket biltong rump andouille tenderloin leberkas. Bacon andouille pork loin chuck shankle, ground round hamburger ham hock ball tip meatball sirloin drumstick flank kielbasa prosciutto.

    -

    Hamburger pig sausage, pancetta rump turducken shankle landjaeger pork belly bresaola jerky brisket leberkas porchetta swine. Pork shankle salami, sausage ham cupim andouille. Sirloin sausage jerky, leberkas chuck short ribs pancetta t-bone kielbasa strip steak prosciutto salami beef ribs frankfurter. Brisket doner andouille t-bone prosciutto short loin. Kielbasa tenderloin doner beef ribs shankle.

    -

    Kielbasa cupim boudin shank frankfurter. Short loin shank shankle sausage, salami pork loin leberkas biltong venison. Bresaola chuck chicken drumstick. Fatback cupim short ribs jerky brisket pancetta flank cow beef ribs salami capicola picanha rump short loin meatloaf. Corned beef meatloaf pork belly picanha alcatra pork chop.

    -

    Sirloin frankfurter tongue pork, filet mignon drumstick shankle beef ribs tri-tip turducken hamburger salami andouille meatloaf. Andouille landjaeger flank boudin beef short ribs brisket alcatra kielbasa ham pork loin pancetta. Pancetta tenderloin shoulder, porchetta tri-tip pastrami ham pork loin tail swine andouille. Sausage landjaeger strip steak tail corned beef biltong meatball picanha beef ribs swine kielbasa. T-bone tail rump kevin doner chuck. Prosciutto andouille pork loin shankle t-bone, turducken sirloin landjaeger pancetta picanha spare ribs filet mignon. Shank ham beef chuck salami tail t-bone shoulder beef ribs kevin.

    +
    big block
    +

    Sirloin frankfurter tongue pork, filet mignon drumstick shankle beef ribs tri-tip turducken hamburger salami andouille meatloaf. Andouille landjaeger flank boudin beef short ribs brisket alcatra kielbasa ham pork loin pancetta. Pancetta tenderloin shoulder, porchetta tri-tip pastrami ham pork loin tail swine andouille. Sausage landjaeger strip steak tail corned beef biltong meatball picanha beef ribs swine kielbasa. T-bone tail rump kevin doner chuck. Prosciutto andouille pork loin shankle t-bone, turducken sirloin landjaeger pancetta picanha spare ribs filet mignon. Shank ham beef chuck salami tail t-bone shoulder beef ribs kevin.

    Bacon ipsum dolor amet andouille tri-tip salami frankfurter fatback venison. Cupim sausage rump boudin pork loin, salami kielbasa chuck jowl venison kevin prosciutto flank ribeye. Alcatra chuck sausage meatball beef ribs brisket biltong rump andouille tenderloin leberkas. Bacon andouille pork loin chuck shankle, ground round hamburger ham hock ball tip meatball sirloin drumstick flank kielbasa prosciutto.

    @@ -38,7 +38,7 @@

    Salami tongue picanha, cupim cow ground round bacon turducken fatback meatloaf landjaeger leberkas kevin. Ground round kielbasa swine pork loin kevin ball tip. Shankle beef ribs venison biltong turkey, strip steak short ribs fatback kielbasa tenderloin pastrami ham hock ham. Salami bacon tail tongue pancetta cupim pastrami pork chop prosciutto brisket beef ribs. Swine salami ball tip tongue, turducken sausage pork belly frankfurter picanha capicola andouille meatloaf biltong short ribs hamburger. Shankle meatloaf tenderloin meatball, boudin ground round andouille cupim short loin filet mignon capicola. Boudin bresaola spare ribs meatloaf, alcatra bacon pork loin frankfurter prosciutto meatball pork belly salami.

    Corned beef doner flank kevin. Alcatra ham hock kielbasa, pork loin turkey landjaeger bresaola shankle t-bone rump flank tenderloin beef. Meatball shank turducken leberkas t-bone. Ground round beef swine rump porchetta pork chop tri-tip pork loin kielbasa pastrami frankfurter pork andouille.

    Turkey chicken tongue boudin cupim flank porchetta pastrami. Cow ham alcatra, porchetta turkey doner t-bone ribeye brisket spare ribs short ribs kielbasa. Ribeye ground round tenderloin beef ribs. Pancetta capicola porchetta ball tip doner drumstick boudin t-bone shankle. Pancetta flank leberkas, chicken tri-tip porchetta drumstick tail ribeye venison. Landjaeger alcatra turkey, picanha porchetta beef frankfurter meatball bacon pig short loin.

    -

    Filet mignon beef corned beef biltong doner, kielbasa prosciutto venison tenderloin. Shoulder bresaola t-bone pig tenderloin, meatball spare ribs leberkas tongue drumstick picanha rump ground round. Tongue short ribs pig salami rump, boudin andouille hamburger pancetta meatball. Filet mignon capicola pancetta cupim, shank jerky ham hock pork spare ribs strip steak short ribs rump alcatra. Shoulder short ribs picanha, chicken beef ham pork chop leberkas pastrami bresaola meatball salami. Salami pork chop ham hock pork meatloaf kielbasa turkey ribeye chicken tenderloin hamburger boudin frankfurter. Tri-tip hamburger short ribs short loin ham ball tip shoulder landjaeger tongue pig.

    +

    Filet mignon beef corned beef biltong doner, kielbasa prosciutto venison tenderloin. Shoulder bresaola t-bone pig tenderloin, meatball spare ribs leberkas tongue drumstick picanha rump ground round. Tongue short ribs pig salami rump, boudin andouille hamburger pancetta meatball. Filet mignon capicola pancetta cupim, shank jerky ham hock pork spare ribs strip steak short ribs rump alcatra. Shoulder short ribs picanha, chicken beef ham pork chop leberkas pastrami bresaola meatball salami. Salami pork chop ham hock pork meatloaf kielbasa turkey ribeye chicken tenderloin hamburger boudin frankfurter. Tri-tip hamburger short ribs short loin ham ball tip shoulder landjaeger tongue pig.

    Flank pastrami sausage jerky cupim. Corned beef shankle boudin pancetta salami short ribs. Frankfurter beef ribs ribeye corned beef pastrami landjaeger flank meatloaf pork chop. Prosciutto pig frankfurter leberkas ham hock. Cupim flank tenderloin shank porchetta tail.

    Bresaola beef ribs beef, tongue picanha sirloin short loin biltong. Short ribs leberkas jerky beef ribs tongue bacon pork chop. Meatloaf shankle short loin, jerky cupim chuck ground round biltong ham cow tenderloin. Sausage pancetta ribeye, venison doner landjaeger kielbasa bresaola. Leberkas tail andouille jowl spare ribs pork frankfurter pork belly salami sausage t-bone rump tongue. Flank chuck pork chop short ribs ball tip ham hock jerky shankle beef kielbasa prosciutto salami.

    diff --git a/test/tests.js b/test/tests.js index cf21c43..0c4b426 100644 --- a/test/tests.js +++ b/test/tests.js @@ -22,3 +22,36 @@ QUnit.test('Returns scrollable element (div#scrollable)', function(assert) { assert.equal(scrollable.length, 1, 'One scrollable element is returned'); assert.equal(scrollable[0].id, 'scrollable', 'Scrollable element is
    '); }); + +QUnit.module('$.smoothScroll'); +QUnit.test('Scrolls the #scrollable element, or not.', function(assert) { + var done = assert.async(2); + + $.smoothScroll({ + scrollElement: $('#scrollable'), + scrollTarget: '#does-not-exist', + speed: 200, + afterScroll: function() { + var scrollTop = $('#scrollable').scrollTop(); + assert.equal(scrollTop, 0, '#scrollable element does not scroll to non-existant element'); + + // Trigger the next scroll, which should actually work + $('#scrollit').trigger('click'); + done(); + } + }); + + $('#scrollit').bind('click', function(event) { + event.preventDefault(); + $.smoothScroll({ + scrollElement: $('#scrollable'), + scrollTarget: '#findme', + speed: 200, + afterScroll: function() { + var scrollTop = $('#scrollable').scrollTop(); + assert.equal(scrollTop, 100, '#scrollable element scrolls'); + done(); + } + }); + }); +}); From cfb4c29bea87fa95cac373aa9eff1298a0a2b8be Mon Sep 17 00:00:00 2001 From: Karl Swedberg Date: Sat, 23 Jan 2016 16:14:47 -0500 Subject: [PATCH 56/58] Gruntfile: fix grunt-version config so everything gets correct version. --- Gruntfile.js | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 7b5ba73..a164a33 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -1,7 +1,7 @@ /*global module:false*/ module.exports = function(grunt) { - + var pkg = grunt.file.readJSON('package.json'); var marked = require('marked'); var hl = require('node-syntaxhighlighter'); @@ -18,7 +18,7 @@ module.exports = function(grunt) { // Project configuration. grunt.initConfig({ pluginName: 'smooth-scroll', - pkg: grunt.file.readJSON('package.json'), + pkg: pkg, meta: { banner: '/*!<%= "\\n" %>' + ' * <%= pkg.title %> - v<%= pkg.version %> - ' + @@ -103,21 +103,22 @@ module.exports = function(grunt) { } }, version: { - - files: { - src: [ - 'package.json', - 'src/jquery.<%= pluginName %>.js', - 'jquery.<%= pluginName %>.js' - ] + src: { + src: ['src/jquery.<%= pluginName %>.js'] }, - banner: { - src: ['jquery.<%= pluginName %>.js'], + banners: { + pkg: pkg, + src: [ + 'jquery.<%= pluginName %>.js', + 'jquery.<%= pluginName %>.min.js' + ], options: { prefix: '- v' } }, - + package: { + src: ['package.json'] + }, } }); @@ -152,9 +153,12 @@ module.exports = function(grunt) { grunt.registerTask('lint', ['jshint', 'jscs']); grunt.registerTask('build', ['lint', 'concat', 'version', 'uglify', 'docs']); - grunt.registerTask('patch', ['lint', 'concat', 'version::patch', 'uglify']); grunt.registerTask('default', ['build']); + ['patch', 'minor', 'major'].forEach(function(release) { + grunt.registerTask(release, ['lint', 'version:src:' + release, 'concat', 'uglify', 'version:banners:' + release, 'version:package:' + release]); + }); + grunt.loadNpmTasks('grunt-jscs'); grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.loadNpmTasks('grunt-contrib-uglify'); From 7c327265673e06234f7ae740cbcf3aaf984ffed3 Mon Sep 17 00:00:00 2001 From: Karl Swedberg Date: Sun, 24 Jan 2016 21:37:31 -0500 Subject: [PATCH 57/58] Build and bump to 1.7.2. --- index.html | 8 ++++---- jquery.smooth-scroll.js | 9 +++++++-- jquery.smooth-scroll.min.js | 4 ++-- package.json | 2 +- src/jquery.smooth-scroll.js | 2 +- 5 files changed, 15 insertions(+), 10 deletions(-) diff --git a/index.html b/index.html index 130b13a..d4d97dc 100644 --- a/index.html +++ b/index.html @@ -77,7 +77,7 @@

    Smooth Scroll Plugin

    NPM

    Download

    Using npm:

    -
    1
    npm install jquery-smooth-scroll
    +
    1
    npm install jquery-smooth-scroll

    The old-fashioned way:

    Go to the following URL in your browser and copy/paste the code into your own file: @@ -98,7 +98,7 @@

    $.fn.smoothScroll

    Options

    The following options, shown with their default values, are available for both $.fn.smoothScroll and $.smoothScroll:

    -
    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
    {
      offset: 0,
     
      // one of 'top' or 'left'
      direction: 'top',
     
      // only use if you want to override default behavior
      scrollTarget: null,
     
      // string to use as selector for event delegation (Requires jQuery >=1.4.2)
      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: '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) / autoCoeffic
      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
     
    }
    +
    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
    {
      offset: 0,
     
      // one of 'top' or 'left'
      direction: 'top',
     
      // only use if you want to override default behavior
      scrollTarget: null,
     
      // string to use as selector for event delegation (Requires jQuery >=1.4.2)
      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: '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) / autoCoeffic
      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
     
    }

    The options object for $.fn.smoothScroll can take two additional properties: exclude and excludeWithin. The value for both of these is an array of @@ -115,7 +115,7 @@

    $.smoothScroll

    document.body)
  • Doesn't automatically fire, so you need to bind it to some other user interaction. For example:

    -
    1
    2
    3
    4
    5
    6
    7
    $('button.scrollsomething').on('click', function() {
      $.smoothScroll({
        scrollElement: $('div.scrollme'),
        scrollTarget: '#findme'
      });
      return false;
    });
    +
    1
    2
    3
    4
    5
    6
    7
    $('button.scrollsomething').on('click', function() {
      $.smoothScroll({
        scrollElement: $('div.scrollme'),
        scrollTarget: '#findme'
      });
      return false;
    });
  • The $.smoothScroll method can take one or two arguments.

      @@ -129,7 +129,7 @@

      $.smoothScroll

      Additional Option

      The following option, in addition to those listed for $.fn.smoothScroll above, is available for $.smoothScroll:

      -
      1
      2
      3
      4
      5
      {
        // jQuery set of elements you wish to scroll.
        //  if null (default), $('html, body').firstScrollable() is used.
        scrollElement: null
      }
      +
      1
      2
      3
      4
      5
      {
        // jQuery set of elements you wish to scroll.
        //  if null (default), $('html, body').firstScrollable() is used.
        scrollElement: null
      }

      $.fn.scrollable

        diff --git a/jquery.smooth-scroll.js b/jquery.smooth-scroll.js index bbc9262..02b06d8 100644 --- a/jquery.smooth-scroll.js +++ b/jquery.smooth-scroll.js @@ -1,5 +1,5 @@ /*! - * jQuery Smooth Scroll - v1.7.1 - 2016-01-18 + * jQuery Smooth Scroll - v1.7.2 - 2016-01-23 * https://github.com/kswedberg/jquery-smooth-scroll * Copyright (c) 2016 Karl Swedberg * Licensed MIT @@ -18,7 +18,7 @@ } }(function($) { - var version = '1.7.1'; + var version = '1.7.2'; var optionOverrides = {}; var defaults = { exclude: [], @@ -167,6 +167,10 @@ var pathMatch = thisOpts.scrollTarget || (linkPath === locationPath); var thisHash = escapeSelector(link.hash); + if (thisHash && !$(thisHash).length) { + include = false; + } + if (!thisOpts.scrollTarget && (!hostMatch || !pathMatch || !thisHash)) { include = false; } else { @@ -305,3 +309,4 @@ $.fn.smoothScroll.defaults = defaults; })); + diff --git a/jquery.smooth-scroll.min.js b/jquery.smooth-scroll.min.js index 337051c..2477dd0 100644 --- a/jquery.smooth-scroll.min.js +++ b/jquery.smooth-scroll.min.js @@ -1,8 +1,8 @@ /*! - * jQuery Smooth Scroll - v1.7.1 - 2016-01-18 + * jQuery Smooth Scroll - v1.7.2 - 2016-01-23 * https://github.com/kswedberg/jquery-smooth-scroll * Copyright (c) 2016 Karl Swedberg * Licensed MIT */ -!function(a){"function"==typeof define&&define.amd?define(["jquery"],a):a("object"==typeof module&&module.exports?require("jquery"):jQuery)}(function(a){var b="1.7.1",c={},d={exclude:[],excludeWithin:[],offset:0,direction:"top",delegateSelector:null,scrollElement:null,scrollTarget:null,beforeScroll:function(){},afterScroll:function(){},easing:"swing",speed:400,autoCoefficient:2,preventDefault:!0},e=function(b){var c=[],d=!1,e=b.dir&&"left"===b.dir?"scrollLeft":"scrollTop";return this.each(function(){var b=a(this);if(this!==document&&this!==window)return!document.scrollingElement||this!==document.documentElement&&this!==document.body?void(b[e]()>0?c.push(this):(b[e](1),d=b[e]()>0,d&&c.push(this),b[e](0))):(c.push(document.scrollingElement),!1)}),c.length||this.each(function(){this===document.documentElement&&"smooth"===a(this).css("scrollBehavior")&&(c=[this]),c.length||"BODY"!==this.nodeName||(c=[this])}),"first"===b.el&&c.length>1&&(c=[c[0]]),c};a.fn.extend({scrollable:function(a){var b=e.call(this,{dir:a});return this.pushStack(b)},firstScrollable:function(a){var b=e.call(this,{el:"first",dir:a});return this.pushStack(b)},smoothScroll:function(b,c){if(b=b||{},"options"===b)return c?this.each(function(){var b=a(this),d=a.extend(b.data("ssOpts")||{},c);a(this).data("ssOpts",d)}):this.first().data("ssOpts");var d=a.extend({},a.fn.smoothScroll.defaults,b),e=function(b){var c=function(a){return a.replace(/(:|\.|\/)/g,"\\$1")},e=this,f=a(this),g=a.extend({},d,f.data("ssOpts")||{}),h=d.exclude,i=g.excludeWithin,j=0,k=0,l=!0,m={},n=a.smoothScroll.filterPath(location.pathname),o=a.smoothScroll.filterPath(e.pathname),p=location.hostname===e.hostname||!e.hostname,q=g.scrollTarget||o===n,r=c(e.hash);if(g.scrollTarget||p&&q&&r){for(;l&&j0?c.push(this):(b[e](1),d=b[e]()>0,d&&c.push(this),b[e](0))):(c.push(document.scrollingElement),!1)}),c.length||this.each(function(){this===document.documentElement&&"smooth"===a(this).css("scrollBehavior")&&(c=[this]),c.length||"BODY"!==this.nodeName||(c=[this])}),"first"===b.el&&c.length>1&&(c=[c[0]]),c};a.fn.extend({scrollable:function(a){var b=e.call(this,{dir:a});return this.pushStack(b)},firstScrollable:function(a){var b=e.call(this,{el:"first",dir:a});return this.pushStack(b)},smoothScroll:function(b,c){if(b=b||{},"options"===b)return c?this.each(function(){var b=a(this),d=a.extend(b.data("ssOpts")||{},c);a(this).data("ssOpts",d)}):this.first().data("ssOpts");var d=a.extend({},a.fn.smoothScroll.defaults,b),e=function(b){var c=function(a){return a.replace(/(:|\.|\/)/g,"\\$1")},e=this,f=a(this),g=a.extend({},d,f.data("ssOpts")||{}),h=d.exclude,i=g.excludeWithin,j=0,k=0,l=!0,m={},n=a.smoothScroll.filterPath(location.pathname),o=a.smoothScroll.filterPath(e.pathname),p=location.hostname===e.hostname||!e.hostname,q=g.scrollTarget||o===n,r=c(e.hash);if(r&&!a(r).length&&(l=!1),g.scrollTarget||p&&q&&r){for(;l&&j Date: Sun, 20 Mar 2016 14:42:50 -0400 Subject: [PATCH 58/58] Update readme with a couple examples --- index.html | 24 +++++++++++++++++++---- readme.md | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 4 deletions(-) diff --git a/index.html b/index.html index d4d97dc..2443d33 100644 --- a/index.html +++ b/index.html @@ -77,7 +77,7 @@

        Smooth Scroll Plugin

        NPM

        Download

        Using npm:

        -
        1
        npm install jquery-smooth-scroll
        +
        1
        npm install jquery-smooth-scroll

        The old-fashioned way:

        Go to the following URL in your browser and copy/paste the code into your own file: @@ -98,7 +98,7 @@

        $.fn.smoothScroll

      Options

      The following options, shown with their default values, are available for both $.fn.smoothScroll and $.smoothScroll:

      -
      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
      {
        offset: 0,
       
        // one of 'top' or 'left'
        direction: 'top',
       
        // only use if you want to override default behavior
        scrollTarget: null,
       
        // string to use as selector for event delegation (Requires jQuery >=1.4.2)
        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: '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) / autoCoeffic
        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
       
      }
      +
      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
      {
        offset: 0,
       
        // one of 'top' or 'left'
        direction: 'top',
       
        // only use if you want to override default behavior
        scrollTarget: null,
       
        // string to use as selector for event delegation (Requires jQuery >=1.4.2)
        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: '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) / autoCoeffic
        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
       
      }

      The options object for $.fn.smoothScroll can take two additional properties: exclude and excludeWithin. The value for both of these is an array of @@ -115,7 +115,7 @@

      $.smoothScroll

      document.body)
    • Doesn't automatically fire, so you need to bind it to some other user interaction. For example:

      -
      1
      2
      3
      4
      5
      6
      7
      $('button.scrollsomething').on('click', function() {
        $.smoothScroll({
          scrollElement: $('div.scrollme'),
          scrollTarget: '#findme'
        });
        return false;
      });
      +
      1
      2
      3
      4
      5
      6
      7
      $('button.scrollsomething').on('click', function() {
        $.smoothScroll({
          scrollElement: $('div.scrollme'),
          scrollTarget: '#findme'
        });
        return false;
      });
    • The $.smoothScroll method can take one or two arguments.

        @@ -129,7 +129,7 @@

        $.smoothScroll

        Additional Option

        The following option, in addition to those listed for $.fn.smoothScroll above, is available for $.smoothScroll:

        -
        1
        2
        3
        4
        5
        {
          // jQuery set of elements you wish to scroll.
          //  if null (default), $('html, body').firstScrollable() is used.
          scrollElement: null
        }
        +
        1
        2
        3
        4
        5
        {
          // jQuery set of elements you wish to scroll.
          //  if null (default), $('html, body').firstScrollable() is used.
          scrollElement: null
        }

        $.fn.scrollable

          @@ -148,6 +148,22 @@

          $.fn.firstScrollable

          $('html, body').firstScrollable().animate({scrollTop: someNumber}, someSpeed)
        +

        Examples

        +

        Smooth scrolling on page load

        +

        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().

        +

        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:

        +
        1
        2
        3
        4
        5
        $('a[href="page-2.html#scrolltome"]').attr('href', function() {
          var hrefParts = this.href.split(/#/);
          hrefParts[1] = 'smoothScroll' + hrefParts[1];
          return hrefParts.join('#');
        });
        +
        +

        Then for page-2.html, your script would do this:

        +
        1
        2
        3
        4
        5
        6
        7
        8
        // 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});
        }
        +
        +

        Focus element after scrolling to it.

        +

        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:

        +
        1
        2
        3
        4
        5
        $('a.example').smoothScroll({
          afterScroll: function(options) {
            $(options.scrollTarget).find('input')[0].focus();
          }
        });
        +
        +

        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:

        +
        1
        2
        3
        4
        5
        6
        7
        8
        $('a.example').smoothScroll({
          afterScroll: function(options) {
            var $tgt = $(options.scrollTarget);
            $tgt.attr('tabIndex', '0');
            // Using $tgt[0] allows us to call .focus() on the DOM node itself, not the jQuery collection
            $tgt[0].focus();
          }
        });
        +

        Notes

        • To determine where to scroll the page, the $.fn.smoothScroll method looks diff --git a/readme.md b/readme.md index 38baeb2..07c0976 100644 --- a/readme.md +++ b/readme.md @@ -138,6 +138,62 @@ for `$.smoothScroll`: `$('html, body').firstScrollable().animate({scrollTop: someNumber}, someSpeed)` +## Examples + +### Smooth scrolling on page load + +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()`. + +For example, let's say you want to smooth scroll to `
          ` on page-2.html. For page-1.html, your script might do the following: + +```js +$('a[href="page-2.html#scrolltome"]').attr('href', function() { + var hrefParts = this.href.split(/#/); + hrefParts[1] = 'smoothScroll' + hrefParts[1]; + return hrefParts.join('#'); +}); + +``` + +Then for page-2.html, your script would do this: + +```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}); +} +``` + +## Focus element after scrolling to it. + +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: + +```js +$('a.example').smoothScroll({ + afterScroll: function(options) { + $(options.scrollTarget).find('input')[0].focus(); + } +}); + +``` + +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: + +```js +$('a.example').smoothScroll({ + afterScroll: function(options) { + var $tgt = $(options.scrollTarget); + $tgt.attr('tabIndex', '0'); + // Using $tgt[0] allows us to call .focus() on the DOM node itself, not the jQuery collection + $tgt[0].focus(); + } +}); +``` + ## Notes * To determine where to scroll the page, the `$.fn.smoothScroll` method looks