简体   繁体   English

如何检测浏览器是否支持视口单位,如 vh 或 dvh?

[英]How can I detect if a browser supports a viewport unit, like vh or dvh?

I'm planning to make very responsive website.我打算制作非常敏感的网站。 For this, I'm including external stylesheet currently.为此,我目前包括外部样式表。

But I want to include it for only those browsers which supports viewport units such as vw.但我只想将它包含在那些支持视口单位的浏览器中,例如 vw。

So, how could I detect for browser that it supports or not.那么,我如何检测它支持或不支持的浏览器。

Please note: I don't want to include modernizer.请注意:我不想包括 modernizer。

This is the code that Modernizr uses: 这是Modernizr使用的代码:

  testStyles('#modernizr { width: 50vw; }', function(elem) {
    var width = parseInt(window.innerWidth / 2, 10);
    var compStyle = parseInt((window.getComputedStyle ?
                              getComputedStyle(elem, null) :
                              elem.currentStyle).width, 10);

    Modernizr.addTest('cssvwunit', compStyle == width);
  });

So you can do something similar. 因此,您可以执行类似的操作。 Just follow these steps: 只需按照以下步骤操作:

  1. Set an element with width in vw . 设置widthvw的元素。
  2. Check the computed width of the element to match the viewport's width. 检查元素的计算width以匹配视口的宽度。
  3. If both are same, then your browser supports vw and vh ! 如果两者相同,则您的浏览器支持vwvh

Snippet coming soon... 片段即将推出...

 $(function () { elemWidth = parseInt(getComputedStyle(document.querySelector("#checkVw"), null).width, 10); halfWidth = parseInt(window.innerWidth / 2, 10); $("#checkVw").html("Your browser" + ((elemWidth == halfWidth) ? "" : "does not ") + " support VW and VH"); }); 
 #checkVw { width: 50vw; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div id="checkVw"></div> 

I checked with IE 7 and it screwed up! 我检查了IE 7并搞砸了! ;)

In modern browsers you can use @supports in CSS to check support for various things, including viewport units.在现代浏览器中,您可以在 CSS 中使用@supports来检查对各种事物的支持,包括视口单位。

Wrap any styles you wish in @supports much like @media queries:将您希望的任何 styles 包装在@supports中,就像@media查询一样:

@supports(height: 100vh) {
 .myClass { height: 100vh; }
}

This example tests to see if a browser supports units like vh or dvh or a non-existing type of madeup (ie a type that your browser does not know about):此示例测试浏览器是否支持vhdvh等单位或不存在的madeup类型(即您的浏览器不知道的类型):

 #supportVH::after, #supportDVH:after, #supportMadeUp:after { content: "❌ NO"; color: white; background-color: darkred; display: block; margin: 1em 0; padding: 1em; border: 1px solid #ccc; } @supports(height: 100vh) { #supportVH::after { content: "✅ YES"; background-color: lightgreen; } } @supports(height: 100dvh) { #supportDVH:after { content: "✅ YES"; background-color: lightgreen; } } @supports(height: 100madeup) { #supportMadeUp:after { content: "✅ YES"; background-color: lightgreen; } }
 <div id="supportVH">Does your browser support <code>vh</code>?</div> <div id="supportDVH">Does your browser support <code>dvh</code>?</div> <div id="supportMadeUp">Does your browser support <code>madeup</code>?</div>

In JavaScript you can use it the same way with CSS.supports() :在 JavaScript 中,您可以像CSS.supports()一样使用它:

const supportsDvh = CSS.supports('height: 100dvh');

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM