简体   繁体   English

JavaScript 计算视口宽度/高度

[英]JavaScript calculate with viewport width/height

I am trying to set a responsive point in my mobile Webview and did this:我正在尝试在我的移动 Webview 中设置一个响应点并这样做:

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

This works great so far.到目前为止,这很好用。 But the values -40 and -100 are not in the viewport scaling height and width.但值 -40 和 -100 不在视口缩放高度和宽度中。

When I do this:当我这样做时:

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

as it should be to stay responsive and relative to the viewport - the JS does not work anymore.因为它应该保持响应并相对于视口 - JS 不再工作了。 I think vh and vw works only in CSS ?我认为 vh 和 vw 仅在 CSS 中有效? How can I achieve this in JS ?我怎样才能在 JS 中实现这一点?

Pleas no JQuery solutions - only JS!请不要使用 JQuery 解决方案 - 只有 JS!

Thanks谢谢

Based on this site you can write following function in javascript to calculate your desired values:基于此站点,您可以在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));

I used this incredible question in my codes!我在我的代码中使用了这个不可思议的问题!

Try this:尝试这个:

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];
}

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

Problem is that JS does not have 40vh , calculate how much pixels is 40vh first to use it.问题是,JS没有40vh ,算算多少像素是如何40vh首先使用它。 It will throw error when doing 1000 - 40vh执行1000 - 40vh时会抛出错误

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

Also there is no such thing as wh , only vh (% of viewport height)也没有wh这样的东西,只有vh (视口高度的百分比)

The simplest way to do this, if you can fully edit the page, is to make a css class that has -40vw and -100vh like so:如果您可以完全编辑页面,最简单的方法是创建一个具有 -40vw 和 -100vh 的 css 类,如下所示:

CSS: CSS:

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

JS: JS:

element.classList.add("class");

Note: "classList" is not supported in Internet Explorer 9. If you want it to work in all browsers, use this for JS instead:注意:Internet Explorer 9 不支持“classList”。如果您希望它适用于所有浏览器,请改用 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;
    }
}

you just need to surround it in quotes I think.你只需要用我认为的引号将它括起来。 var w = window.innerWidth = "40vw" var w = window.innerWidth = "40vw"

this is my solve with you can use CSS;这是我的解决方案,您可以使用 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`);

How to use in CSS ?如何在 CSS 中使用?

If you will use 100vh or 100vw with this method, you should set 100vh/100vw for uncompatible browser.如果您将通过此方法使用100vh100vw ,则应为不兼容的浏览器设置 100vh/100vw。

Examples;例子;

.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.. */

This isn't a universal solution, but it's a much simpler implementation if you're working with a page that is always 100% displayed within the viewport (ie, if the body doesn't have to be scrolled and always matches the window width and height).这不是一个通用的解决方案,但如果您使用的页面始终 100% 显示在视口中(即,如果正文不必滚动并且始终与窗口宽度匹配,则它是一个更简单的实现和高度)。

let vh = document.body.getBoundingClientRect().height;

This sets the vh variable to the pixel value of the document body with just one line of code.这只需一行代码即可将 vh 变量设置为文档正文的像素值。

Useful for game dev and other scenarios where you have the body affixed to the viewport.对于游戏开发和其他将主体附加到视口的场景很有用。

get vmin in px以 px 为单位获取vmin

function vmin(){
    return window.innerHeight < window.innerWidth ? window.innerHeight: window.innerWidth;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用JavaScript使div的宽度和高度等于视口 - Make divs width and height equal to viewport with JavaScript JavaScript计算固定div中矩形的高度和宽度 - JavaScript calculate height & width of rectangles in fixed div 使用javascript计算给定高度的字体宽度 - Calculate font width for a given height using javascript 计算所选文本的宽度和高度(javascript) - Calculate width & height of the selected text (javascript) 如何根据视口的宽度/高度计算将背景图像移位多少 - How to calculate how much to shift a background image based on viewport width / height 在JavaScript中以编程方式计算给定的面积和比例的宽度和高度 - Programmatically calculate width and height given area and ratio in JavaScript 根据x和y坐标以及javascript中的宽度和高度值计算面积 - calculate an area based on x and y coordinates and width and height values in javascript 调整图片大小可让用户设置宽度或高度,javascript计算并填充宽度或高度 - Resize image have user set width or height javascript calculate and fill width or height 如何在JavaScript中获取浏览器视口的宽度和高度并将其应用于CSS ID? - How Do I Get Browser Viewport Width & Height In Javascript And Apply To CSS ID's? 如何获取浏览器视口的小数宽度和高度? - How to get the fractional width and height of the browser viewport?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM