绝对位置在滚动溢出容器中

时间:2018-06-21 14:43:01

标签: html css position

我有以下代码:

.chat-content {
  height: 455px;
  min-height: 100%;
  width: 100%;
  overflow-y: auto;
  background: #cacac8;
}

.chat-content,
.chat-content-inner {
  position: relative;
}

.chat-container {
  position: relative;
  overflow: hidden;
}

.top-buttons {
  position: relative;
  display: block;
  padding: 10px;
  padding-left: 80px;
  height: 50px;
  background: #dddddb;
  top: 0;
  right: 0;
  left: 0
}

.top-buttons .badge {
  position: relative;
  border-radius: 3px;
  padding: 7px;
  margin-left: 10px;
  margin-top: 5px;
}

.top-buttons .badge:after {
  content: "";
  position: absolute;
  top: 5px;
  left: -5px;
  border-style: solid;
  border-width: 5px 5px 5px 0px;
  border-color: transparent #999;
  display: block;
  width: 0;
  z-index: 1;
}

.top-buttons h2 {
  font-weight: 300;
  color: #858689
}

.chat-pusher {
  position: relative;
}


/*Styling Chat Sidebar Menu*/

.chat-users-menu {
  position: absolute;
  top: 0;
  left: -110px;
  z-index: 100;
  visibility: visible;
  width: 180px;
  height: 100%;
  background: #858689;
  -webkit-transition: all .5s;
  transition: all .5s;
  overflow-y: auto;
}


/*Toggle Class for Moving Chat Menu Users*/

.chatbar-toggle {
  left: 0;
}


/*/Toggle Class for Moving Chat Menu Users*/

.chat-users-menu ul {
  margin: 0;
  padding: 0;
  list-style: none;
}

.chat-users-menu .menu-header {
  padding: 10px;
  text-align: right;
  color: #fff;
  font-size: 1.5em;
}

.chat-users-menu ul li a {
  position: relative;
  display: block;
  height: 70px;
  padding: 15px 90px 10px 10px;
  outline: none;
  box-shadow: inset 0 -1px rgba(0, 0, 0, .2);
  color: #fff;
  text-shadow: 0 0 1px rgba(255, 255, 255, .1);
  font-weight: 700;
  -webkit-transition: background .3s, box-shadow .3s;
  transition: background .3s, box-shadow .3s;
}

.chat-users-menu ul li a .chat-name {
  display: block;
  text-overflow: ellipsis;
  width: 100px;
  font-size: 1.05em;
}

.chat-users-menu ul li a .badge {
  position: absolute;
  background: #d24d33;
  color: #fff;
  top: 10px;
  right: 10px;
}

.chat-users-menu ul li a:hover {
  background: rgba(0, 0, 0, .2);
  color: #fff;
  text-decoration: none;
}

.chat-users-menu ul li a .label {
  margin-top: 2px;
}

.chat-users-menu ul li .user-img {
  position: absolute;
  right: 15px;
  top: 15px;
  display: block;
  max-width: 40px;
}

.chat-users-menu ul li .user-img img {
  display: inline-block;
  max-width: 100%;
  border-radius: 50%;
  box-shadow: 0 0 0 5px #fff;
}


/* And finally! Messages List and Chat Contents */

.chat-messages {
  padding: 15px;
  margin-bottom: 5px;
}

.chat-messages-with-sidebar {
  margin-left: 70px;
}

.chat-messages ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

.chat-messages li {
  margin-bottom: 10px;
}

.chat-messages li.left .chat-body {
  position: relative;
  margin-left: 70px;
  padding: 10px;
  background: #fff;
  border-radius: 3px;
}

.chat-messages li.left .chat-body :before {
  /*content: "";
	position: absolute;
	top: 20%;
	left: -7px;
	border-style: solid;*/
  border-width: 6px 7px 6px 0;
  border-color: transparent #fff;
  /*display: block;
	width: 0;*/
}

.chat-messages li.right .chat-body {
  position: relative;
  margin-right: 70px;
  padding: 10px 15px;
  background: #fff;
  border-radius: 3px;
}

.chat-messages li.right .chat-body :after {
  content: "";
  position: absolute;
  top: 20%;
  right: -7px;
  border-style: solid;
  border-width: 6px 0 6px 7px;
  border-color: transparent #fff;
  display: block;
  width: 0;
}

.chat-messages .badge {
  font-size: .85em;
  float: right;
  border-radius: 3px;
  background: #c0c2c7;
}

.chat-messages .chat-body .name {
  font-size: 1em;
  font-weight: 700;
}

.chat-messages .user-img {
  margin-top: 8px;
  display: block;
  max-width: 45px;
}

.chat-messages .user-img img {
  display: inline-block;
  max-width: 100%;
  border-radius: 50%;
  box-shadow: 0 0 0 6px #fff;
}

.chat-message-form {
  padding: 15px;
  background: #dddddb;
  display: block
}

