Use justify-content: flex-end and to have vertical scrollbar

前端 未结 5 425
無奈伤痛
無奈伤痛 2020-12-16 09:32

I have chat and I need to scroll all content to bottom. I want to use justify-content: flex-end and to have vertical scrollbar.

5条回答
  •  长情又很酷
    2020-12-16 10:22

    It seems to be a common bug among the browsers.

    You should distribute your style onto 2 containers: the outer will be scrolled, and the inner will be a flex container. Also, you need some js to keep your message list scrolled to bottom while adding new messages.

    Here is an example of code:

    markup:

    Item 1
    Item 2
    ...

    style:

    #inner-scroll {
        height: 100%;
        overflow: auto;
    }
    
    #inner-flex {
        display: flex;
        flex-direction: column;
        justify-content: flex-end;
        min-height: 100%;
    }
    
    .flex-item { /*nothing*/ }
    

    JS:

    function messagePushCallback()
    {
        var scrollable=document.getElementById('inner-scroll');
        scrollable.scrollTo(0, scrollable.scrollHeight-scrollable.clientHeight);
    }
    
    // for an example
    chat.onMessagePush(messagePushCallback);
    
    window.addEventListener('load', messagePushCallback);
    

    In JS, scrollable.scrollHeight shows the whole height of the element, including the space beyond its visible part, while scrollable.clientHeight is for the height of the visible part.

提交回复
热议问题