首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >javascript chatbox / messenger脚本

javascript chatbox / messenger脚本
EN

Stack Overflow用户
提问于 2013-02-16 02:03:31
回答 2查看 7.6K关注 0票数 1

我正在尝试写一个像chatbox这样的facebook,但我遇到了一个小问题。我使用了以下代码(这只是测试代码,所以它并不是很干净):

css代码:

代码语言:javascript
复制
#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;
}

代码语言: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代码来:

代码语言: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);
} 
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-02-16 02:22:19

您不能使用元素的样式获得正确的偏移量,除非设置了样式并使其有效。相反,您必须获取element.offsetLeft和窗口区域的大小,并执行以下操作:

代码语言:javascript
复制
new_right = windowSize()[0]-messenger.offsetLeft;

其中窗口大小为this function.

下面是你的函数的我的工作版本:

代码语言:javascript
复制
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)放在代码中。

And here is the link.

票数 0
EN

Stack Overflow用户

发布于 2013-02-16 02:16:50

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

代码语言:javascript
复制
.chatbox {
  float: right;
}

将其添加到您的chatbox样式中。你可能需要花点功夫来确保这个浮动不会影响到你的其他元素。你可能需要一个更好的容器来存放它们。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14900658

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档