Sunday, January 27, 2013

‘Back to top’ link using jQuery

Html code: 
 <div id="toTop">^ Back to Top</div>
 
 
Css Code:
#toTop {
 width:100px;
        border:1px solid #ccc;
        background:#f7f7f7;
        text-align:center;
        padding:5px;
        position:fixed; /* this is the magic */
        bottom:10px; /* together with this to put the div at the bottom*/
        right:10px;
        cursor:pointer;
        display:none;
        color:#333;
        font-family:verdana;
        font-size:11px;
}
 
 
Function:
 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
 $(window).scroll(function() {
  if($(this).scrollTop() != 0) {
   $('#toTop').fadeIn(); 
  } else {
   $('#toTop').fadeOut();
  }
 });
 
 $('#toTop').click(function() {
  $('body,html').animate({scrollTop:0},800);
 }); 
});
</script>  
 
 

No comments:

Post a Comment

Opps Part 1 : Abstraction

  Abstraction in C# is a fundamental concept of object-oriented programming (OOP) that allows developers t...