.nano {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.nano>.nano-content {
  position: absolute;
  overflow: scroll;
  overflow-x: hidden;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
}

.nano>.nano-content:focus {
  outline: thin dotted;
}

.nano>.nano-content::-webkit-scrollbar {
  visibility: hidden;
}

.has-scrollbar>.nano-content::-webkit-scrollbar {
  visibility: visible;
}

.nano>.nano-pane {
  background: rgba(0, 0, 0, .25);
  position: absolute;
  width: 10px;
  right: 0;
  top: 0;
  bottom: 0;
  visibility: hidden\9;
  /* Target only IE7 and IE8 with this hack */
  opacity: .01;
  -webkit-transition: .2s;
  -moz-transition: .2s;
  -o-transition: .2s;
  transition: .2s;
}

.nano>.nano-pane>.nano-slider {
  background: #fff;
  position: relative;
  margin: 0;
}

.nano:hover>.nano-pane,
.nano-pane.active,
.nano-pane.flashed {
  visibility: visible\9;
  /* Target only IE7 and IE8 with this hack */
  opacity: 0.99;
}

.msg-is-typing-container {
  position: absolute;
  bottom: 0;
}
<div class="chat-container">
  <div class="chat-pusher">
    <div class="chat-content">
      <!-- this is the wrapper for the content -->
      <div class="nano">
        <!-- this is the nanoscroller -->
        <div class="nano-content">
          <div class="">
            <!-- extra div for emulating position:fixed of the menu -->
            <!-- Top Navigation -->
            <div class="">
              <div class="chat-messages chat-messages-with-sidebar">
                <ul id="thread-msg-content" data-msg-thread-id="">


                  <li class="left clearfix">
                    <div class="user-img pull-left">
                      <img src="http://www.simpsonspark.com/images/persos/contributions/bart-simpson-20516.gif" alt="User Avatar" width="50" height="50" />
                    </div>
                    <div class="chat-body clearfix">
                      <div class="header"> <span class="name"><a href="#">Administrator</a></span><span class="name"></span> <span class="badge"><i class="fa fa-clock-o"></i>2018-06-21 04:38:44</span> …
                        <div class="chat-body clearfix">
                          <p>fdfdffdfddfd</p>
                        </div>
                  </li>

                  <li class="left clearfix">
                    <div class="user-img pull-left">
                      <img src="http://www.simpsonspark.com/images/persos/contributions/bart-simpson-20516.gif" alt="User Avatar" width="50" height="50" />
                    </div>
                    <div class="chat-body clearfix">
                      <div class="header"> <span class="name"><a href="#">Administrator</a></span><span class="name"></span> <span class="badge"><i class="fa fa-clock-o"></i>2018-06-21 04:38:44</span> …
                        <div class="chat-body clearfix">
                          <p>fdfdffdfddfd</p>
                        </div>
                  </li>

                  <li class="left clearfix">
                    <div class="user-img pull-left">
                      <img src="http://www.simpsonspark.com/images/persos/contributions/bart-simpson-20516.gif" alt="User Avatar" width="50" height="50" />
                    </div>
                    <div class="chat-body clearfix">
                      <div class="header"> <span class="name"><a href="#">Administrator</a></span><span class="name"></span> <span class="badge"><i class="fa fa-clock-o"></i>2018-06-21 04:38:44</span> …
                        <div class="chat-body clearfix">
                          <p>fdfdffdfddfd</p>
                        </div>
                  </li>

                </ul>

                <div class="msg-is-typing-container">
                  <p class="msg-is-typing">Administrator is typing ...</p>
                </div>

                </div>
                </div>
                </div>
                <!-- /chat-content-inner -->
                </div>
                </div>
                <!-- /nano -->
                </div>
                <!-- /chat-content -->
              </div>
              <!-- /chat-pusher -->
            </div>

我想要的结果:https://www.awesomescreenshot.com/image/3438835/f249eeacac535cd7715d72d9c3b1dcb4

我想在容器“ .chat-c​​ontent”的底部显示div“ .msg-is-typing-container”,但是在显示滚动条时显示div“ .msg-is-typing-container”不会显示在容器底部。

如何解决此问题?

谢谢!

2 个答案:

答案 0 :(得分:1)

.chat-messages {
  position: relative;
  margin: 50px;
}
.msg-is-typing-container{
  position:absolute;
  bottom:-25px;
}

enter image description here

答案 1 :(得分:1)

我更改了.msg-is-typing-container的CSS,以实现我认为您想要的定位。

.msg-is-typing-container{
    position: relative;
    border: 1px solid red;
    display: inline-block;
    padding: 5px 10px;
}

然后我向该元素添加了一个匹配的ID,如下所示:<p class="msg-is-typing" id="msg-is-typing"></p>

下面的jQuery&JavaScript隐藏了键入容器.msg-is-typing-container,直到.nano-content容器滚动到底部,这时该容器才淡入视图。我还添加了JavaScript输入效果found here at w3Schools。您可以在自己理解的JSFiddle处看到这个问题。

$('.msg-is-typing-container').hide();

$(document).ready(function() {
  var i = 0;
  var txt = 'Administrator is typing ...And Only Shows when div is scolled to bottom';
  var speed = 100;                

  function typeWriter() {
    if (i < txt.length ) {
      document.getElementById("msg-is-typing").innerHTML += txt.charAt(i);
      i++;
      setTimeout(typeWriter, speed);
    }
  }    


  $(".nano-content").scroll(function(){   
    if ($(this).scrollTop()>0) {
      $('.msg-is-typing-container').fadeIn(2000);

    }
    else{
      $('.msg-is-typing-container').fadeOut(1000);
    }
  })

  typeWriter();
})