JavaScript使用视口宽度/高度计算

时间:2017-05-22 09:25:14

标签: javascript css viewport

我正在尝试在移动Webview中设置响应点并执行此操作:

var w = window.innerWidth-40;
var h = window.innerHeight-100;

到目前为止这种方法很有效。但值-40和-100不在视口缩放高度和宽度中。

当我这样做时:

var w = window.innerWidth-40vw;
var h = window.innerHeight-100vh;

应该是保持响应和相对于视口 - JS不再起作用了。 我认为vh和vw只适用于CSS? 我怎样才能在JS中实现这个目标?

请求没有JQuery解决方案 - 只有JS!

由于

6 个答案:

答案 0 :(得分:13)

基于this site,您可以在javascript中编写以下函数来计算您想要的值:



function vh(v) {
  var h = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
  return (v * h) / 100;
}

function vw(v) {
  var w = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
  return (v * w) / 100;
}

function vmin(v) {
  return Math.min(vh(v), vw(v));
}

function vmax(v) {
  return Math.max(vh(v), vw(v));
}
console.info(vh(20), Math.max(document.documentElement.clientHeight, window.innerHeight || 0));
console.info(vw(30), Math.max(document.documentElement.clientWidth, window.innerWidth || 0));
console.info(vmin(20));
console.info(vmax(20));




我在代码中使用了this难以置信的问题!

答案 1 :(得分:2)

问题是JS没有40vh,先计算40vh首先使用它的像素数。在执行1000 - 40vh

时会抛出错误

40vh表示40 % of viewport height。所以window.innerHeight * 0.4 == 40vh

此外不存在wh,只有vh(视口高度的百分比)

答案 2 :(得分:2)

试试这个:

function getViewport() {

 var viewPortWidth;
 var viewPortHeight;

 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
 if (typeof window.innerWidth != 'undefined') {
   viewPortWidth = window.innerWidth,
   viewPortHeight = window.innerHeight
 }

// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
 else if (typeof document.documentElement != 'undefined'
 && typeof document.documentElement.clientWidth !=
 'undefined' && document.documentElement.clientWidth != 0) {
    viewPortWidth = document.documentElement.clientWidth,
    viewPortHeight = document.documentElement.clientHeight
 }

 // older versions of IE
 else {
   viewPortWidth = document.getElementsByTagName('body')[0].clientWidth,
   viewPortHeight = document.getElementsByTagName('body')[0].clientHeight
 }
 return [viewPortWidth, viewPortHeight];
}

参考:http://andylangton.co.uk/articles/javascript/get-viewport-size-javascript/

答案 3 :(得分:0)

如果您可以完全编辑页面,最简单的方法是创建一个具有-40vw和-100vh的css类:

CSS:

.class{
    width: -40vw;
    height: -100vh;
}

JS:

element.classList.add("class");

注意:" classList" Internet Explorer 9不支持。如果您希望它在所有浏览器中都可以使用,请将其用于JS:

function myFunction() {
    var element, name, arr;
    element = document.getElementById("myDIV");
    name = "mystyle";
    arr = element.className.split(" ");
    if (arr.indexOf(name) == -1) {
        element.className += " " + name;
    }
}

答案 4 :(得分:0)

您只需要将其括在我认为的引号中即可。 var w = window.innerWidth = "40vw" var w = window.innerWidth = "40vw"

答案 5 :(得分:0)

这是我可以使用CSS的解决方法;

    // calc dynamic customer device height/width
    let vh = window.innerHeight * 0.01,
        vw = window.innerWidth * 0.01;
    document.documentElement.style.setProperty('--vh', `${vh}px`);
    document.documentElement.style.setProperty('--vw', `${vw}px`);

如何在CSS中使用?

如果通过此方法使用 100vh 100vw ,则应为不兼容的浏览器设置100vh / 100vw。

示例;

.wrapper{
    height: 100vh; /* Fallback for browsers that do not support Custom Properties */
    height: calc(var(--vh, 1vh) * 100);
}

.slide-container{
    height: calc(var(--vh, 1vh) * 100 - var(--menuHeight) - var(--footerHeight));
}

.little-image{
    width: calc(var(--vw, 1vw) * 5);
    margin-bottom: calc(var(--vh, 1vh) * 1);
}

/* and more.. */
相关问题