简体   繁体   English

反向无限滚动

[英]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. 加载新项目后scrollTop控件位置的问题。 How to count this position? 怎么算这个位置?

  • given example in context of meteor - but answer in pure js (theory) will be helpful to) 在流星的背景下举例 - 但纯粹的js(理论)中的答案将有助于)

HTML 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 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: 我用jquery解决了这个问题:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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