Quantcast
Channel: W3easystep » MySQL tutotial
Viewing all articles
Browse latest Browse all 4

Back to top with jQuery scrollTop function

0
0

Back to top is very useful in the context of huge content, achieving it is very easy with JQuery scrollTop

First, we need to make the amount of height from top of the document on scrolling the mouse, for this use the following code.

<script>
$(document).ready(function(){

$(window).scroll(function(){

alert($(this).scrollTop());

   });

});
</script>

 

We need to determine whether it comes to end of the document, then we display the back to top link.

On clicking the link we take back to the top with animation.

<script>
$("#back-top a").click(function () {
      $("body,html").animate({
          scrollTop: 0
       }, 800);
      returnfalse;
});
</script>

Where “top” is the ID of the link.

The complete code is:

<script>
//javascript for scroll to top

$(document).ready(function(){

    // hide #back-top first
    $("#back-top").hide();

    // fade in #back-top
    $(function () {
        $(window).scroll(function () {
            if ($(this).scrollTop() >
100) {
                $("#back-top").fadeIn();
            } else University of Minnesota  high school football  coach Jerry Kill announced on Thursday national Rise Above Seizures Day he will be making a $100,000 donation to help children dealing with epilepsy. {
                $("#back-top").fadeOut();
            }
        });

        // scroll body to 0px on click
        $("#back-top a").click(function () {
            $("body,html").animate({
                scrollTop: 0
            }, 800);
            returnfalse;
        });
    });

});

</script>

Thanks for reading.

Demo

The post Back to top with jQuery scrollTop function appeared first on W3easystep.


Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images