首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >正在使用所有监视器计算window.screenX,如何获得当前监视器中窗口的x位置?

问正在使用所有监视器计算window.screenX,如何获得当前监视器中窗口的x位置?
EN

Stack Overflow用户
提问于 2020-09-05 18:42:02
回答 1查看 98关注 0票数 4

下面的代码获取监视器、窗口大小和位置的一些属性。我的问题是,window.screenX被错误的监视器计算出来了。

因为我有三个显示器,如果我把窗口放在左边监视器的左边,它会在-192 to左右返回,因为它使用中间监视器作为中心点,我希望它返回零。是否有任何方法根据窗口所在的当前监视器返回windows X位置?

​

代码语言:javascript
复制
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;
}
代码语言:javascript
复制
<div id="windowWidth"></div>
<div id="windowHeight"></div>
<div id="screenX"></div>
<div id="screenY"></div>
<div id="monitorWidth"></div>
<div id="monitorHeight"></div>

​

EN

回答 1

Stack Overflow用户

发布于 2020-09-05 18:57:29

使用CSS进行视口调整--不需要javascript。

示例:

代码语言:javascript
复制
/* IF VIEWPORT IS SMALLER THAN 510 PX */
@media only screen and (max-width : 510px) {
div.this_wrapper{
max-width: 320px;
}
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63757370

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档