2016-09-26 48 views
-2

如何计算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'); 
    } 
} 
+0

可以包括HTML,所以我们知道你正在围绕 – depperm

+1

你的意思是浏览器窗口?在jQuery中'$(window).width()'应该是你所需要的。 (对于原生js只是使用'screen'对象) – Carl

+0

你需要的是视口大小,屏幕大小不会帮助 –

回答

3

jquery width()函数会给你窗口的宽度。

$(window).width() 
0

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

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

0

document.write(window.outerWidth );

+1

请随时向你的代码添加一些解释... – andreas