【问题标题】:width different from 100% for div containerdiv 容器的宽度不同于 100%
【发布时间】:2015-10-23 19:33:51
【问题描述】:

我有一个显示 3D 图形的简单 WebGL 代码。它工作得很好,但我想为宽度设置一个不同于 100% 的值;即我想把 webGL 动画放在一个小盒子里。

在我的代码下面;我试图将 WebGL 放入带有 CSS“高度”和“宽度”的 500px 框中,但它不起作用:动画仍然占用 100% 的宽度:

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>three.js webgl - trackball controls</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
        <style>
body {
    color: #000;
    font-family:Monospace;
    font-size:13px;
    text-align:center;
    font-weight: bold;

    background-color: #fff;
    margin: 0px;
    overflow: hidden;
}

.container {
    width: 500px;
    height: 500px;
}

        </style>
    </head>

    <body>
        <center><div id="container"></div></center>
        <script src="three.js/build/three.min.js"></script>

        <script src="three.js/examples/js/controls/TrackballControls.js"></script>

        <script src="three.js/examples/js/Detector.js"></script>
        <script src="three.js/examples/js/libs/stats.min.js"></script>

        <script>

if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

var container, stats;

var camera, controls, scene, renderer;

var cross;

init();
animate();

function init() {
...
}

function onWindowResize() {
...
}

function animate() {

    requestAnimationFrame( animate );
    controls.update();

    // For displaying
    render();

}

function render() {

    renderer.render( scene, camera );
    stats.update();

}
        </script>
    </body>
</html>

如何规避这个问题?

谢谢

更新 1:

好的,对于类 ID 混淆感到抱歉。

关于 div 容器,我在WebGL 代码中使用appenChild 添加renderer domElement

使用以下代码:

<html lang="en">
    <head>
        <title>three.js webgl - trackball controls</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
        <style>
body {
    color: #000;
    font-family:Monospace;
    font-size:13px;
    text-align:center;
    font-weight: bold;

    background-color: #fff;
    margin: 0px;
    overflow: hidden;               
}

#container {
    width: 300px;
    height: 300px;
    margin-left: auto;
    margin-right: auto;

}

        </style>
    </head>

    <body>
        <div id="container"></div>
        <script src="three.js/build/three.min.js"></script>

        <script src="three.js/examples/js/controls/TrackballControls.js"></script>

        <script src="three.js/examples/js/Detector.js"></script>
        <script src="three.js/examples/js/libs/stats.min.js"></script>

        <script>

if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

...

</script>
</body>
</html>

我没有得到带有 WebGL 动画的居中框。 WebGL 窗口像这样向右移动(宽度设置为 300 像素),但它不像我想的那样居中。

PS:margin-left:auto 和 margin-right:auto 似乎不适用于居中。

谢谢。

【问题讨论】:

  • initonWindowResize 是与画布大小最相关的函数。请提供这些。

标签: javascript html css three.js


【解决方案1】:

您的 html 存在多个问题。

  1. 您的容器在您的 css 中定义为一个类,您在 html 中将其作为 id

  2. 你在 div 容器中什么都没有

  3. &lt;center&gt;&lt;/center&gt; 已弃用 在您的 CSS 中使用 margin-left:auto; margin:right:auto;

再试一次,看看你的进展如何

【讨论】:

【解决方案2】:

1)

画布仍然是页面宽度的 100%,因为您可能在 onWindowResize 函数中进行了设置。为了让你的画布有一定的大小,你需要告诉three.js,而不是CSS。

为了突出显示,这几行取决于宽度和高度:

camera = new THREE.PerspectiveCamera(50, width / height, 1, 10000);
renderer.setSize(width, height);

2)

如果您的容器确实是一个“容器”,您希望将margin-left: auto; margin-right: auto 设置为画布。另外,由于画布默认显示为inline,所以需要使用display: block


查看实际操作:http://jsfiddle.net/60uzdyss/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-04
    • 1970-01-01
    • 2015-05-09
    • 2017-08-06
    • 2015-09-11
    • 2013-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多