基本上,我正在尝试使聊天消息可滚动。但是,到目前为止,该内容似乎不可滚动。当“ chat-messages-wrapper”类是相对的时,它可以很好地滚动,但是我希望聊天消息最初显示在底部,这就是为什么我将其切换为绝对的。

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/

最佳答案

为了使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;
}

10-05 21:07
查看更多