2013-05-11 262 views
2

我正在嘗試創建一個聊天窗口,它允許聊天消息(下面的#messages_window)將調整大小以適應聊天區域的標題和輸入區域之間的空白區域。當它調整大小時,它仍然需要滾動。動態高度DIV滾動

下面是HTML我有:

<div id="chat_window"> 
    <div id="header"> 

    </div> 
    <div id="messages_window"> 

    </div> 
    <div id="input_area"> 
     <input type="text" id="chat_input"/> 
     <br/> 
     <input type="button" style="float:right;" onclick="Javascript:sendMessage();" value="Send"/> 
    </div> 
</div> 

這裏是CSS:

#chat_window 
{ 
    position: relative; 
    width: 300px; 
    height: 100%; 
    border: 1px solid black; 
} 

#chat_window #header 
{ 
    width: 100%; 
    height: 30px; 
    background-color: #69acf1; 
    color: #ffffff; 
    border-bottom: 1px solid black; 
} 

#chat_window #messages_window 
{ 
    width: 100%; 
    /*overflow: hidden;*/ 
} 

#input_area 
{ 
    position: absolute; 
    bottom: 0; 
    width: 100%; 
    height: 60px; 
} 
#input_area #chat_input 
{ 
    width: 100%; 
} 
+0

請閱讀:[應該'嗨','謝謝',標語和致敬從帖子中刪除?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and -salutations-be-removed-from-posts) – Antony 2013-05-11 06:08:44

+0

從這麼多的電子郵件發送的SOrry,習慣的力量。謝謝。 – 2013-05-11 06:10:16

回答

3

你可以嘗試定位#headermessage_windowabsolute,並使用topbottom屬性,如你對輸入區域做了什麼:

#chat_window { 
    position: relative; 
    height:100%; 
    width: 300px; 
    border: 1px solid black; 
} 

#chat_window #header { 
    width: 100%; 
    height: 30px; 
    background-color: #69acf1; 
    color: #ffffff; 
    border-bottom: 1px solid black; 
} 

#chat_window #messages_window { 
    position:absolute; 
    width: 100%; 
    top:30px; 
    bottom:60px; 
    overflow-y: auto; 
} 

#input_area { 
    position: absolute; 
    bottom: 0; 
    width: 100%; 
    height: 60px; 
} 
#input_area #chat_input { 
    width: 98%; 
} 

jsfiddle

底部和的#messages_window需要頂部被調整到#headerinput_area的heignt。當內容太長時,滾動條會自動添加(overflow-y: auto;)。

+0

不客氣=) – 2013-05-11 08:35:41

+0

馬丁,我敢肯定,這不會讓人意外,但這在IE9中無法正常工作。 #input_area浮動到DIV的頂部。有任何想法嗎? – 2013-05-11 10:02:24

+1

哦,我現在已經絕對定位了#header,#messages_window和#input_area突破了#chat_window的範圍。最後一個應該是相對位置,以保持其他人在正確的位置,並且定義一個高度。我更新了我的答案中的代碼,並添加了jsfiddle http://jsfiddle.net/7NGG7/希望它現在可以工作了......我現在沒有IE,現在就試用它。 – 2013-05-11 10:14:51