如何在jquery中计算屏幕大小

时间:2016-09-26 13:28:09

标签: javascript jquery

如何在jQuery中计算屏幕大小?

我有一个脚本可以创建一个新的聊天窗口,但是当我创建第四个窗口时,它会出现在屏幕外面。我会帮助知道如何计算屏幕的大小而不是超过这个限制!

function createChatBox(chatboxtitle,chatname,minimizeChatBox) {
  $(" <div />" )
    .attr("id","chatbox_"+chatboxtitle)
    .addClass("chatbox")
    .html('<div style="cursor:pointer"onclick="javascript:toggleChatBoxGrowth(\''+chatboxtitle+'\')"><div class="chatboxhead"><div class="chatboxtitle">'+chatname+'</div><div id="chatboxoptions"><a href="javascript:void(0)" onclick="javascript:closeChatBox(\''+chatboxtitle+'\')"> X </a></div><br clear="all"/></div><div class="chatboxcontent"></div><div class="chatboxinput"><textarea class="chatboxtextarea" onkeydown="javascript:return checkChatBoxInputKey(event,this,\''+chatboxtitle+'\',\''+chatname+'\');"></textarea> <br clear="all"/></div>')
    .appendTo($( "body" ));

  $("#chatbox_"+chatboxtitle).css('bottom', '0px');
  chatBoxeslength = 0;

  for (x in chatBoxes) {
    if ($("#chatbox_"+chatBoxes[x]).css('display') != 'none') {
    }
  }

  if (chatBoxeslength == 0) {
    $("#chatbox_"+chatboxtitle).css('right', '215px');
  } else {
    width = (chatBoxeslength)*(225+7)+215;
    $("#chatbox_"+chatboxtitle).css('right', width+'px');
  }
}

3 个答案:

答案 0 :(得分:3)

Jquery width()函数将为您提供窗口宽度。

$(window).width()

答案 1 :(得分:0)

创建一个等于窗口宽度的变量,然后根据需要将其插入到代码中。请参阅下面的示例,根据需要进行编辑。

&#13;
&#13;
$(document).ready(function () {
var windowWidth = $(window).width();
alert(windowWidth);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

document.write( window.outerWidth  );