简体   繁体   中英

Reverse infinity scroll

I want to make messenger, where messages are showing like in a chat where scroll from bottom to top. And when you scroll to the top of limit, system will load more messages.

The problem in control position of scrollTop after load new items. How to count this position?

  • given example in context of meteor - but answer in pure js (theory) will be helpful to)

HTML

<template name='chatBox'>
    <div class='chat-box'><!--viewport-->

        {{> chatBoxItem}}
        {{/each}}
    </div>
</template>

<template name='chatBoxItem'>
    <div class='chat-box-wrapper'><!--content block-->
        {{#each messeges}}
            <p>{{messege}}</p> <!--each messege-->
        {{/each}}
    </div>
</template>

JS

Template.chatBox.onRendered(function() {
    // viewport template

    $('.chat-box').scroll(function(e) {
        var chatBoxHeight = $('.chat-box').innerHeight(); 
        var wrapperHeight = $('#chat-box-wrapper').innerHeight();
        var chatBoxScroll = $('.chat-box').scrollTop();

        var currentScrollFromBottom = wrapperHeight - chatBoxScroll; //don't sure if this formula is correct but it's count how many pixels was scrolled from bottom to top

        if (currentScrollFromBottom  == wrapperHeight && MESSAGES_LIMIT < amountOfMessages) {

            //here some code to increase limit of queue

        }
    });


Template.chatBoxItem.onRendered(function () {
    // content template

    var $heightOfBlock = $('#chat-box-wrapper').height();
    $('.chat-box').scrollTop($heightOfBlock); 
    // Here I have the most problem, 
    // I need to scroll to bottom when page is load (work good)                     
    // but after loaded new items, it's need to stand still, but   
    // it's not. I don't know which formula i need     
    // to use to count current scroll position 

});

Maybe this example are wrong at all, but I don't understand how to make infinity scroll in reverse - from bottom to top.

UPDATE:

I found this formula, but it's for normal scrolling, how I need to rebuild this for reverse scroll?

$(window).scrollTop() + $(window).height() > $(document).height() - 100

I solved it like this with jquery:

var st=$("#chat").scrollTop();
$("#chat").prepend(me);
$("#chat").scrollTop(st+me.outerHeight());

https://jsfiddle.net/Lhmn07rg/8/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM