使动态创建的div水平滚动

时间:2016-08-24 20:47:39

标签: javascript html css

我正在尝试使用javascript创建一组div来继续水平制作。我似乎无法找到解决方案。我基本上需要它继续水平添加然后能够水平滚动。目前,他们被创建到浏览器窗口的边缘,然后在下面。



function makeResponseBox() {
    document.getElementById("calculate").onclick = function()
    {
        var responseBox = document.createElement("DIV"); //create <div>
        responseBox.setAttribute("class", "content" );
        document.getElementById('container').appendChild(responseBox);
    }

}//close function (makeResponseBox)

window.onload=makeResponseBox;
&#13;
body {
	margin: 0 0;
}
#container {
	border: 1px solid blue;

}
#headerbar {
	font-size: 26px;
	color: white;
	padding-left: 10px;
	border: 1px solid blue;
	height: 50px;

}
#sidebar {
	color: black;
	border: 1px solid blue;
	width: 50px;
	float: left;
	height: 100vh;
}
.content {
	width: 200px;
	height: 100vh;
	float: left;
	display: inline;
	border: 1px solid blue;
}
&#13;
<div id='container'>
<div id='headerbar'>Test Div</div>
<div id='sidebar'> <input type="button" value="Calculate" id="calculate" />
<br /><br />
<br /><br />
</div>
<div class='content'>test1</div>


</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

white-space: nowrap上使用.container以防止内嵌项目换行。将.content.sidebar设置为display: inline-block; vertical-align: top;,然后移除浮动以维持一行:

function makeResponseBox() {
    document.getElementById("calculate").onclick = function() {
      var responseBox = document.createElement("DIV"); //create <div>
      responseBox.setAttribute("class", "content");
      document.getElementById('container').appendChild(responseBox);
    }

  } //close function (makeResponseBox)

window.onload = makeResponseBox;
body {
  margin: 0 0;
}
#container {
  border: 1px solid blue;
  white-space: nowrap; /*** prevent the divs from wrapping to the next lines ***/
  overflow: auto; /** resize the container with the added content **/
    font-size: 0; /** remove spaces between inline-block elements **/
}

#container > * {
  font-size: 16px; /** set font-size to all direct children of #container **/
}

#headerbar {
  font-size: 26px;
  color: white;
  padding-left: 10px;
  border: 1px solid blue;
  height: 50px;
}
#sidebar {
  display: inline-block;
  color: black;
  border: 1px solid blue;
  width: 50px;
  height: 100vh;
  vertical-align: top;
}
.content {
  display: inline-block; /*** this will make the divs stay on the same line, but still have the attributes of a block element ***/
  width: 200px;
  height: 100vh;
  border: 1px solid blue;
  margin: 0 5px 0 0;
  vertical-align: top;
}
<div id="container">
  <div id="headerbar">Test Div</div>
  <div id="sidebar">
    <input type="button" value="Calculate" id="calculate" />
    <br />
    <br />
    <br />
    <br />
  </div>
  <div class="content">test1</div>


</div>