简体   繁体   English

在不支持vw或vh属性的旧版移动浏览器中会发生什么?

[英]What happens in old mobile browsers which don't support vw or vh properties?

I am using the CSS3 properties such as VW VH. 我正在使用CSS 3属性,例如VW VH。 i need to know the impact of old mobile browser which doesn't support the CSS3. 我需要了解不支持CSS3的旧移动浏览器的影响。 i am using to set the height of the div using the VH. 我正在使用VH设置div的高度。

.block{
   height : 50vh;
   width  : 50vw;
}
<div class="block">
      //do some thing....
</div>

I know your pain. 我知道你的痛苦

On mobile browsers that don't support vw or vh , the properties are ignored and default to inherit on block elements' width and collapse on height , and on inline-block elements both width and height are set to collapse . 在不支持vwvh移动浏览器中,这些属性将被忽略,默认情况下继承于块元素的width并在height折叠 ,而在内联块元素上widthheight均设置为crash

With vw units, for compatibility, you can easily switch them out for % values, but percentage height units never work as expected. 使用vw单位,出于兼容性考虑,您可以轻松地将它们切换为%值,但是百分比高度单位永远无法按预期工作。

In PhoneGap, Cordova, and even my website which targets Android users, I use a "polyfill" (code that makes the unsupported styling "work") along with my vw and vh units to support Android 4.3 Jellybean and below: 在PhoneGap,Cordova,甚至是针对Android用户的网站中,我都使用“ polyfill”(使不受支持的样式“工作”的代码)以及vwvh单元支持Android 4.3 Jellybean及以下版本:

function orient() {
    var ua = navigator.userAgent;
    if(ua.indexOf('droid 4.3') == -1 && ua.indexOf('droid 4.2') == -1 && ua.indexOf('droid 4.1') == -1 && ua.indexOf('droid 4.0') == -1 && ua.indexOf('droid 2.') == -1) {

      //This version of Android supports vw and vh units!

    } else {

      //Polyfill!

      var w = window.innerWidth, h = window.innerHeight;

      //Examples:

      elem.style.width = (w*.94)+'px'; //width: 94vw
      elem.style.height = (h*.94)+'px'; //height: 94vh
      elem2.style.height = (w*.5)+'px'; //height: 50vw (Opposite viewport unit! % doesn't support that!)

    }
}
window.onresize = function() { window.setTimeout(orient,50); }
window.onresize();

I like Javascript because it doesn't clutter my CSS code, and doesn't execute if the browser is compatible. 我喜欢Javascript,因为它不会使我的CSS代码混乱,并且如果浏览器兼容也不执行。 Better than percentage units, right? 比百分比单位好吧?

Hope this (or a version of this) helps you on your project! 希望这个(或一个版本)对您的项目有所帮助!

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

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