SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    848
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    jQuery scroll to top not working

    Hi on this page http://bit.ly/d67oe4 I would like the 'Top' button just above the footer to scroll the page back to the top using jquery. I am using the script below in the <head> of the page but the effect isn't working. Any idea why?

    Code:
        <!-- Back to Top -->
        <script type="text/javascript">
        jQuery('a.backtotop').click(function(){
            alert('hello');
            jQuery('html, body').animate({scrollTop:0}, 'fast');
            return false;
        });
          </script>

  2. #2
    SitePoint Member
    Join Date
    Jul 2010
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to put it in a $(document).ready -- in the head, your selector doesn't actually exist yet, so your click event is never being called.

    Code:
    jQuery(document).ready(function() {
            jQuery('a.backtotop').click(function(){
            alert('hello');
            jQuery('html, body').animate({scrollTop:0}, 'fast');
            return false;
        });
    });

  3. #3
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    848
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    worked thanks!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •