Unity Web GL HTML容器:如何基于浏览器宽度“拉伸”宽度窗口?

时间:2019-04-01 07:49:07

标签: html css

Unity WebGL构建创建一个html模板,我可以在其中指定宽度和高度大小。

如何根据浏览器窗口的宽度设置div id =“ gameContainer”的宽度以使其始终填充?

.webgl-content * {
  border: 0; 
  margin: 0; 
  padding: 0
}

.webgl-content {
  position: absolute; 
  top: 50%; 
  left: 50%; 
  -webkit-transform: translate(-50%, -50%); 
  transform: translate(-50%, -50%);
}

.webgl-content .logo, 
.progress {
  position: absolute; 
  left: 50%; 
  top: 50%; 
  -webkit-transform: translate(-50%, -50%); 
  transform: translate(-50%, -50%);
}

.webgl-content .logo {
  background: url('progressLogo.Light.png') no-repeat center / contain; 
  width: 154px; 
  height: 130px;
}

.webgl-content .progress {
  height: 18px; 
  width: 141px; 
  margin-top: 90px;
}

.webgl-content .progress .empty {
  background: url('progressEmpty.Light.png') no-repeat right / cover; 
  float: right; 
  width: 100%; 
  height: 100%; 
  display: inline-block;
}

.webgl-content .progress .full {
  background: url('progressFull.Light.png') no-repeat left / cover; 
  float: left; 
  width: 0%; 
  height: 100%; 
  display: inline-block;
}

.webgl-content .logo.Dark {
  background-image: url('progressLogo.Dark.png');
}

.webgl-content .progress.Dark .empty {
  background-image: url('progressEmpty.Dark.png');
}

.webgl-content .progress.Dark .full {
  background-image: url('progressFull.Dark.png');
}

.webgl-content .footer {
  margin-top: 5px; 
  height: 38px; 
  line-height: 38px; 
  font-family: Helvetica, Verdana, Arial, sans-serif; 
  font-size: 18px;
}

.webgl-content .footer .webgl-logo, 
.title, 
.fullscreen {
  height: 100%; 
  display: inline-block; 
  background: transparent center no-repeat;
} 

.webgl-content .footer .webgl-logo {
  background-image: url('webgl-logo.png'); 
  width: 204px; 
  float: left;
}

.webgl-content .footer .title {
  margin-right: 10px; 
  float: right;
}

.webgl-content .footer .fullscreen {
  background-image: url('fullscreen.png'); 
  width: 38px; 
  float: right;
}

form input[type="file"] {
  display: none;
  position: absolute;
}
<body>
  <div class="webgl-content">
    <div id="gameContainer" style="width: 960px; height: 600px; margin: auto"></div>
  </div>

  <!-- BEGIN WEBGL FILE BROWSER LIB -->
  <form id="fileBrowserPopup" style="display: none;">
    <img src="TemplateData/2x2.png" style="position: absolute; width: 100%; height: 100%;"/>
    <img src="TemplateData/White-Button.png" style="position: absolute;  top: 35%; left: 38%; width: 25%; height: 28%;"/>

      <label for="fileToUpload">
        <img src="TemplateData/upload_button.png" style="position: absolute; top: 45%; left: 42.8%; width: 16%; height: 10%;"/>
      </label>
      <input type="File" name="fileToUpload" id="fileToUpload" style="width: 0px; height: 0px;" onchange="sendfile(event);return false;" />
  </form>
</body>

1 个答案:

答案 0 :(得分:1)

您可以使用vw代表视图宽度属性。 vh代表视图高度。

详细了解可在MDN https://developer.mozilla.org/en-US/docs/Web/CSS/length#Viewport-percentage_lengths上使用的可能值

因此,如果您希望它具有与视口一样宽的高度,则可以将其设置为:

<div id="gameContainer" style="width: 100vw; height: 100vh; margin: auto"></div>

如本片段所示,我也将内联css也移到了css文件中。

.body {
   margin: 0px;
}
#gameContainer {
  width: 100vw;
  height: 100vh;
  display: block;
  background-color: black;
}

.webgl-content * {
  border: 0; 
  margin: 0; 
  padding: 0
}

.webgl-content {
  position: absolute; 
  top: 50%; 
  left: 50%; 
  -webkit-transform: translate(-50%, -50%); 
  transform: translate(-50%, -50%);
}

.webgl-content .logo, 
.progress {
  position: absolute; 
  left: 50%; 
  top: 50%; 
  -webkit-transform: translate(-50%, -50%); 
  transform: translate(-50%, -50%);
}

.webgl-content .logo {
  background: url('progressLogo.Light.png') no-repeat center / contain; 
  width: 154px; 
  height: 130px;
}

.webgl-content .progress {
  height: 18px; 
  width: 141px; 
  margin-top: 90px;
}

.webgl-content .progress .empty {
  background: url('progressEmpty.Light.png') no-repeat right / cover; 
  float: right; 
  width: 100%; 
  height: 100%; 
  display: inline-block;
}

.webgl-content .progress .full {
  background: url('progressFull.Light.png') no-repeat left / cover; 
  float: left; 
  width: 0%; 
  height: 100%; 
  display: inline-block;
}

.webgl-content .logo.Dark {
  background-image: url('progressLogo.Dark.png');
}

.webgl-content .progress.Dark .empty {
  background-image: url('progressEmpty.Dark.png');
}

.webgl-content .progress.Dark .full {
  background-image: url('progressFull.Dark.png');
}

.webgl-content .footer {
  margin-top: 5px; 
  height: 38px; 
  line-height: 38px; 
  font-family: Helvetica, Verdana, Arial, sans-serif; 
  font-size: 18px;
}

.webgl-content .footer .webgl-logo, 
.title, 
.fullscreen {
  height: 100%; 
  display: inline-block; 
  background: transparent center no-repeat;
} 

.webgl-content .footer .webgl-logo {
  background-image: url('webgl-logo.png'); 
  width: 204px; 
  float: left;
}

.webgl-content .footer .title {
  margin-right: 10px; 
  float: right;
}

.webgl-content .footer .fullscreen {
  background-image: url('fullscreen.png'); 
  width: 38px; 
  float: right;
}

form input[type="file"] {
  display: none;
  position: absolute;
}
<body>
  <div class="webgl-content">
    <div id="gameContainer"></div>
  </div>

  <!-- BEGIN WEBGL FILE BROWSER LIB -->
  <form id="fileBrowserPopup" style="display: none;">
    <img src="TemplateData/2x2.png" style="position: absolute; width: 100%; height: 100%;"/>
    <img src="TemplateData/White-Button.png" style="position: absolute;  top: 35%; left: 38%; width: 25%; height: 28%;"/>

      <label for="fileToUpload">
        <img src="TemplateData/upload_button.png" style="position: absolute; top: 45%; left: 42.8%; width: 16%; height: 10%;"/>
      </label>
      <input type="File" name="fileToUpload" id="fileToUpload" style="width: 0px; height: 0px;" onchange="sendfile(event);return false;" />
  </form>

</body>