javascript - Jquery Reading Position progress between article content -


in example http://jsfiddle.net/snjxq/61/
reading progress indicator it's width increased top of site !!

but need progress bar width begin increasing

when article content div reached until end of article content

and sample code need edit
html

<div class="bar-long"></div> <header>header & menu     <br>header , menu content     <p>header & menu         <br>header , menu content         <p>header & menu             <br>header , menu content             <p> </header>     <h1>begin article <br>(need show bar here) </h1>  <p>     <article>         <div class="bar-long2">article content             <br />article content             <br />article content             <br />article content             <br />article content             <br />article content             <br />article content             <br />article content             <br />article content             <br />content             <br />article content             <br />article content             <br />article content             <br />article content             <br />article content             <br />article content             <br />article content             <br />article content             <br />article content             <br />article content             <br />article content             <br />article content             <br />article content             <br />article content             <br />article content             <br />article content             <br />article content             <br />article content             <br />article content             <br />article content             <br />article content             <br />article content             <br />article content             <br />article content             <br />         </div>         <div class="bar-long3">              <h1>endendend<br> (need width bar 100%</h1>          </div>     </article>     <footer>          <h1>footer</h1>          <div class="footer">              <h4>footer</h4>                  <h4>footer</h4>               <h4>footer</h4>               <h4>footer</h4>                  <h4>footer</h4>          </div>     </footer> 

css

 .bar-long {      height: 5px;      background-color: #009acf;      width: 0px;      z-index: 1000;      position: fixed;      top: 50px;      left: 0;  }  body {      height:2000px;  } 

jquery

$(window).scroll(function () {       // calculate percentage user has scrolled down page      var scrollpercent = 100 * $(window).scrolltop() / ($(document).height() - $('article').height());       $('.bar-long').css('width', scrollpercent + "%");   }); 

its little complicated finally

$(window).scroll(function() {      // calculate percentage user has scrolled down page     var scrollwin = $(window).scrolltop();     var articleheight = $('article').outerheight(true);     var windowwidth = $(window).width();     if(scrollwin >= $('article').offset().top){         if(scrollwin <= ($('article').offset().top + articleheight)){             $('.bar-long').css('width', ((scrollwin - $('article').offset().top) / articleheight) * 100 + "%"  );         }else{             $('.bar-long').css('width',"100%");         }     }else{         $('.bar-long').css('width',"0px");     }   }); 

demo

let me explain going on here

the width percentage come part of article pass scrolltop , divided article height percentage of part

in if statement create 2nd if statement stop blue bar @ 100% not increase each time scroll down article

so whatever article height code work


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -