简体   繁体   English

聊天框滚动到底部滚动到页面末尾而不是聊天结束

[英]Chat box scroll to bottom scroll to the end of the page not end of the chat

i have chatbox and this is my html code我有聊天框,这是我的 html 代码

                         <div class="card-body msg_card_body card-footer" >
                            <template v-for='(data,data_id) in active_chat_datas ' class='card-footer' id='scrollable' >
                                <div class="d-flex justify-content-start mb-4 last-footer" v-if="data['to_facebook_id'] == null">
                                    <div class="img_cont_msg">
                                        <img :src="active_customer['facebook_messages_link']" class="rounded-circle user_img_msg" />
                                    </div>
                                    <div class="msg_cotainer">
                                        @{{data['message']}}
                                        <span class="msg_time" style='width:250px;'>@{{date_format(data['created_at'])}}</span>
                                    </div>
                                </div>
                                <div v-else class="d-flex justify-content-end mb-4 last-footer">
                                    <div class="msg_cotainer_send">
                                        @{{data['message']}}
                                        <span class="msg_time_send" style='width:100px;'>@{{date_format(data['created_at'])}}</span>
                                    </div>
                                    <div class="img_cont_msg">
                                        <img src="https://static.turbosquid.com/Preview/001292/481/WV/_D.jpg" class="rounded-circle user_img_msg" />
                                    </div>
                                </div>
                            </template>
                            <div class="d-flex justify-content-end mb-4" id='myid'>
                                <div class="msg_cotainer_send ">
                                    <span class="msg_time_send" style='width:100px;'></span>
                                </div>
                            </div>
                        </div>

and i have this js code我有这个js代码

$('#myid').stop ().animate ({
    scrollTop: $('#myid')[0].scrollHeight
});

when the code当代码

scrollTop: $('#myid')[0].scrollHeight

execute the scrol go to the end of the page not of the chat like this pictures执行滚动到页面末尾而不是像这样的聊天在此处输入图像描述

but i need it to scroll to the botton of the chat like this picture但我需要它像这张图片一样滚动到聊天的底部在此处输入图像描述

and this is sandbox link https://bo1lej.csb.app/这是沙盒链接https://bo1lej.csb.app/

https://codesandbox.io/s/bo1lej https://codesandbox.io/s/bo1lej

how can i do that thanks ..我该怎么做谢谢..

Please try this :请试试这个:

<script>
 $(document).ready(function () {
   $(".msg_card_body").scrollTop($(".msg_card_body")[0].scrollHeight);
 });
</script>

this anwser is based on the sandbox you provided此 anwser 基于您提供的沙箱

Try this code below,试试下面的代码,

$("#custommyid").animate(
  { scrollTop: $("#custommyid")[0].scrollHeight },
  2000
);

This is the working code.这是工作代码。 click here In the provided sandbox,I make scrolling the chat app when the page loads ,since you didnt provide the chat url for getting chat messages and load it on the frontend. 单击此处在提供的沙箱中,我在页面加载时滚动聊天应用程序,因为您没有提供用于获取聊天消息的聊天 URL 并将其加载到前端。

I will give some knowledge about this.我会提供一些关于这方面的知识。 Because later someone can revist this and can learn this instead of just looking the code.animate function use to smoth the scrolling functionality.因为以后有人可以修改这个并且可以学习这个而不是仅仅查看用于平滑滚动功能的 code.animate 函数。 we can set this trasition.In here I set 2000ms seconds.我们可以设置这个 trasition。在这里我设置了 2000ms 秒。 And there some important point there.You can see that I have used "scrollHeight".It means total container height.there are couple of other concepts there.just see below还有一些重要的一点。你可以看到我使用了“scrollHeight”。它表示容器的总高度。那里还有其他几个概念。见下文

scrollTop -by now how amount of height user has scroll scrollTop - 现在用户滚动了多少高度

clientHeight -how much of amount that user see in the container clientHeight - 用户在容器中看到的数量

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

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