Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am make a simple demo where I am showing only two divs.When user scroll up it create a div and prepend to exiting div.when user scroll again it again create a div , and remove the last div (because i want to show on two div).But I am struck my i come down bottom of the div I want to append the divs which i remove .

I take example when I load "page_5" then scroll up "page_4" load , again "page_3" load and remove "page_5" .Now if I want to come down I want to remove "page_3" and load "page_5" with page_4 already load.

can we do that functionality ? here I am trying but not getting success ..
http://jsfiddle.net/N5LZB/7/[^]



XML
var pages = [page_1, page_2, page_3, page_4, page_5];
var backupPages=new Array();
var totalPage = "page_" + pages.length;
$("<div id='" + totalPage + "'>" + pages.pop() + "</div>").prependTo($("#fullContainer"));


    $("#fullContainer").scroll(function () {
        // top
        if ($(this).scrollTop() === 0 && pages.length) {
            console.log("up");
            var stringLoad = "page_" + pages.length;
            $("<div id='" + stringLoad + "'>" + pages.pop() + "</div>").prependTo($("#fullContainer"));

            //remove element at 2nd index
            $('#fullContainer').children().slice(2).remove()
        }
        if ($(this).scrollTop() >= $(this)[0].scrollHeight - document.body.offsetHeight) {
            console.log("down"+$('#fullContainer div:last-child').attr('id'));
        }
    });
Posted
Comments
Sergey Alexandrovich Kryukov 4-Mar-14 11:23am    
This kind of design looks highly questionable to me, especially the attempt to do it fully on the client side. Where are you getting new data to append? I doubt the whole idea makes sense.
—SA

1 solution

XML
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript" src="Scripts/jquery-2.1.0.js"></script>
    <script>
        $(window).scroll(function () {
            if ($(window).scrollTop() + $(window).height() == $(document).height())
            {
                var div = '<div style="height: 100px; background-color:#ccc;">This Div just Got Appended</div>';
                $('body').append(div);
            }
        });
    </script>
</head>
<body>
    <div style="height: 1000px">Scroll down!</div>
</body>
</html>



For Mor Details Refer http://jsfiddle.net/33PpH/1/[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900