Advertisement
Guest User

Untitled

a guest
Nov 27th, 2015
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;(function($, window, document, undefined){
  2.  
  3.     // our plugin constructor
  4.     var OnePageNav = function(elem, options){
  5.         this.elem = elem;
  6.         this.$elem = $(elem);
  7.         this.options = options;
  8.         this.metadata = this.$elem.data('plugin-options');
  9.         this.$win = $(window);
  10.         this.sections = {};
  11.         this.didScroll = false;
  12.         this.$doc = $(document);
  13.         this.docHeight = this.$doc.height();
  14.     };
  15.  
  16.     // the plugin prototype
  17.     OnePageNav.prototype = {
  18.         defaults: {
  19.             navItems: 'a',
  20.             currentClass: 'current',
  21.             changeHash: false,
  22.             easing: 'swing',
  23.             filter: '',
  24.             scrollSpeed: 750,
  25.             scrollThreshold: 0.5,
  26.             begin: false,
  27.             end: false,
  28.             scrollChange: false
  29.         },
  30.  
  31.         init: function() {
  32.             // Introduce defaults that can be extended either
  33.             // globally or using an object literal.
  34.             this.config = $.extend({}, this.defaults, this.options, this.metadata);
  35.  
  36.             this.$nav = this.$elem.find(this.config.navItems);
  37.  
  38.             //Filter any links out of the nav
  39.             if(this.config.filter !== '') {
  40.                 this.$nav = this.$nav.filter(this.config.filter);
  41.             }
  42.  
  43.             //Handle clicks on the nav
  44.             this.$nav.on('click.onePageNav', $.proxy(this.handleClick, this));
  45.  
  46.             //Get the section positions
  47.             this.getPositions();
  48.  
  49.             //Handle scroll changes
  50.             this.bindInterval();
  51.  
  52.             //Update the positions on resize too
  53.             this.$win.on('resize.onePageNav', $.proxy(this.getPositions, this));
  54.  
  55.             return this;
  56.         },
  57.  
  58.         adjustNav: function(self, $parent) {
  59.             self.$elem.find('.' + self.config.currentClass).removeClass(self.config.currentClass);
  60.             $parent.addClass(self.config.currentClass);
  61.         },
  62.  
  63.         bindInterval: function() {
  64.             var self = this;
  65.             var docHeight;
  66.  
  67.             self.$win.on('scroll.onePageNav', function() {
  68.                 self.didScroll = true;
  69.             });
  70.  
  71.             self.t = setInterval(function() {
  72.                 docHeight = self.$doc.height();
  73.  
  74.                 //If it was scrolled
  75.                 if(self.didScroll) {
  76.                     self.didScroll = false;
  77.                     self.scrollChange();
  78.                 }
  79.  
  80.                 //If the document height changes
  81.                 if(docHeight !== self.docHeight) {
  82.                     self.docHeight = docHeight;
  83.                     self.getPositions();
  84.                 }
  85.             }, 250);
  86.         },
  87.  
  88.         getHash: function($link) {
  89.             return $link.attr('href').split('#')[1];
  90.         },
  91.  
  92.         getPositions: function() {
  93.             var self = this;
  94.             var linkHref;
  95.             var topPos;
  96.             var $target;
  97.  
  98.             self.$nav.each(function() {
  99.                 linkHref = self.getHash($(this));
  100.                 $target = $('#' + linkHref);
  101.  
  102.                 if($target.length) {
  103.                     topPos = $target.offset().top;
  104.                     self.sections[linkHref] = Math.round(topPos);
  105.                 }
  106.             });
  107.         },
  108.  
  109.         getSection: function(windowPos) {
  110.             var returnValue = null;
  111.             var windowHeight = Math.round(this.$win.height() * this.config.scrollThreshold);
  112.  
  113.             for(var section in this.sections) {
  114.                 if((this.sections[section] - windowHeight) < windowPos) {
  115.                     returnValue = section;
  116.                 }
  117.             }
  118.  
  119.             return returnValue;
  120.         },
  121.  
  122.         handleClick: function(e) {
  123.             var self = this;
  124.             var $link = $(e.currentTarget);
  125.             var $parent = $link.parent();
  126.             var newLoc = '#' + self.getHash($link);
  127.  
  128.             if(!$parent.hasClass(self.config.currentClass)) {
  129.                 //Start callback
  130.                 if(self.config.begin) {
  131.                     self.config.begin();
  132.                 }
  133.  
  134.                 //Change the highlighted nav item
  135.                 self.adjustNav(self, $parent);
  136.  
  137.                 //Removing the auto-adjust on scroll
  138.                 self.unbindInterval();
  139.  
  140.                 //Scroll to the correct position
  141.                 self.scrollTo(newLoc, function() {
  142.                     //Do we need to change the hash?
  143.                     if(self.config.changeHash) {
  144.                         window.location.hash = newLoc;
  145.                     }
  146.  
  147.                     //Add the auto-adjust on scroll back in
  148.                     self.bindInterval();
  149.  
  150.                     //End callback
  151.                     if(self.config.end) {
  152.                         self.config.end();
  153.                     }
  154.                 });
  155.             }
  156.  
  157.             e.preventDefault();
  158.         },
  159.  
  160.         scrollChange: function() {
  161.             var windowTop = this.$win.scrollTop();
  162.             var position = this.getSection(windowTop);
  163.             var $parent;
  164.  
  165.             //If the position is set
  166.             if(position !== null) {
  167.                 $parent = this.$elem.find('a[href$="#' + position + '"]').parent();
  168.  
  169.                 //If it's not already the current section
  170.                 if(!$parent.hasClass(this.config.currentClass)) {
  171.                     //Change the highlighted nav item
  172.                     this.adjustNav(this, $parent);
  173.  
  174.                     //If there is a scrollChange callback
  175.                     if(this.config.scrollChange) {
  176.                         this.config.scrollChange($parent);
  177.                     }
  178.                 }
  179.             }
  180.         },
  181.  
  182.         scrollTo: function(target, callback) {
  183.             var offset = $(target).offset().top;
  184.  
  185.             $('html, body').animate({
  186.                 scrollTop: offset
  187.             }, this.config.scrollSpeed, this.config.easing, callback);
  188.         },
  189.  
  190.         unbindInterval: function() {
  191.             clearInterval(this.t);
  192.             this.$win.unbind('scroll.onePageNav');
  193.         }
  194.     };
  195.  
  196.     OnePageNav.defaults = OnePageNav.prototype.defaults;
  197.  
  198.     $.fn.onePageNav = function(options) {
  199.         return this.each(function() {
  200.             new OnePageNav(this, options).init();
  201.         });
  202.     };
  203.  
  204. })( jQuery, window , document );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement