javascript - find percentage of page height jquery -
i trying write code div telling user how far has scrolled down page. here code:
$(document).scroll(function(){ var fullheight = $(this).height(); var currentheight = $(this).scrolltop(); var percentageofpage = currentheight/fullheight; percentageofpage = percentageofpage.tofixed(2); var t = $("#t"); t.html("you " + percentageofpage + " down page." ); });
fiddle
code works how should: writes out percentage how far user has scrolled. stops @ .67 or .69. why that? want go way 1. also, how can display percentage, 60%, instead of decimal, .6? here page is.
addition:
how can make @ when user reaches bottom of page, message becomes: "you have reached bottom of page", instead of percentage?
$(document).on('scroll.percentage', function() { var scrollpercent = math.round(100 * $(window).scrolltop() / ($(document).height() - $(window).height())); // if user has reached bottom of page // unbind scroll listener if (scrollpercent == 100) { $("#t").html("you have reached bottom of page."); $(this).off('scroll.percentage'); return; } $("#t").html("you " + scrollpercent + "% down page."); });
html, body { height: 1000px; } #t { position: fixed; top: 0; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div id="t"></div>
Comments
Post a Comment