我正在为我的社交网络聊天。我正在使用以下代码,它似乎在显示聊天时有效,但在隐藏它时却无效。你可以在http://live-pin.com上看到它在右下角(“system
”栏)。
我做错了什么?
function toggleChat(obj){
current_margin = $(obj).css('marginBottom');
if (current_margin == 0){
$(obj).animate({marginBottom : "-270px"}).removeClass("active_box").addClass("hidden_box");
}else{
$(obj).animate({marginBottom : "0"}).removeClass("hidden_box").addClass("active_box");
}
}
此外,我遇到了麻烦,因为它显示/隐藏了两个聊天,我不希望这种情况发生,而且“新消息”表格不起作用,只是在最后创建的聊天中。作为附加细节,这些框是使用从 JSON 检索到的数据动态创建的,因此我不能使用 click() 函数或类似函数。
<?php echo "
谢谢! (: "; ?>
最佳答案
即使您可以将 marginBottom 设置为 0,它在内部也将是 0px,因此 current_margin
将是 0px
而不是 0
。
但您可以先尝试解析该值:
function toggleChat(obj) {
var currentMargin = parseInt($(obj).css('margin-bottom'));
if (currentMargin === 0) {
$(obj).animate({ marginBottom: "-270px" })
.removeClass("active_box")
.addClass("hidden_box");
} else {
$(obj).animate({marginBottom : "0px"})
.removeClass("hidden_box")
.addClass("active_box");
}
}
关于javascript - 如果 css() == X,否则,不工作,表单不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9251417/