2

i was wondering if is online any jquery plugin that help me to scroll page when i'm at bottom of this.

i mean,when i scroll my page to bottom side ,i would like a button that appears and clicking it i can return to the top of page

any suggestions?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
mystesso
  • 71
  • 1
  • 1
  • 4
  • (just wondering), why do you want to use javascript for that? Wouldn't `` at top and `Top` at the bottom of the page be enough? – M4N Sep 12 '10 at 11:34
  • you're right man.....but i would like to fadeIn the button when i'm at the bottom of the page so i'm looking to something in js ;) – mystesso Sep 12 '10 at 12:28

3 Answers3

3

How to tell when you're at the bottom of the page:

if (  document.documentElement.clientHeight + 
      $(document).scrollTop() >= document.body.offsetHeight ) { 
    ... 
}

Here's how to scroll to the top of the page:

$('html, body').animate({scrollTop:0}, 'slow');

Here's how to combine those to and fade a link in to scroll to the top of the page when you hit the bottom of the page (the link only gets reset if you click on it, since that made more sense to me).

This makes use of .animate(). Out of 4 possible arguments, I used 3: properties, duration, and a callback.

jsFiddle example


$(function() {
    $(window).scroll(function() {
        var totalHeight, currentScroll, visibleHeight;        
          // How far we've scrolled
        currentScroll = $(document).scrollTop();
          // Height of page
        totalHeight = document.body.offsetHeight;
          // Height visible
        visibleHeight = document.documentElement.clientHeight;
          // If visible + scrolled is greater or equal to total
          //   we're at the bottom
        if (visibleHeight + currentScroll >= totalHeight) {
              // Add to top link if it's not there
            if ($("#toTop").length === 0) {
                var toTop = $("<a>");
                toTop.attr("id", "toTop");
                toTop.attr("href", "#");
                toTop.css("display", "none");
                toTop.html("Scroll to Top of Page");
                  // Bind scroll to top of page functionality
                toTop.click(function() {
                      // Use animate w properties, duration and a callback
                    $('html, body').animate({scrollTop:0}, 'slow', function() {
                        $("#toTop").remove();
                    });
                    return false;
                });
                $("body").append(toTop);
                $("#toTop").fadeIn(3000);
            }
        }
    });
});
Community
  • 1
  • 1
Peter Ajtai
  • 56,972
  • 13
  • 121
  • 140
1

You can use something like this. It animates the screen to the top of the page.

$(document).ready(function() {
    $('.backtotop').click(function(){ // requires a class backtotop in the a element.
        $('html, body').animate({scrollTop:0}, 'slow');
    });
    return false;
});
Tim
  • 9,351
  • 1
  • 32
  • 48
  • uhm.... it should be really good but i want to fadeIn the a element when i'm in exactly bottom of page....not before :P ehehe – mystesso Sep 12 '10 at 12:29
1

Check this out. Go to the bottom of page and the button is shown. When clicking on it, it scrolls to the page top.

Ant, this is a demo for another plugin.

Mohsen
  • 3,512
  • 3
  • 38
  • 66