December 2, 2011

How to add load more button for a HTML/CSS page?

Question by Saiful Islam

I want to make a single page website and it will have huge content. Suppose it has 1000 photos on it. I don’t want people to wait 5 minutes to load my page. So I wanna add LOAD MORE button on the page bottom.

How to do that with HTML/CSS/JS?

Answer by Purmou

You could set all the divs’ to display:none; at first and then use jQuery to show the first 10 (or however many you wish to show):

$(function(){
    $("div").slice(0, 10).show(); // select the first ten
    $("#load").click(function(e){ // click event for load more
        e.preventDefault();
        $("div:hidden").slice(0, 10).show(); // select next 10 hidden divs and show them
        if($("div:hidden").length == 0){ // check if any hidden divs still exist
            alert("No more divs"); // alert if there are none left
        }
    });
});

Example.

This saves you the trouble of including an entire plugin when what you want can be achieved in a few lines of code.

Answer by Starx

The closest i can get reading your question, you want the effect which google and facebook uses nowadays to load posts.

Visit infinite-scroll.com
They have your answer.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!