-Annie-

NavBar-for-Golden-Page

Aug 16th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * stickyNavbar.js v1.3.3
  3.  * https://github.com/jbutko/stickyNavbar.js
  4.  * Fancy sticky navigation jQuery plugin with smart anchor links highlighting
  5.  *
  6.  * Developed and maintenained under MIT licence by Jozef Butko - www.jozefbutko.com
  7.  * http://www.opensource.org/licenses/MIT
  8.  
  9.  * Original jquery-browser code Copyright 2005, 2014 jQuery Foundation, Inc. and other contributors
  10.  * http://jquery.org/license
  11.  *
  12.  * CREDITS:
  13.  * Daniel Eden for Animate.CSS:
  14.  * http://daneden.github.io/animate.css/
  15.  * jQuery easing plugin:
  16.  * http://gsgd.co.uk/sandbox/jquery/easing/
  17.  *
  18.  * COPYRIGHT (C) 2014-2016 Jozef Butko
  19.  * https://github.com/jbutko
  20.  * LAST UPDATE: 14/04/2016
  21.  *
  22.  */
  23. ;(function($, window, document) {
  24.  
  25.   'use strict';
  26.  
  27.   $.fn.stickyNavbar = function(prop) {
  28.  
  29.     // Set default values
  30.     var options = $.extend({
  31.         activeClass: 'active', // Class to be added to highlight nav elements
  32.         sectionSelector: 'scrollto', // Class of the section that is interconnected with nav links
  33.         animDuration: 350, // Duration of jQuery animation as well as jQuery scrolling duration
  34.         startAt: 0, // Stick the menu at XXXpx from the top of the this() (nav container)
  35.         easing: 'swing', // Easing type if jqueryEffects = true, use jQuery Easing plugin to extend easing types - gsgd.co.uk/sandbox/jquery/easing
  36.         animateCSS: true, // AnimateCSS effect on/off
  37.         animateCSSRepeat: false, // Repeat animation everytime user scrolls
  38.         cssAnimation: 'fadeInDown', // AnimateCSS class that will be added to selector
  39.         jqueryEffects: false, // jQuery animation on/off
  40.         jqueryAnim: 'slideDown', // jQuery animation type: fadeIn, show or slideDown
  41.         selector: 'a', // Selector to which activeClass will be added, either 'a' or 'li'
  42.         mobile: false, // If false, nav will not stick under viewport width of 480px (default) or user defined mobileWidth
  43.         mobileWidth: 480, // The viewport width (without scrollbar) under which stickyNavbar will not be applied (due user usability on mobile)
  44.         zindex: 9999, // The zindex value to apply to the element: default 9999, other option is 'auto'
  45.         stickyModeClass: 'sticky', // Class that will be applied to 'this' in sticky mode
  46.         unstickyModeClass: 'unsticky' // Class that will be applied to 'this' in non-sticky mode
  47.       }, prop),
  48.       sections = $('.' + options.sectionSelector);
  49.  
  50.     // Make sections focusable by scripts
  51.     sections.attr('tabindex', -1);
  52.  
  53.     return this.each(function() {
  54.  
  55.       // cache variables
  56.       var $self = $(this),
  57.           $selfPosition = $self.css('position'), // Initial position of this,
  58.           $selfZindex = $self.css('zIndex'), // Z-index of this
  59.           thisHeight = $self.outerHeight(true), // Height of navigation wrapper
  60.           $selfScrollTop = $self.offset().top - thisHeight, // scrollTop position of this
  61.           $topOffset = $self.css('top') === 'auto' ? 0 : $self.css('top'), // Top property of this: if not set = 0
  62.           menuItems = options.selector === 'a' ? $self.find('li a') : $self.find('li'), // Navigation lists or links
  63.           menuItemsHref = $self.find('li a[href*="#"]'), // href attributes of navigation links
  64.           windowPosition = $(window).scrollTop();
  65.  
  66.       /* Smooth scrolling logic */
  67.       menuItems.click(function(e) {
  68.  
  69.         // get href attr
  70.         var href,
  71.             currentHref,
  72.             i,
  73.             sectionOffsets,
  74.             sectionsLength;
  75.  
  76.         // cache href value
  77.         if (options.selector === 'li') {
  78.           href = $(this).children('a').attr('href');
  79.         } else {
  80.           href = $(this).attr('href');
  81.         }
  82.  
  83.         // let normal links in navigation redirect to location
  84.         if (href.substring(0, 4) === 'http' || (href.substring(0, 5) === 'https' || href.substring(0, 7) === 'mailto:' || href.substring(0, 1) === '/')) {
  85.           return true;
  86.         }
  87.  
  88.         // prevent default click behaviour
  89.         e.preventDefault();
  90.  
  91.         // href attr of clicked nav link
  92.         currentHref = href.substr(1); // remove # character with substr
  93.         sectionsLength = sections.length;
  94.  
  95.         // create object which will hold all offsetTop values
  96.         // of each section
  97.         sectionOffsets = {};
  98.         for (i = 0; i < sectionsLength; i++) {
  99.           sectionOffsets[sections[i].id] = sections[i].offsetTop;
  100.         }
  101.  
  102.         var toScroll = $self.hasClass(options.unstickyModeClass) ? sectionOffsets[currentHref] - 2 * thisHeight + 2 + 'px' : sectionOffsets[currentHref] - thisHeight + 2 + 'px';
  103.  
  104.         // on nav click navigate to selected section
  105.         $('html, body').stop().animate({
  106.           scrollTop: toScroll
  107.         }, {
  108.           duration: options.animDuration,
  109.           easing: options.easing,
  110.           complete: function () {
  111.             // Set keyboard focus to selected section
  112.             document.getElementById(currentHref).focus();
  113.           }
  114.         });
  115.       });
  116.  
  117.  
  118.       /* Main function, then on bottom called window.scroll, ready and resize */
  119.       var mainFunc = function() {
  120.  
  121.         // cache window and window position from the top
  122.         var win = $(window),
  123.             windowPosition = win.scrollTop(),
  124.             windowWidth = win.width(),
  125.             windowHeight = win.height();
  126.  
  127.         // optional mobileWidth
  128.         if (!options.mobile && windowWidth < options.mobileWidth) {
  129.           $self.css('position', $selfPosition);
  130.           return;
  131.         }
  132.  
  133.         // everytime we scroll remove the activeClass, later on we add it if needed
  134.         menuItems.removeClass(options.activeClass);
  135.  
  136.         // add activeClass to the div that is passing the top of the window
  137.         sections.each(function() {
  138.           var top = $(this).offset().top - thisHeight,
  139.               bottom = $(this).outerHeight(true) + top;
  140.  
  141.           if ((windowPosition >= top) && (windowPosition <= bottom)) {
  142.             if (options.selector === 'a') {
  143.               $self.find('li a[href~="#' + this.id + '"]').addClass(options.activeClass);
  144.             } else {
  145.               $self.find('li a[href~="#' + this.id + '"]').parent().addClass(options.activeClass);
  146.             }
  147.           }
  148.         });
  149.  
  150.         /* 1.) As soon as we start scrolling */
  151.         if (windowPosition >= $selfScrollTop + options.startAt) {
  152.  
  153.           // add 'sticky' class to this as soon as 'this' is in sticky mode
  154.           $self.removeClass(options.unstickyModeClass).addClass(' ' + options.stickyModeClass);
  155.  
  156.           // as soon as scrolling starts set position of this() to fixed
  157.           $self.css({
  158.             'position': 'fixed',
  159.             'zIndex': options.zindex
  160.           }).stop();
  161.  
  162.           // if jQuery effects are turned on
  163.           if (options.jqueryEffects) {
  164.             if (!options.animateCSSRepeat) {
  165.               $self.hide().stop()[options.jqueryAnim](options.animDuration, options.easing);
  166.             }
  167.             $self.hide().stop()[options.jqueryAnim](options.animDuration, options.easing);
  168.  
  169.             // if animateCSS are turned on
  170.           } else if (options.animateCSS) {
  171.  
  172.             // if animateCSSRepeat == true animation will repeat on each scroll
  173.             if (options.animateCSSRepeat) {
  174.  
  175.               // restart the animation */
  176.               $self.addClass(options.cssAnimation + ' animated').one('animationend webkitAnimationEnd MSAnimationEnd oAnimationEnd', function(e) {
  177.                 $self.removeClass(options.cssAnimation + ' animated');
  178.               });
  179.             } else {
  180.  
  181.               // restart the animation just once
  182.               $self.addClass(options.cssAnimation + ' animated').one('animationend webkitAnimationEnd MSAnimationEnd oAnimationEnd');
  183.             }
  184.  
  185.             // else if jQuery and animateCSS are turned off
  186.           } else {
  187.             $self.stop(); // pin navigation to the top
  188.           }
  189.  
  190.           // if top of the window is over this() (nav container)
  191.         } else {
  192.  
  193.           // add 'sticky' class to this as soon as 'this' is in sticky mode */
  194.           $self.css({
  195.             'position': $selfPosition,
  196.             'zIndex': $selfZindex
  197.           }).removeClass(options.stickyModeClass).addClass(' ' + options.unstickyModeClass);
  198.         }
  199.  
  200.  
  201.         if (typeof lastSection !== 'undefined') {
  202.           // grab bottom position of last section
  203.           var lastSection = sections.last(),
  204.               lastSectionBottom = lastSection.offset().top + lastSection.outerHeight(true);
  205.  
  206.           /* 2.) As soon as we hit the bottom of the page */
  207.           if (win.scrollTop() + windowHeight >= $(document).height() && windowPosition <= lastSectionBottom) {
  208.  
  209.             // remove activeClass from menuItem before the last and add activeClass to the lastests one
  210.             menuItems.removeClass(options.activeClass).last().addClass(options.activeClass);
  211.           }
  212.  
  213.           /* 3.) As soon as we get back to the top of the page */
  214.           // if top of the window is over this() (nav container)
  215.           if (windowPosition <= $selfScrollTop - 2) {
  216.             $self.removeClass(options.cssAnimation + ' animated');
  217.  
  218.             // if jQuery effects are turned on
  219.             if (options.jqueryEffects) {
  220.  
  221.               // if we are at the very top of the page remove active class
  222.               if (windowPosition === 0) {
  223.                 menuItems.removeClass(options.activeClass);
  224.               }
  225.  
  226.               // if the top of the window is under the this() stick the nav and start the animation
  227.               if (windowPosition >= $selfScrollTop) {
  228.                 $self.css({
  229.                   'position': 'fixed',
  230.                   'zIndex': options.zindex
  231.                 }).hide().stop()[options.jqueryAnim](options.animDuration, options.easing);
  232.               } else {
  233.                 $self.css({
  234.                   'position': $selfPosition,
  235.                   'zIndex': options.zindex
  236.                 });
  237.               }
  238.  
  239.               // if jQuery effects are turned off
  240.             } else {
  241.  
  242.               // if we are at the very top of the page remove active class
  243.               if (windowPosition === 0) {
  244.                 menuItems.removeClass(options.activeClass);
  245.               }
  246.               // set initial position of this() and initial CSS top property
  247.               $self.css({
  248.                 'position': $selfPosition,
  249.                 'top': $topOffset
  250.               }).stop().animate({
  251.                 top: $topOffset
  252.               }, options.animDuration, options.easing);
  253.             }
  254.           } // ( windowPosition <= $selfScrollTop ) end
  255.         }
  256.  
  257.       };
  258.  
  259.       $(window).scroll(mainFunc); // scroll fn end
  260.       $(window).ready(mainFunc);
  261.       $(window).resize(mainFunc);
  262.       $(window).load(mainFunc);
  263.  
  264.     }); // return this.each end
  265.   }; // $.fn.stickyNavbar end
  266. })(jQuery, window, document); // document ready end
Advertisement
Add Comment
Please, Sign In to add comment