下面的代码获取监视器、窗口大小和位置的一些属性。我的问题是,window.screenX被错误的监视器计算出来了。
因为我有三个显示器,如果我把窗口放在左边监视器的左边,它会在-192 to左右返回,因为它使用中间监视器作为中心点,我希望它返回零。是否有任何方法根据窗口所在的当前监视器返回windows X位置?
let windowWidth
let windowHeight
let screenX
let screenY
let monitorWidth
let monitorHeight
window.addEventListener('resize', function() {
onResize()
});
setInterval(() => {
onResize()
}, 1000)
function onResize() {
windowWidth = window.innerWidth
windowHeight = window.innerHeight
screenX = window.screenX
screenY = window.screenY
monitorWidth = window.screen.availWidth
monitorHeight = window.screen.availHeight
document.getElementById("windowWidth").innerHTML = 'windowWidth : ' + windowWidth;
document.getElementById("windowHeight").innerHTML = 'windowHeight : ' + windowHeight;
document.getElementById("screenX").innerHTML = 'screenX : ' + screenX;
document.getElementById("screenY").innerHTML = 'screenY : ' + screenY;
document.getElementById("monitorWidth").innerHTML = 'monitorWidth : ' + monitorWidth;
document.getElementById("monitorHeight").innerHTML = 'monitorHeight : ' + monitorHeight;
}<div id="windowWidth"></div>
<div id="windowHeight"></div>
<div id="screenX"></div>
<div id="screenY"></div>
<div id="monitorWidth"></div>
<div id="monitorHeight"></div>
发布于 2020-09-05 18:57:29
使用CSS进行视口调整--不需要javascript。
示例:
/* IF VIEWPORT IS SMALLER THAN 510 PX */
@media only screen and (max-width : 510px) {
div.this_wrapper{
max-width: 320px;
}
}https://stackoverflow.com/questions/63757370
复制相似问题