Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll to Top and Scroll to Bottom in Jquery

I tried to implement the scroller with top to bottom and bottom to top with jquery. I recently tried with percent. From the Top to bottom with pixel is seems ok.(Means it works) for the bottom to top is only make scroll not completely finish if i mention percentage as 100

$('#spnTop').on("click",function(){
    var percentageToScroll = 100;
    var percentage = percentageToScroll/100;
    var height = jQuery(document).height();
    var documentHeight = $(document).height();
    var scroll = $(window).scrollTop();
    alert(scroll);
    var scrollAmount = Math.round((height) * percentageToScroll/ 100)-scroll;

    //alert(point);
    alert(scrollAmount);
    $('html,body').animate({ scrollTop: scrollAmount }, 'slow', function () {
                          alert("reached top");    });

});

Here is the fiddle. For Example: percentageToScroll is now 100 but the ending of scroll is not completely finish. (from bottom to top) For top to bottom is 100 then it completely scroll to bottom of the page.

I am not sure how to make it workable.

Thanks.

Vicky

like image 657
Vignesh Pichamani Avatar asked Nov 19 '25 06:11

Vignesh Pichamani


1 Answers

What about this?

$('#spnTop').on("click",function(){
    var percentageToScroll = 100;
    var percentage = percentageToScroll/100;
    var height = $(document).scrollTop();
    var scrollAmount = height * (1 - percentage);

    alert(scrollAmount);
    $('html,body').animate({ scrollTop: scrollAmount }, 'slow', function () {
                          alert("reached top");    });

});
$('#spnbottom').on("click",function() {
    var percentageToScroll = 100;
    var height = $(document).innerHeight();
    var scrollAmount = height * percentageToScroll/ 100;
    alert(scrollAmount);
     var overheight = jQuery(document).height() - jQuery(window).height();
    //alert(overheight);
jQuery("html, body").animate({scrollTop: scrollAmount}, 900);    
});

Fiddle here

like image 160
Jacques Snyman Avatar answered Nov 20 '25 19:11

Jacques Snyman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!