website_scroll_back2top.js 839 B

123456789101112131415161718192021222324252627
  1. $(document).ready(function()
  2. {
  3. $('body').prepend('<a href="#" class="back-to-top">Back to Top</a>');
  4. var amountScrolled = 300;
  5. $(window).scroll(function() {
  6. if ($(window).scrollTop() > amountScrolled) {
  7. $('a.back-to-top').fadeIn('slow');
  8. } else {
  9. $('a.back-to-top').fadeOut('slow');
  10. }
  11. var scrollTop = $(window).scrollTop();
  12. var docHeight = $(document).height();
  13. var winHeight = $(window).height();
  14. var scrollPercent = (scrollTop) / (docHeight - winHeight);
  15. var scrollPercentRounded = Math.round(scrollPercent*100);
  16. $('a.back-to-top').text(scrollPercentRounded+"%");
  17. });
  18. $('a.back-to-top').click(function() {
  19. $('body,html').animate({
  20. scrollTop: 0
  21. }, 'fast');
  22. return false;
  23. });
  24. });