简体   繁体   English

使溢出的内容滚动到元素上方(而不是下方)

[英]Make overflowing content scroll above element (instead of below)

I am trying to create a chat page on my website. 我正在尝试在我的网站上创建一个聊天页面。 One of the important features of this application is that old messages should be pushed up on the chat output area whenever a new message is received. 此应用程序的重要功能之一是,每当收到新消息时,旧消息都应在聊天输出区域上推。

The user should be able to scroll up to see messages in the chronological order that they were received. 用户应该能够向上滚动以按收到消息的时间顺序查看消息。 I have tried to do this ( http://jsfiddle.net/wasingej/rqee378d/ ). 我试图做到这一点( http://jsfiddle.net/wasingej/rqee378d/ )。 However, I cannot scroll up to see the overflowing content. 但是,我无法向上滚动以查看溢出的内容。

This works fine when content is overflowing below my element ( http://jsfiddle.net/wasingej/rqee378d/3/ ). 当内容在我的元素( http://jsfiddle.net/wasingej/rqee378d/3/ )下溢出时,此方法正常工作。

All that has changed between the two is the bottom property has been changed to top . 两者之间所有更改的是, bottom属性已更改为top Why won't my scroll bar appear in the first example? 为什么我的滚动条不会出现在第一个示例中?

Isn't this much simpler? 这不是更简单吗?

http://jsfiddle.net/rqee378d/4/ http://jsfiddle.net/rqee378d/4/

div.outer
{
    position: relative;
    min-height: 20px;
    overflow-y: scroll;
}

div
{
    position: relative;
}

And adding the text using a prepend. 并使用前缀添加文本。

http://api.jquery.com/prepend/ http://api.jquery.com/prepend/

Your scroll bar wont appear in your first example because you are positioning the element using absolute . 您的滚动条不会出现在第一个示例中,因为您使用absolute定位元素。 Please read more about positioning here . 在此处阅读有关定位的更多信息 Here is a quote: 这是一个报价:

What is happening is the absolutely positioned elements are positioning themselves in relation to the body element instead of their direct parent. 发生的情况是绝对定位的元素相对于body元素而不是其直接父元素定位于自身。

This is why your scrollbar is not appearing. 这就是为什么您的滚动条没有出现的原因。 If you make them block elements with a position of static then scrolling would work fine. 如果将它们设置为具有static位置的block元素,则滚动会很好。

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

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