Skip to content

Commit

Permalink
Lint files with both jshint and jscs.
Browse files Browse the repository at this point in the history
  • Loading branch information
kswedberg committed Jan 16, 2016
1 parent 0d48ac7 commit d28ff55
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 260 deletions.
250 changes: 129 additions & 121 deletions jquery.smooth-scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Licensed MIT
*/

(function (factory) {
(function(factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], factory);
Expand All @@ -16,118 +16,122 @@
// Browser globals
factory(jQuery);
}
}(function ($) {
}(function($) {

var version = '1.6.1',
optionOverrides = {},
defaults = {
exclude: [],
excludeWithin: [],
offset: 0,
var version = '1.6.1';
var optionOverrides = {};
var defaults = {
exclude: [],
excludeWithin: [],
offset: 0,

// one of 'top' or 'left'
direction: 'top',
// one of 'top' or 'left'
direction: 'top',

// if set, bind click events through delegation
// supported since jQuery 1.4.2
delegateSelector: null,
// 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,
// 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,
// 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 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,
// 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,
// coefficient for "auto" speed
autoCoefficient: 2,

// $.fn.smoothScroll only: whether to prevent the default click action
preventDefault: true
},
// $.fn.smoothScroll only: whether to prevent the default click action
preventDefault: true
};

getScrollable = function(opts) {
var scrollable = [],
scrolled = false,
dir = opts.dir && opts.dir === 'left' ? 'scrollLeft' : 'scrollTop';
var getScrollable = function(opts) {
var scrollable = [];
var scrolled = false;
var dir = opts.dir && opts.dir === 'left' ? 'scrollLeft' : 'scrollTop';

this.each(function() {
var el = $(this);
this.each(function() {
var el = $(this);

if (this === document || this === window) {
return;
}
if (this === document || this === window) {
return;
}

if ( document.scrollingElement && (this === document.documentElement || this === document.body) ) {
scrollable.push(document.scrollingElement);
if (document.scrollingElement && (this === document.documentElement || this === document.body)) {
scrollable.push(document.scrollingElement);

return false;
}
return false;
}

if ( el[dir]() > 0 ) {
scrollable.push(this);
} else {
// if scroll(Top|Left) === 0, nudge the element 1px and see if it moves
el[dir](1);
scrolled = el[dir]() > 0;
if ( scrolled ) {
scrollable.push(this);
}
// then put it back, of course
el[dir](0);
}
});
if (el[dir]() > 0) {
scrollable.push(this);
} else {
// if scroll(Top|Left) === 0, nudge the element 1px and see if it moves
el[dir](1);
scrolled = el[dir]() > 0;

// If no scrollable elements, fall back to <body>,
// 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') {
scrollable = [this];
}
});
if (scrolled) {
scrollable.push(this);
}

// Use the first scrollable element if we're calling firstScrollable()
if ( opts.el === 'first' && scrollable.length > 1 ) {
scrollable = [ scrollable[0] ];
// then put it back, of course
el[dir](0);
}
});

// If no scrollable elements, fall back to <body>,
// 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') {
scrollable = [this];
}
});
}

return scrollable;
};
// Use the first scrollable element if we're calling firstScrollable()
if (opts.el === 'first' && scrollable.length > 1) {
scrollable = [scrollable[0]];
}

return scrollable;
};

