diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bb93d68 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +bower_components \ No newline at end of file diff --git a/bower.json b/bower.json index c33d0e4..44b3ec7 100644 --- a/bower.json +++ b/bower.json @@ -10,6 +10,6 @@ "*.css" ], "dependencies": { - "jquery": "~1.8.0" + "jquery": ">=1.11.0" } -} \ No newline at end of file +} diff --git a/jquery.nav.js b/jquery.nav.js index 1296443..254484b 100644 --- a/jquery.nav.js +++ b/jquery.nav.js @@ -129,6 +129,9 @@ var returnValue = null; var windowHeight = Math.round(this.$win.height() * this.config.scrollThreshold); + // Sort Sections by Position + this.sections = this.sortSectionsByPosition(this.sections); + for(var section in this.sections) { if((this.sections[section] - windowHeight) < windowPos) { returnValue = section; @@ -138,6 +141,38 @@ return returnValue; }, + /** + * Sort Sections by its position value + * based on http://am.aurlien.net/post/1221493460/sorting-javascript-objects + * @param {Object} obj Object to sort + * @return {Object} sorted Object + */ + sortSectionsByPosition: function (obj) { + var tempArray = []; + var tempObj = {}; + + // Transform Object in Array + for (var key in obj) { + if (obj.hasOwnProperty(key)) { + tempArray.push(key); + } + } + + tempArray.sort(function(a,b) { + var x = obj[a]; + var y = obj[b]; + + return ((x < y) ? -1 : ((x > y) ? 1 : 0)); + }); + + // Transform sorted tempArray back into Object + for (var i=0; i