我有一堆用戶可以創建和表示消息框的div。當用戶點擊其朋友的姓名時,會創建一個框。我已經能夠做到這一點,所以當創建一個新的盒子時,它被定位在正確的距離上,但是我一直無法弄清楚如何製作盒子,所以當一個盒子被移除時,其他盒子被移動填補空白,爲新盒子騰出空間。我知道我需要使用foreach,但我不知道如何引用框。預先感謝您的幫助。當一個被刪除時重新安排框
#chatbox{
border:2px solid #0969A2;
height:250px;
width:200px;
background-color:#fff;
position:fixed;
z-index:11002;
padding:3px;
}
$('#open-chat').live('click', function() {
var user=$(this).attr('data-name');
//check if bar exists
if ($("#bar-icon[data-name="+user+"]").length == 0){
$('.dockleft-block').append('<div id="bar-icon" data-name="'+user+'">'+user+'</div>');
$('body').append('<div id="chatbox" data-name="'+user+'"></div>');
//position new boxes
$("#chatbox[data-name="+user+"]").css('bottom', '37px');
var chatBoxeslength = $("div[id=chatbox]").length-1;
if (chatBoxeslength == 0) {
$("#chatbox[data-name="+user+"]").css('left', '35px');
} else {
width = (chatBoxeslength)*(225+7)+20;
$("#chatbox[data-name="+user+"]").css('left', width+'px');
$("#bar-icon[data-name="+user+"]").css('left', width-35+'px');
}
}
//end if length
});
//end click function
$('.closechatbox').live('click', function() {
user=$(this).attr('data-name');
$("#chatbox[data-name="+user+"]").remove();
$("#bar-icon[data-name="+user+"]").remove();
//need to rearrange other boxes
});
UPDATE
這是另一個聊天腳本的解決方案,但我不明白它 anantgarg.com/chat/sampleb.php 他們如何引用每箱爲#chatbox _「+成功chatboxtitle?
function restructureChatBoxes() {
align = 0;
for (x in chatBoxes) {
chatboxtitle = chatBoxes[x];
if ($("#chatbox_"+chatboxtitle).css('display') != 'none') {
if (align == 0) {
$("#chatbox_"+chatboxtitle).css('right', '20px');
} else {
width = (align)*(225+7)+20;
$("#chatbox_"+chatboxtitle).css('right', width+'px');
}
align++;
}
}
}
雖然你的答案不完全是我所需要的,它是接近,並讓我正在尋找數組等等。感謝兄弟,感謝你的時間。 – Scarface 2010-11-13 23:17:10