反向无限滚动

时间:2016-10-28 12:22:48

标签: javascript jquery meteor infinite-scroll

我想制作信使,信息在聊天中显示,从下到上滚动。当您滚动到限制的顶部时,系统将加载更多消息。

加载新项目后scrollTop的控制位置问题。如何计算这个位置?

  • 在流星语境中给出了示例 - 但纯粹的js(理论)中的答案将有所帮助)

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 

});

也许这个例子完全错了,但我不明白如何反向无限滚动 - 从下到上。

更新:

我找到了这个公式,但它适用于普通滚动,我需要如何重建这个以进行反向滚动?

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

1 个答案:

答案 0 :(得分:0)

我用jquery解决了这个问题:

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

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