this._inTransition = true;
var _this = this;
- Foundation.SmoothScroll.scrollToLoc(loc, this.options, function() {
+ var options = {
+ animationEasing: this.options.animationEasing,
+ animationDuration: this.options.animationDuration,
+ threshold: this.options.threshold,
+ offset: this.options.offset
+ };
+
+ Foundation.SmoothScroll.scrollToLoc(loc, options, function() {
_this._inTransition = false;
_this._updateActive();
})
var isDown = this.scrollPos < winPos,
_this = this,
curVisible = this.points.filter(function(p, i){
- return isDown ? p - _this.options.barOffset <= winPos : p - _this.options.barOffset - _this.options.threshold <= winPos;
+ return isDown ? p - _this.options.offset <= winPos : p - _this.options.offset - _this.options.threshold <= winPos;
});
curIdx = curVisible.length ? curVisible.length - 1 : 0;
}
* @type {number}
* @default 0
*/
- barOffset: 0
+ offset: 0
}
// Window exports
}
/**
- * Initialize the SmoothScroll plugin
+ * Initialize the SmoothScroll plugin
+ * @private
*/
_init() {
var id = this.$element[0].id || Foundation.GetYoDigits(6, 'smooth-scroll');
this._events();
}
+ /**
+ * Initializes events for SmoothScroll.
+ * @private
+ */
_events() {
var _this = this;
- this.$element.on('click.zf.smoothScroll', 'a[href^="#"]', function(e) {
+ // click handler function.
+ var handleLinkClick = function(e) {
+ // exit function if the event source isn't coming from an anchor with href attribute starts with '#'
+ if(!$(this).is('a[href^="#"]')) {
+ return false;
+ }
+
+ var arrival = this.getAttribute('href');
+
+ _this._inTransition = true;
+
+ SmoothScroll.scrollToLoc(arrival, _this.options, function() {
+ _this._inTransition = false;
+ });
+
e.preventDefault();
- var arrival = this.getAttribute('href');
- _this.scrollToLoc(arrival);
- });
- }
+ };
- /**
- * Function to scroll to a given location on the page.
- * @param {String} loc - a properly formatted jQuery id selector. Example: '#foo'
- * @function
- */
- scrollToLoc(loc) {
- this._inTransition = true;
- var _this = this;
- this.constructor.scrollToLoc(loc, this.options, function() {
- _this._inTransition = false;
- });
+ this.$element.on('click.zf.smoothScroll', handleLinkClick)
+ this.$element.on('click.zf.smoothScroll', 'a[href^="#"]', handleLinkClick);
}
/**
* Function to scroll to a given location on the page.
- * @param {String} loc - a properly formatted jQuery id selector. Example: '#foo'
+ * @param {String} loc - A properly formatted jQuery id selector. Example: '#foo'
+ * @param {Object} options - The options to use.
+ * @param {Function} callback - The callback function.
* @static
* @function
*/
- static scrollToLoc(loc, options, callback) {
+ static scrollToLoc(loc, options = SmoothScroll.defaults, callback) {
// Do nothing if target does not exist to prevent errors
if (!$(loc).length) {
return false;
}
- var scrollPos = Math.round($(loc).offset().top - options.threshold / 2 - options.barOffset);
+ var scrollPos = Math.round($(loc).offset().top - options.threshold / 2 - options.offset);
$('html, body').stop(true).animate(
{ scrollTop: scrollPos },
);
}
+ /**
+ * Destroys an instance of SmoothScroll.
+ * @function
+ */
destory() {
Foundation.unregisterPlugin(this);
}
* @type {number}
* @default 0
*/
- barOffset: 0
+ offset: 0
}
// Window exports
Foundation.plugin(SmoothScroll, 'SmoothScroll');
-}(jQuery);
\ No newline at end of file
+}(jQuery);