Advertisement
Guest User

progressive.js

a guest
Jul 17th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function($){
  2.  
  3. $(document).ready(function(){
  4. var $window = $(window),
  5.         $body = $(document.body),
  6.         $doc = $('.doc-container'),
  7.         $docnav = $doc.find ('.doc-nav');
  8.  
  9.     // name your elements here
  10.     var stickyElement   = '.doc-nav',   // the element you want to make sticky
  11.         bottomElement   = '.ccomment row-fluid'; // the bottom element where you want the sticky element to stop (usually the footer)
  12.     // make sure the element exists on the page before trying to initalize
  13.     if($( stickyElement ).length){
  14.         $( stickyElement ).each(function(){
  15.             var fromTop = $( this ).offset().top,
  16.             fromBottom = $( document ).height()-($( this ).offset().top + $( this ).outerHeight()),
  17.                stopOn = $( document ).height()-( $( bottomElement ).offset().top)+($( this ).outerHeight() - $( this ).height());
  18.             if( (fromBottom-stopOn) > 200 ){
  19.                 $( this ).css('width', $( this ).width()).css('top', 0).css('position', '');
  20.                 $( this ).affix({
  21.                     offset: {
  22.                         // make it stick where the top pixel of the element is
  23.                         top: fromTop,  
  24.                         // make it stop where the top pixel of the bottom element is
  25.                         bottom: stopOn
  26.                     }
  27.                 }).on('affix.bs.affix', function(){ $( this ).css('top', 0).css('position', ''); });
  28.             }
  29.             $( window ).trigger('scroll');
  30.         });
  31.     }
  32.  
  33.    
  34.     $body.scrollspy({
  35.       target: '.doc-sidebar'
  36.     });  
  37.  
  38.     // add progress bar for navigation
  39.     $docnav.find ('a').before ($('<span class="docs-progress-bar" />'));
  40.  
  41.     $docnav.on ('activate activate.bs.scrollspy', function () {
  42.       $body.scrollspy("refresh");
  43.       var $active = $docnav.find('li.active');
  44.       $active.prevAll().find('.docs-progress-bar').css ('width', '100%');
  45.       $active.nextAll().find('.docs-progress-bar').css ('width', '0%');
  46.     });
  47.  
  48.     $window.on ('scroll', function (event) {
  49.       if (this.timeout) {
  50.         clearTimeout(this.timeout);
  51.       }
  52.       this.timeout = setTimeout (function () {
  53.         var $active = $docnav.find('li.active'),
  54.             $progress = $active.find('.docs-progress-bar'),
  55.             $scrollspy = $body.data('bs.scrollspy'),
  56.             scrollTop    = $scrollspy.$scrollElement.scrollTop() + $scrollspy.options.offset,
  57.             scrollHeight = $scrollspy.$scrollElement[0].scrollHeight || $scrollspy.$body[0].scrollHeight,
  58.             maxScroll    = scrollHeight - $scrollspy.$scrollElement.height(),
  59.             offsets      = $scrollspy.offsets,
  60.             targets      = $scrollspy.targets,
  61.             activeTarget = $scrollspy.activeTarget,
  62.             i;
  63.  
  64.         if (scrollTop >= maxScroll) {
  65.           $progress.css ('width', '100%');
  66.           return ;
  67.         }
  68.  
  69.         if (activeTarget && scrollTop <= offsets[0]) {
  70.           $progress.css ('width', '0%');
  71.           return ;
  72.         }
  73.         for (i = offsets.length; i--;) {
  74.           if (scrollTop >= offsets[i]
  75.             && (!offsets[i + 1] || scrollTop <= offsets[i + 1])) {
  76.             var p1 = offsets[i],
  77.                 p2 = scrollTop,
  78.                 p3 = !offsets[i + 1] ? maxScroll : offsets[i + 1],
  79.                 p = (p2-p1)/(p3-p1)*100;
  80.             $progress.css ('width', (p < 2 ? 2 : p) + '%');
  81.             return ;
  82.           }
  83.         }
  84.       }, 100);
  85.     });  
  86.  
  87. });
  88.  
  89. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement