Sunday 6 March 2011

Adding a scollTop link to blogger.com

After "Adding jQuery to your blogger.com" I decided it was time to add my first little hack to every blog post I make. So the first item on the agenda is a link to scroll to the top of the page (see it in action at the bottom right of this page...) this isn't something new, but I decided I would do things my way, so I'll share how I have achieved this.



The beauty of this link and it is a link, not an image ;) is, it will only appear once the visitor has scrolled down from the top of the page and disappear again once they are back at the top. And it uses jQuery's .animate() function to make the users experience that little bit more pleasant. 

Lets get started:

OK so to start with, we will need to add a link to the page. It isn't really too important where on the page its added as we will be using CSS to move its location and make it look pretty, but inside the <body>..</body> tags will be best.
  1. Login to your blog. At the top right of the page (for me anyway) click Design
  2. Click on the Edit HTML tab.
  3. Now search for the line: </body>
  4. Now just above the tag you want to add the line (set the text to what ever you want to appear):
  5. <a href='#' id='toTop'>Scroll to Top</a>
Next we want to add the CSS to the page to make our link look a little more appealing. We can use the Template Designer to add the CSS, this will ensure the code is in the correct place.

  1. Now click on the Template Designer tab.
  2. On the list (top left) click on Advanced.
  3. Now scroll down and click on Add CSS.
  4. In the input box, add the following (change the colors and things to suite) and click Apply to Blog:
  5. /* to top */
    #toTop {
      width: 100px;
      background: #99d9f2;
      border: 1px solid #bce1a6;
      text-align: center;
      padding: 5px;
      position: fixed;
      bottom: 10px;
      right: 10px;
      cursor: pointer;
      color: #333333;
      text-decoration: none;
    }

Now we will have a link appear on the page at the bottom right, it will technically return the visitor to the top of the page, but not in a nice way, so onto the jQuery that will do the niceties for us:
  1. Return to the Edit HTML tab, in the Design section.
  2. Now we need to add some jQuery code to the section we setup in: Adding jQuery to your blogger.com. Add the following lines:
  3. $("#toTop").hide().removeAttr("href");
     if($(document).scrollTop()!="0") {
      $("#toTop").fadeIn("slow");
     }
     $(window).scroll(function(){
      if($(window).scrollTop()=="0"){
       $("#toTop").hide("slow");
      }
      else{
       $("#toTop").show("slow"); 
      }
     });
    
     $("#toTop").click(function(){
      $("html, body").animate({ scrollTop: "0px" });
     });
    
  4. The result will be something like this:
  5. <script type='text/javascript'>
    //<![CDATA[
    $(document).ready(function() {
     $("#toTop").hide().removeAttr("href");
     if($(document).scrollTop()!="0") {
      $("#toTop").fadeIn("slow");
     }
     $(window).scroll(function(){
      if($(window).scrollTop()=="0"){
       $("#toTop").hide("slow");
      }
      else{
       $("#toTop").show("slow"); 
      }
     });
    
     $("#toTop").click(function(){
      $("html, body").animate({ scrollTop: "0px" });
     });
    });
    //]]>
    </script>
    
  6. Now save the template, and take a look at your work!!
If you have followed everything, you should have a link appear once you scroll away from the top of a post, and if you click the link, you will be scrolled to the top of the page.

Scoobler.

Some jQuery books from Amazon.com:

jQuery 1.4 Animation Techniques: Beginners Guide     jQuery For Dummies     jQuery Pocket Reference

No comments:

Scroll to Top