Skip to content

Commit

Permalink
do less calculations to improve the performance
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielRuf committed Feb 28, 2018
1 parent e50e15b commit 6371c6d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
29 changes: 19 additions & 10 deletions js/foundation.magellan.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,31 @@ class Magellan extends Plugin {
_this._updateActive();
});

this.$element.on({
'resizeme.zf.trigger': this.reflow.bind(this),
'scrollme.zf.trigger': this._updateActive.bind(this)
}).on('click.zf.magellan', 'a[href^="#"]', function(e) {
if (document.readyState === "complete") {
_this.$element.on({
'resizeme.zf.trigger': _this.reflow.bind(_this),
'scrollme.zf.trigger': _this._updateActive.bind(_this)
}).on('click.zf.magellan', 'a[href^="#"]', function(e) {
e.preventDefault();
var arrival = this.getAttribute('href');
var arrival = _this.getAttribute('href');
_this.scrollToLoc(arrival);
_this.calcPoints();
_this._updateActive();
});

} else {
$(window).one('load', function(){
_this.$element.on({
'resizeme.zf.trigger': _this.reflow.bind(_this),
'scrollme.zf.trigger': _this._updateActive.bind(_this)
}).on('click.zf.magellan', 'a[href^="#"]', function(e) {
e.preventDefault();
var arrival = _this.getAttribute('href');
_this.scrollToLoc(arrival);
});
});
}

this._deepLinkScroll = function(e) {
if(_this.options.deepLinking) {
_this.scrollToLoc(window.location.hash);
_this.calcPoints();
_this._updateActive();
}
};

Expand Down
8 changes: 6 additions & 2 deletions test/javascript/components/magellan.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ describe('Magellan', function() {

// Jump to last section
var target = $html.find('a').eq(-1).attr('href');
plugin.scrollToLoc(target);
$html.one('load', function(){
plugin.scrollToLoc(target);
});

// The `update` event doesn't work properly because it fires too often
setTimeout(function() {
Expand All @@ -82,7 +84,9 @@ describe('Magellan', function() {
var hasError = false;
try {
var target = $html.find('a').eq(-1).attr('href');
plugin.scrollToLoc(target);
$html.one('load', function(){
plugin.scrollToLoc(target);
});
} catch (err) {
hasError = true;
}
Expand Down

0 comments on commit 6371c6d

Please sign in to comment.