我怎样才能得到windowWidth
,windowHeight
,pageWidth
,pageHeight
,screenWidth
,screenHeight
,pageX
,pageY
,screenX
,screenY
将在所有主要浏览器工作?
获取屏幕,当前网页和浏览器窗口的大小
With the introduction of globalThis
in ES2020 you can use properties like.
For screen size:
globalThis.screen.availWidth
globalThis.screen.availHeight
For Window Size
globalThis.outerWidth
globalThis.outerHeight
For Offset:
globalThis.pageXOffset
globalThis.pageYOffset
...& so on.
alert("Screen Width: "+ globalThis.screen.availWidth +"\nScreen Height: "+ globalThis.screen.availHeight)
here is my solution!
// innerWidth
const screen_viewport_inner = () => {
let w = window,
i = `inner`;
if (!(`innerWidth` in window)) {
i = `client`;
w = document.documentElement || document.body;
}
return {
width: w[`${i}Width`],
height: w[`${i}Height`]
}
};
// outerWidth
const screen_viewport_outer = () => {
let w = window,
o = `outer`;
if (!(`outerWidth` in window)) {
o = `client`;
w = document.documentElement || document.body;
}
return {
width: w[`${o}Width`],
height: w[`${o}Height`]
}
};
// style
const console_color = `
color: rgba(0,255,0,0.7);
font-size: 1.5rem;
border: 1px solid red;
`;
// testing
const test = () => {
let i_obj = screen_viewport_inner();
console.log(`%c screen_viewport_inner = \n`, console_color, JSON.stringify(i_obj, null, 4));
let o_obj = screen_viewport_outer();
console.log(`%c screen_viewport_outer = \n`, console_color, JSON.stringify(o_obj, null, 4));
};
// IIFE
(() => {
test();
})();
I developed a library for knowing the real viewport size for desktops and mobiles browsers, because viewport sizes are inconsistents across devices and cannot rely on all the answers of that post (according to all the research I made about this) : https://github.com/pyrsmk/W
但是,当我们谈论响应式屏幕时,如果出于某种原因要使用jQuery处理它,
window.innerWidth, window.innerHeight
给出正确的测量。即使它消除了滚动条的多余空间,我们也不必担心调整该空间:)
这是使用纯JavaScript的跨浏览器解决方案(源):
var width = window.innerWidth
|| document.documentElement.clientWidth
|| document.body.clientWidth;
var height = window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight;
一种获取可用屏幕尺寸的非jQuery方法。window.screen.width/height
已经提出,但是出于响应性网页设计和完整性的考虑,我认为值得提及以下属性:
alert(window.screen.availWidth);
alert(window.screen.availHeight);
http://www.quirksmode.org/dom/w3c_cssom.html#t10:
availWidth和availHeight-屏幕上的可用宽度和高度(不包括OS任务栏等)。
您可以使用jQuery获取窗口或文档的大小:
// Size of browser viewport.
$(window).height();
$(window).width();
// Size of HTML document (same as pageHeight/pageWidth in screenshot).
$(document).height();
$(document).width();
对于屏幕大小,您可以使用screen
对象:
window.screen.height;
window.screen.width;
This how I managed to get the screen width in React JS Project:
If width is equal to 1680 then return 570 else return 200
Screen.availWidth