1

我正在尝试编写一个类似聊天框的 facebook,但我遇到了一个小问题。我正在使用以下代码(它只是测试代码,所以它不是很干净):

CSS代码:

#messenger {
  position: fixed;
  bottom: 0px;
  right: 10px;
  width: 200px;
  height: 300px;
  z-index: 4;
  background-color: #ECECEC;
  border: 1px solid #000;
}
#messenger.p {
  text-align: right;
}
#contacts {
  margin: 5px 5px 5px 5px;
}
#chatspace {
  position: fixed;
  bottom: 0px;
  right: 240px;
  height: 20px;
  left: 20px;
  background-color: #ECECEC;
  border: 1px solid #000;
  z-index: 4;
}
.chatbox {
  position: absolute;
  bottom: 0px;
  width: 200px;
  height: 200px;
  z-index: 4;
  background-color: #ECECEC;
  border: 1px solid #000;
}

html/javascript 代码:

<script type="text/javascript">
var i = 0;

function oc_chatbox() {

  if (i == 0) {
  document.getElementById('contacts').style.visibility = 'hidden';
  document.getElementById('messenger').style.height = '20px';
  i = 1;
  }

  else {
  document.getElementById('contacts').style.visibility = 'visible';
  document.getElementById('messenger').style.height = '300px';
  i = 0;
  }
}

function new_chat(userid) {
  var new_right;
  new_right = document.getElementById('messenger').style.right;
  //alert('old value: '+ new_right);
  new_right += 20;
  //alert('New value of right: '+ new_right);
  document.getElementById('chatspace').innerHTML = '<div id="'+userid+'" class="chatbox" style="right: '+new_right+'px;"></div>';
  //document.write('<div id="'+userid+'" class="chatbox" style="right: '+new_right+'px;"></div>');
}
</script>
<div id="chatspace"></div>
<div id="messenger">
 <p><a href="#" onclick="oc_chatbox();">Collapse</a></p>
 <div id="contacts">
 <ul>
  <li><a href="#" onclick="new_chat('contact_a');">contact A</a></li>
 </ul>
 </div>
</div>

问题是,当我尝试向聊天栏添加新聊天时,我似乎无法将它们放在一起。谁能帮忙?

编辑:

所以我将javascript代码更改为:

var last = null;
function new_chat(userid) {
  if(userid==null)
    userid = "user666";
  var new_right;
  var margin = 10;
  var messenger = window.last==null?document.getElementById('messenger'):window.last;  //Take the messenger or the last added chat
  new_right = document.body.clientWidth-messenger.offsetLeft;      //Compute the window size
  console.log(new_right);                                //Log the number
  new_right += margin;                                   //keep spaces between divs

  var newChat = document.createElement("div");           //DOM create DIV
  newChat.id = userid;
  newChat.className = "chatbox shadow";
  newChat.style.right = new_right+"px";
  newChat.innerHTML = '<p>'+userid+'</p><p><textarea></textarea></p>';
  window.last = newChat;  //Remember whichever is last
  document.body.appendChild(newChat);
} 

现在它可以工作了,谢谢!

4

2 回答 2

0

您应该尝试添加浮动样式。

.chatbox {
  float: right;
}

将其添加到您的聊天框样式中。您可能需要稍微弄乱一下,以确保浮动不会与您的其他元素混淆。您可能需要一个更好的容器来存放它们。

如果你想获得真正的乐趣,你可以从 jQuery 中添加 .draggable(),然后你可以让它们捕捉到你的聊天栏。然后,您可以更改聊天的顺序。

于 2013-02-15T18:16:50.297 回答
0

除非样式已设置且有效,否则您无法使用其样式获得元素的右偏移量。相反,您必须获取element.offsetLeft窗口区域的大小并执行以下操作:

new_right = windowSize()[0]-messenger.offsetLeft;

其中窗口大小是这个函数。

这是您的功能的我的工作版本:

var last = null;
function new_chat(userid) {
  if(userid==null)
    userid = "user666";
  var new_right;
  var margin = 20;
  var messenger = window.last==null?document.getElementById('messenger'):window.last;  //Take the messenger or the last added chat
  new_right = windowSize()[0]-messenger.offsetLeft;      //Compute the window size
  console.log(new_right);                                //Log the number
  new_right += margin;                                   //keep spaces between divs

  var newChat = document.createElement("div");           //DOM create DIV
  newChat.id = userid;
  newChat.className = "chatbox";
  newChat.style.right = new_right+"px";
  window.last = newChat;  //Remember whichever is last
  document.body.appendChild(newChat);
} 

如果console未在您的浏览器中定义,您可能会收到错误。但在这种情况下,您应该使用更好的浏览器。通常,将if(console!=null)放在代码中。
这是链接。

于 2013-02-15T18:22:18.727 回答