绝对的孩子不可滚动

时间:2014-04-01 14:25:26

标签: html css css3

基本上我试图让聊天消息可滚动。然而,截至目前,内容似乎不可滚动。当"聊天消息包装" class是相对的,它滚动得很好但是我希望聊天消息最初显示在底部,这就是为什么我把它切换到绝对值。

HTML:

<div class="chat-widget">
   <div class="header">Social Chat</div>
   <div class="chat-messages-wrapper">
      <div class="chat-message">
         <img class="profile-image" src="../Content/images/facebook-profile.png" /> <span class="header-1">
         <img class="logo" src="../Content/images/facebook-logo.png" />
         <span class="username">Jens Olsen says:</span>
         </span> <span class="header-2">
         <button class="reply btn btn-primary btn-xs">Reply</button>
         <button class="like btn btn-success btn-xs">Like</button>
         <button class="spam btn btn-danger btn-xs">Spam</button>
         <span class="date">5:03PM</span>
         </span>
         <div class="text">This is the chat message. This is the chat message.</div>
      </div>
      <div class="chat-message">
         <img class="profile-image" src="../Content/images/facebook-profile.png" /> <span class="header-1">
         <img class="logo" src="../Content/images/twitter-logo.png" />
         <span class="username">Jens Olsen says:</span>
         </span> <span class="header-2">
         <button class="reply btn btn-primary btn-xs">Reply</button>
         <button class="like btn btn-success btn-xs">Like</button>
         <button class="spam btn btn-danger btn-xs">Spam</button>
         <span class="date">5:03PM</span>
         </span>
         <div class="text">This is the chat message. This is the chat message.</div>
      </div>
      <div class="chat-message">
         <img class="profile-image" src="../Content/images/default-avatar.png" /> <span class="header-1">
         <span class="username">Jens Olsen says:</span>
         </span> <span class="header-2">
         <button class="reply btn btn-primary btn-xs">Reply</button>
         <button class="like btn btn-success btn-xs">Like</button>
         <button class="spam btn btn-danger btn-xs">Spam</button>
         <span class="date">5:03PM</span>
         </span>
         <div class="text">This is the chat message. This is the chat message.</div>
      </div>
   </div>
   <div class="reply-box">
      <input class="reply-text" placeholder="Type your message here..." />
      <button class="btn btn-primary reply-button">Send</button>
   </div>
</div>

的CSS:

.chat-widget {
    position: fixed;
    right: 5px;
    bottom: 5px;
    height: 95%;
    width: 300px;
    background-color: #FFF;
    border: solid 3px #3B5998;
    overflow: hidden;
}

    .chat-widget .header {
        text-align: center;
        font-size: 18px;
        color: #FFF;
        background-color: #3B5998;
        font-weight: bold;
        padding-top: 5px;
        padding-bottom: 5px;
    }

.header {
    position: relative;
    z-index: 2;
    width: 100%;
}

.chat-messages-wrapper {
    position: absolute;
    bottom: 40px;
    padding-left: 7px;
    padding-right: 7px;
    height: auto;
    overflow-y: auto;
}

.chat-message {
    padding-top: 5px;
    position: relative;
    border-bottom: solid 1px #000;
    overflow-y: auto;
}

    .chat-message .profile-image {
        height: 48px;
        width: 48px;
        border: solid 1px #000;
        margin-right: 5px;
    }

    .chat-message > .header-1 {
        position: absolute;
        height: 20px;
    }

        .chat-message > .header-1 .logo {
            width: 75px;
            height: 20px;
        }

        .chat-message > .header-1 .username {
            font-weight: bold;
        }

    .chat-message > .header-2 {
        position: absolute;
        top: 30px;
        height: 20px;
    }

        .chat-message > .header-2 > .reply {
            width: 50px;
        }

        .chat-message > .header-2 > .like {
            width: 50px;
        }

        .chat-message > .header-2 > .spam {
            width: 50px;
        }

        .chat-message > .header-2 .date {
            font-style: italic;
            position: relative;
            right: 0px;
        }

    .chat-message .text {
        position: relative;
        margin: 5px;
    }

    .chat-message:last-child {
        border-bottom: 0px;
    }

.reply-box {
    overflow-y: auto;
    position: absolute;
    bottom: 5px;
}

    .reply-box .reply-text {
        width: 70%;
        height: 32px;
    }

    .reply-box .reply-button {
        width: 25%;
    }

这是一个包含整个代码的JSFiddle: http://jsfiddle.net/5GCE5/1/

2 个答案:

答案 0 :(得分:1)

您需要.chat-messages-wrapper max-height

http://jsfiddle.net/5GCE5/6/

答案 1 :(得分:1)

要使overflow-y生效,您需要为height元素指定显式(最小/最大).chat-messages-wrapper

但是,由于该框绝对定位,您可以使用topbottom属性来实现相同的效果:

<强> Example Here

.chat-messages-wrapper {
    position: absolute;
    padding-left: 7px;
    padding-right: 7px;
    /* height: auto; */
    bottom: 40px; /* = height of bottom element */
    top: 32px;    /* = height of top element    */
    overflow-y: auto;
}