如何在移动Safari中修复vh(视口单元)css?

Alv*_*aro 17 css height lightbox mobile-safari viewport-units

我在我的一个项目中使用了vh(视口单元)css但在移动safari中它不起作用.似乎Safari不知道如何处理vh,但它在其他浏览器中工作正常.无论有没有,我想做同样的效果,请帮助我.顺便说一句,我正在使用facebox.(身高:50%不能正常工作)

HTML:

 <div id="facebox" style="top: 0px; left: 0px; display: block;"> 
       <div class="popup">    
             <div class="body"> 
                  <div class="content_light">
            <div class="Lcontent_left"></div>
                    <div class="Lcontent_right">
                    <div class="Lright_content"></div>
                    </div>
                  </div>
              </div>
       </div>
    </div>
Run Code Online (Sandbox Code Playgroud)

这是我的css:

#facebox .popup {
    display:block;
    position:relative;
margin:auto;
margin-top:0%;
  min-height:100vh;
  height:1px;
  border-radius:5px;
}

    #facebox .body {
  padding:0px;
  display:block;
  position:relative;
  background: #fff;
  width:100%;
  min-height:100vh;
  height:1px;
  margin:auto;
   border-radius:5px;
}
.Lcontent_left{
    position:relative;
    float:left;
    width:100%;
    height:50vh;
    /*min-height:250px;*/
    text-align:center;
    overflow:hidden;
    }
.Lcontent_left_fullscreen{
    display:none;
    }
.Lcontent_right{
    float:left;
    text-justify:inter-word;
    position:relative;
    width:100%;
    height:50vh;
    background-color:white;
    overflow:auto;
    font-family:"Open Sans",Helvetica,sans-serif;
    }
.Lright_content{
    position:relative;
    width:95%;
    margin:auto;
    margin-left:5px;
    overflow:auto;
    height:50vh;
    word-wrap:break-word;
    line-height: 1.5;
    font-size:16px;
    overflow:auto;
}
Run Code Online (Sandbox Code Playgroud)

Rem*_*and 8

我今天遇到了这个问题:https://github.com/rodneyrehm/viewport-units-buggyfill 它检查vh/vw单元的每个元素并将其转换为px.非常值得一试,我想.

  • 这在iOS Safari 10.3上不起作用.https://github.com/rodneyrehm/viewport-units-buggyfill/issues/82有一个简单的`高度:100vh;`仍然是屏幕的1/4.它似乎在桌面上工作(或什么都不做). (3认同)

Kou*_*Das 8

你可以使用jQuery来解决这个问题.

$('container').height($(window).height()/2);
Run Code Online (Sandbox Code Playgroud)

这适用于每个浏览器.希望能帮助到你.


小智 8

在 CSS Tricks 中有一个很好的答案 - https://css-tricks.com/the-trick-to-viewport-units-on-mobile/

.my-element {
  height: 100vh; /* Fallback for browsers that do not support Custom Properties */
  height: calc(var(--vh, 1vh) * 100);
}
Run Code Online (Sandbox Code Playgroud)
// First we get the viewport height and we multiple it by 1% to get a value for a vh unit
let vh = window.innerHeight * 0.01;
// Then we set the value in the --vh custom property to the root of the document
document.documentElement.style.setProperty('--vh', `${vh}px`);
Run Code Online (Sandbox Code Playgroud)


Cri*_*ris 6

我今天只使用了这个CSS混音,它起作用了。iOS 8.2 iPhone Safari:

html, body {
    ...
    width: -webkit-calc(100% - 0px);
    ...
}
Run Code Online (Sandbox Code Playgroud)

想法:使用calc似乎可以使Safari将单位转换为px本身。现在,我的页面已在iOS上的Chrome和Safari上正常显示。


Pra*_*han 6

我也有这个问题,这是我的解决方案.

这等于高度:100vh ;

document.getElementById("your-element").style.height = window.innerHeight + 'px';
Run Code Online (Sandbox Code Playgroud)

你也可以使用下面的代码与jQuery,

$('your-element').height(window.innerHeight + 'px');
Run Code Online (Sandbox Code Playgroud)

检查野生动物园:)