Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function($){
- $(document).ready(function(){
- var $window = $(window),
- $body = $(document.body),
- $doc = $('.doc-container'),
- $docnav = $doc.find ('.doc-nav');
- // name your elements here
- var stickyElement = '.doc-nav', // the element you want to make sticky
- bottomElement = '.ccomment row-fluid'; // the bottom element where you want the sticky element to stop (usually the footer)
- // make sure the element exists on the page before trying to initalize
- if($( stickyElement ).length){
- $( stickyElement ).each(function(){
- var fromTop = $( this ).offset().top,
- fromBottom = $( document ).height()-($( this ).offset().top + $( this ).outerHeight()),
- stopOn = $( document ).height()-( $( bottomElement ).offset().top)+($( this ).outerHeight() - $( this ).height());
- if( (fromBottom-stopOn) > 200 ){
- $( this ).css('width', $( this ).width()).css('top', 0).css('position', '');
- $( this ).affix({
- offset: {
- // make it stick where the top pixel of the element is
- top: fromTop,
- // make it stop where the top pixel of the bottom element is
- bottom: stopOn
- }
- }).on('affix.bs.affix', function(){ $( this ).css('top', 0).css('position', ''); });
- }
- $( window ).trigger('scroll');
- });
- }
- $body.scrollspy({
- target: '.doc-sidebar'
- });
- // add progress bar for navigation
- $docnav.find ('a').before ($('<span class="docs-progress-bar" />'));
- $docnav.on ('activate activate.bs.scrollspy', function () {
- $body.scrollspy("refresh");
- var $active = $docnav.find('li.active');
- $active.prevAll().find('.docs-progress-bar').css ('width', '100%');
- $active.nextAll().find('.docs-progress-bar').css ('width', '0%');
- });
- $window.on ('scroll', function (event) {
- if (this.timeout) {
- clearTimeout(this.timeout);
- }
- this.timeout = setTimeout (function () {
- var $active = $docnav.find('li.active'),
- $progress = $active.find('.docs-progress-bar'),
- $scrollspy = $body.data('bs.scrollspy'),
- scrollTop = $scrollspy.$scrollElement.scrollTop() + $scrollspy.options.offset,
- scrollHeight = $scrollspy.$scrollElement[0].scrollHeight || $scrollspy.$body[0].scrollHeight,
- maxScroll = scrollHeight - $scrollspy.$scrollElement.height(),
- offsets = $scrollspy.offsets,
- targets = $scrollspy.targets,
- activeTarget = $scrollspy.activeTarget,
- i;
- if (scrollTop >= maxScroll) {
- $progress.css ('width', '100%');
- return ;
- }
- if (activeTarget && scrollTop <= offsets[0]) {
- $progress.css ('width', '0%');
- return ;
- }
- for (i = offsets.length; i--;) {
- if (scrollTop >= offsets[i]
- && (!offsets[i + 1] || scrollTop <= offsets[i + 1])) {
- var p1 = offsets[i],
- p2 = scrollTop,
- p3 = !offsets[i + 1] ? maxScroll : offsets[i + 1],
- p = (p2-p1)/(p3-p1)*100;
- $progress.css ('width', (p < 2 ? 2 : p) + '%');
- return ;
- }
- }
- }, 100);
- });
- });
- })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement