【问题标题】:How to make my html DIVS push down rather than Up如何让我的 html DIVS 下推而不是上推
【发布时间】:2014-09-04 19:13:39
【问题描述】:

我正在制作一个可以最小化特定聊天的聊天脚本,但我似乎无法弄清楚如何使 div 的标题下推!有没有办法做到这一点??我对此进行了研究,但无法找到获得所需结果的方法。

我的HTML代码如下

<?php

$usrid = "Joel";

?><!DOCTYPE html>
<HTML>
 <HEAD>
  <LINK REL="stylesheet" HREF="css/style.css">
  <SCRIPT SRC="js/jquery-2.1.1.min.js"></SCRIPT>
  <SCRIPT>
 function onTestChange() {
     var key = window.event.keyCode;

    // If the user has pressed enter
    if (key == 13) {
        event.preventDefault();
        post();
     }
     else {
         return true;
     }
 }

function post()
{
  document.getElementById('txtArea').value = "";
 }
   </SCRIPT>
  </HEAD>
  <BODY><?php

$file = simplexml_load_file("xml/joel.xml");

 echo"
   <DIV CLASS=\"chatbox\">
   <DIV CLASS=\"title\">
    <SPAN CLASS=\"name\">
     $file->subject
    </SPAN>
    <SPAN CLASS=\"buttons\">
      <BUTTON ONCLICK=\"minimize()\" NAME=\"Minimize\" ID=\"min\">-</BUTTON><BUTTON     ONCLICK=\"close()\" NAME=\"Close\">x</BUTTON>
    </SPAN>
   </DIV>
   <DIV ID=\"body\" CLASS=\"hidden\">
   <DIV CLASS=\"history\">";

foreach($file->post as $post)
{
 if($post->auth == $usrid)
 {
  echo"
    <DIV CLASS=\"self\">
     <DIV CLASS=\"self_left\">
      $post->content
     </DIV>
     <DIV CLASS=\"self_right\">
      $post->auth
     </DIV>
    </DIV>      
     ";
  }
 else
 {
  echo"
    <DIV CLASS=\"other\">
     <DIV CLASS=\"other_left\">
      $post->auth
     </DIV>
     <DIV CLASS=\"other_right\">
      $post->content
     </DIV>
    </DIV>      
     ";
 }
}

echo"   </DIV>
   <DIV CLASS=\"input\">
    <DIV ID=\"input\">
     <DIV CLASS=\"text\">
      <TEXTAREA NAME=\"input\" ONKEYPRESS=\"onTestChange()\"  ID=\"txtArea\"></TEXTAREA>
     </DIV>
     <DIV CLASS=\"submit\">
      <BUTTON SRC=\"images/button1.png\" ONCLICK=\"post()\">Send</BUTTON>
     </DIV>
    </DIV>
   </DIV>
   </DIV>
  </DIV>
 </BODY>
</HTML>";

?>

我的 CSS 是 ::-webkit-滚动条 { 宽度:8px; 高度:8px; }

::-webkit-scrollbar-track
{
 background: url(../images/bg.png);
}

::-webkit-scrollbar-thumb
{
 background: rgba(0,0,0,0.5);
 border-radius: 25px;
}

.chatbox
{
 width: 250px;
 margin: 0 auto;
 border: 1px solid black;
 border-top-left-radius: 25px;
 border-top-right-radius: 25px;
 overflow: hidden;
 flex-direction: column;
}

.title
{
 width: 100%;
 border-bottom: 1px solid black;
 display: inline-block;
 background: #436ED2;
 flex-direction: column;
}

.name
{
 width: calc(50% - 10px);
 float: left;
 text-align: left;
 margin-top: 3px;
 padding-left: 10px;
}

.buttons
{
 width: 50%;
 float: right;
 text-align: right;
}

.buttons button
{
 width: 24px;
 height: 24px;
 border: none;
 background: none;
 outline: none;
}

.history
{
 width: 90%;
 margin: 0 auto;
 height: 300px;
 border: 1px solid black;
 margin-top: 5px;
 overflow: auto;
 overflow-x: hidden;
}

.self
{
 width: 100%;
 background: #c6d3f1;
 display: inline-block;
 margin-top: -4px;
 padding-bottom: 5px;
 padding-top: 5px;
}

.self_left
{
 width: calc(80% - 6px);
 float: left;
 text-align: right;
 padding-right: 5px;
 border-right: 1px solid black;
}

.self_right
{
 width: calc(20% - 6px);
 float: right;
 text-align: left;
 padding-left: 5px;
 border-left: 1px solid black;
}


.other
{
 width: 100%;
 background: #f1d3c6;
 display: inline-block;
 margin-top: -4px;
 padding-bottom: 5px;
 padding-top: 5px;
}

.other_left
{
 width: calc(20% - 6px);
 float: left;
 text-align: right;
 padding-right: 5px;
 border-right: 1px solid black;
}

.other_right
{
 width: calc(80% - 6px);
 float: right;
 text-align: left;
 padding-left: 5px;
 border-left: 1px solid black;
}

.input
{
 width: 90%;
 margin: 0 auto;
 height: 90%;
 margin-top: 5px;
}

#input
{
 width: 100%;
 display: inline-block;
}

textarea
{
 resize: none;
 width: 75%;
 height: 40px;
 float: left;
}

.input button
{
 float: right;
 width: 20%;
 height: 46px;
 top: 10px;
}

.hidden
{
 display: none;
 flex-direction: column;
}

当我调用我尚未添加的脚本时,我希望名为 title 的 div 向下移动。

【问题讨论】:

  • 请告诉我们你目前拥有的东西
  • 请发布代码或网站。

标签: html css chat


【解决方案1】:

您可以使用弹性盒子来实现这一点

看看这篇文章:http://css-tricks.com/snippets/css/a-guide-to-flexbox/

这将使 div 向上而不是向下:

.container {
  flex-direction: column-reverse;
}

【讨论】:

  • 这是一个很棒的现代解决方案,如果考虑到浏览器兼容性,这不是为提问者提供的解决方案。
  • 哈哈@MikeLegacy 一开始的问题是“如何让我的 html DIVS 下推而不是上推”。然后他更新了这个问题,它换了一个不同的东西哈哈
【解决方案2】:

我在改变我所问的问题后发现的最简单的方法得到了正确的结果,非常简单

.chatbox
{
 position: fixed;
 bottom: 0px;
}

就是这样

【讨论】:

  • 哈哈我在更新前给出了答案。随着更新更清楚你想要实现的目标,没有更新我认为你想改变元素的堆栈顺序:P
  • 这是一个有用的想法,但不是在这种情况下,但我很惊讶我的答案如此简单
  • 是的,我误解了你的问题。我以为你想改变 DIVS 相互堆叠的方向。如果你的答案更清楚,我会说position: fixed;真的很快哈哈
  • 对我的回答不完全满意(它不允许浮动属性)将不得不为此找到解决方案
  • :O 我会尽力帮助你的
【解决方案3】:

您可以使用 jQuery 的 .animate() 方法将每个聊天 div 缩放到“最小化”/“最大化”状态。

这是一个不完美的例子,我只是把它作为一个起点:

jsFiddle Demo

HTML:

<div class="chatBox" id="chat-17">
    <div class="chatTitle">
        <div class="titleText">This is chat title</div>
        <div class="titleMin"> v</div>
    </div>
    <div class="chatBody">
        Chat message goes here
    </div>
</div>

jQuery:

$('.titleText').click(function(){
    if ( $(this).hasClass('minTitle') ){
        $(this).removeClass('minTitle');
        $(this).parent().parent().removeClass('minimized');
    }else{
        $(this).addClass('minTitle');
        var cb = $(this).parent().parent();
        cb.addClass('minimized');
        //cb.animate(function(){top: '150',left: '10'},500);
    }
});

css:

.chatBox{width:100%;border:1px solid blue;overflow:hidden;}
.chatTitle{width:100%;height:20px;}
.titleText{width:95%;height:100%;color:white;font-weight:bold;float:left;background:black;}
.titleMin{width:4.5%;height:100%;color:red;font-weight:bold;float:left;text-align:center;background:black;}
.titleMin:hover{cursor:pointer;cursor:hand;}
.chatBody{width:100%;height:150px;padding:10px;background:wheat;}

添加了额外的 css,因为示例不起作用:

.titleText:hover{cursor:pointer;cursor:hand;}
.minimized{height:5px;width:5px;position:absolute;top:150px;}
span{font-weight:bold;color:red;}

资源:

http://api.jquery.com/animate/

http://viralpatel.net/blogs/understanding-jquery-animate-function/

http://www.tutorialscollection.com/jquery-animate-how-to-animate-in-jquery-using-animate-method/

http://www.1stwebdesigner.com/tutorials/jquery-animation-tutorial/

【讨论】:

    猜你喜欢
    • 2021-06-11
    • 1970-01-01
    • 2013-07-02
    • 2017-05-30
    • 2023-03-17
    • 2021-11-28
    • 2017-10-25
    • 2014-02-11
    • 1970-01-01
    相关资源
    最近更新 更多