$.fn.extend({
scrollable: function(dir) {
var scrl = getScrollable.call(this, {dir: dir});

return this.pushStack(scrl);
},
firstScrollable: function(dir) {
var scrl = getScrollable.call(this, {el: 'first', dir: dir});

return this.pushStack(scrl);
},

smoothScroll: function(options, extra) {
options = options || {};

if ( options === 'options' ) {
if ( !extra ) {
if (options === 'options') {
if (!extra) {
return this.first().data('ssOpts');
}

return this.each(function() {
var $this = $(this),
opts = $.extend($this.data('ssOpts') || {}, extra);
var $this = $(this);
var opts = $.extend($this.data('ssOpts') || {}, extra);

$(this).data('ssOpts', opts);
});
Expand All @@ -136,82 +140,89 @@
var opts = $.extend({}, $.fn.smoothScroll.defaults, options);

var clickHandler = function(event) {
var link = this,
$link = $(this),
thisOpts = $.extend({}, opts, $link.data('ssOpts') || {}),
exclude = opts.exclude,
excludeWithin = thisOpts.excludeWithin,
elCounter = 0, ewlCounter = 0,
include = true,
clickOpts = {},
locationPath = $.smoothScroll.filterPath(location.pathname),
linkPath = $.smoothScroll.filterPath(link.pathname),
hostMatch = ((location.hostname === link.hostname) || !link.hostname),
pathMatch = thisOpts.scrollTarget || ( linkPath === locationPath ),
thisHash = escapeSelector(link.hash);

if ( !thisOpts.scrollTarget && (!hostMatch || !pathMatch || !thisHash) ) {
var escapeSelector = function(str) {
return str.replace(/(:|\.|\/)/g, '\\$1');
};

var link = this;
var $link = $(this);
var thisOpts = $.extend({}, opts, $link.data('ssOpts') || {});
var exclude = opts.exclude;
var excludeWithin = thisOpts.excludeWithin;
var elCounter = 0;
var ewlCounter = 0;
var include = true;
var clickOpts = {};
var locationPath = $.smoothScroll.filterPath(location.pathname);
var linkPath = $.smoothScroll.filterPath(link.pathname);
var hostMatch = location.hostname === link.hostname || !link.hostname;
var pathMatch = thisOpts.scrollTarget || (linkPath === locationPath);
var thisHash = escapeSelector(link.hash);

if (!thisOpts.scrollTarget && (!hostMatch || !pathMatch || !thisHash)) {
include = false;
} else {
while (include && elCounter < exclude.length) {
if ($link.is(escapeSelector(exclude[elCounter++]))) {
include = false;
}
}
while ( include && ewlCounter < excludeWithin.length ) {

while (include && ewlCounter < excludeWithin.length) {
if ($link.closest(excludeWithin[ewlCounter++]).length) {
include = false;
}
}
}

if ( include ) {

if ( thisOpts.preventDefault ) {
if (include) {
if (thisOpts.preventDefault) {
event.preventDefault();
}

$.extend( clickOpts, thisOpts, {
$.extend(clickOpts, thisOpts, {
scrollTarget: thisOpts.scrollTarget || thisHash,
link: link
});

$.smoothScroll( clickOpts );
$.smoothScroll(clickOpts);
}
};

if (options.delegateSelector !== null) {
this
.undelegate(options.delegateSelector, 'click.smoothscroll')
.delegate(options.delegateSelector, 'click.smoothscroll', clickHandler);
.undelegate(options.delegateSelector, 'click.smoothscroll')
.delegate(options.delegateSelector, 'click.smoothscroll', clickHandler);
} else {
this
.unbind('click.smoothscroll')
.bind('click.smoothscroll', clickHandler);
.unbind('click.smoothscroll')
.bind('click.smoothscroll', clickHandler);
}

return this;
}
});

$.smoothScroll = function(options, px) {
if ( options === 'options' && typeof px === 'object' ) {
if (options === 'options' && typeof px === 'object') {
return $.extend(optionOverrides, px);
}
var opts, $scroller, scrollTargetOffset, speed, delta,
scrollerOffset = 0,
offPos = 'offset',
scrollDir = 'scrollTop',
aniProps = {},
aniOpts = {};
var opts, $scroller, scrollTargetOffset, speed, delta;
var scrollerOffset = 0;
var offPos = 'offset';
var scrollDir = 'scrollTop';
var aniProps = {};
var aniOpts = {};

if (typeof options === 'number') {
opts = $.extend({link: null}, $.fn.smoothScroll.defaults, optionOverrides);
scrollTargetOffset = options;
} else {
opts = $.extend({link: null}, $.fn.smoothScroll.defaults, options || {}, optionOverrides);

if (opts.scrollElement) {
offPos = 'position';

if (opts.scrollElement.css('position') === 'static') {
opts.scrollElement.css('position', 'relative');
}
Expand All @@ -220,9 +231,10 @@

scrollDir = opts.direction === 'left' ? 'scrollLeft' : scrollDir;

if ( opts.scrollElement ) {
if (opts.scrollElement) {
$scroller = opts.scrollElement;
if ( !(/^(?:HTML|BODY)$/).test($scroller[0].nodeName) ) {

if (!(/^(?:HTML|BODY)$/).test($scroller[0].nodeName)) {
scrollerOffset = $scroller[scrollDir]();
}
} else {
Expand All @@ -234,8 +246,8 @@

scrollTargetOffset = (typeof options === 'number') ? options :
px ||
( $(opts.scrollTarget)[offPos]() &&
$(opts.scrollTarget)[offPos]()[opts.direction] ) ||
($(opts.scrollTarget)[offPos]() &&
$(opts.scrollTarget)[offPos]()[opts.direction]) ||
0;

aniProps[scrollDir] = scrollTargetOffset + scrollerOffset + opts.offset;
Expand Down Expand Up @@ -274,18 +286,14 @@
$.smoothScroll.version = version;
$.smoothScroll.filterPath = function(string) {
string = string || '';

return string
.replace(/^\//,'')
.replace(/(?:index|default).[a-zA-Z]{3,4}$/,'')
.replace(/\/$/,'');
.replace(/^\//, '')
.replace(/(?:index|default).[a-zA-Z]{3,4}$/, '')
.replace(/\/$/, '');
};

// default options
$.fn.smoothScroll.defaults = defaults;

function escapeSelector (str) {
return str.replace(/(:|\.|\/)/g,'\\$1');
}

}));

Loading

0 comments on commit d28ff55

Please sign in to comment.