Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scroll down page by the height of current viewport

Tags:

jquery

scroll

I have a page in which I have a fixed positioned button, which when clicked should calculate the height of the viewport, and then scroll down the page by that height. ie. to the next viewport. When the user reaches the point when there is no more room to scroll I want to hide this button. Not sure how to do this, so far I have this:

$(document).on('click', '.next-viewport-down', function(event) {
  event.preventDefault();
  var viewportHeight = $(window).height();
  $('html, body').stop(true,true).animate({ ... }, 2000);
});
like image 290
user1937021 Avatar asked Mar 31 '15 10:03

user1937021


People also ask

How can I calculate with height of my browser viewport?

You can use the window. innerHeight property to get the viewport height, and the window. innerWidth to get its width. let viewportHeight = window.

How do I change the scroll height in jQuery?

One need to use scrollHeight property to get height of the scroll view of an element. If you are using jQuery 1.7 or higher version then use prop(), otherwise use attr(). In the code sample, I have used "prop()" to get value of "scrollHeight" property.

What is scrollTop?

scrollTop property gets or sets the number of pixels that an element's content is scrolled vertically. An element's scrollTop value is a measurement of the distance from the element's top to its topmost visible content. When an element's content does not generate a vertical scrollbar, then its scrollTop value is 0 .


1 Answers

Try this.

$(document).on('click', '.next-viewport-down', function(event){                        
    event.preventDefault();
    var viewportHeight = $(window).height();

    $('html, body').animate({
        scrollTop: viewportHeight,
        complete: function () {
            //Hide your button here
        }
    }, 2000);
});
like image 167
Alvin Magalona Avatar answered Oct 19 '22 17:10

Alvin Magalona