[英]How to add multiple div inside single div with horizontal scroll?
I am trying to add multiple div inside a single div with horizontal scroll. 我正在尝试使用水平滚动在单个div内添加多个div。
This is my code: 这是我的代码:
<div id="scrollimages">
<script>
var container = document.getElementById("scrollimages");
var array=["img/screen2.png","img/logo.png"];
for(var i=0;i<array.length;i++)
{
var src="url("+array[i]+")";
// var inside = '<div class="block" style="background-image: { ' + src + ' }"></div>';
var inside = '<div class="blocks"></div>'
inside.style.backgroundImage=src;
inside.style.marginLeft=100*i+"%";
container.innerHTML +=inside;
}
</script>
</div>
This is my css code: 这是我的CSS代码:
#scrollimages {
background-color: #00FFFF;
width: 100%;
margin-left:0px;
height: 150px;
overflow: scroll;
white-space:normal;
overflow-y:none;
margin-top:20px;
}
.blocks
{
height: 100%;
width: 100%
margin-left:0px;
word-wrap:normal;
}
First time i am using java script and css all ,so i can't get this easy step .help me out this problem. 第一次我使用java脚本和css all,所以我无法执行此简单步骤。请帮我解决这个问题。
what mistake i made, 我犯了什么错误
The same #id
can only be once in a document. 同一
#id
在一个文档中只能出现一次。
var inside = '<div id="block"></div>'
Use a class instead 改用课程
var inside = '<div class="block"></div>'
and in CSS 和在CSS中
.block { ...
Also, much easier, to add the attributes directly to the string 同样,将属性直接添加到字符串更容易
var inside = '<div class="block" style="background-image:'+ src +'"></div>';
Edit: fixed remove {} in style
. 编辑:修复了
style
中的{}。
Edit: There were some other problems in the script, here is a working jsfiddle: https://jsfiddle.net/C14L/9hnqheos/ 编辑:脚本中还有其他一些问题,这是一个有效的jsfiddle: https ://jsfiddle.net/C14L/9hnqheos/
Add Flex property to the container, also keep overflow auto. 将Flex属性添加到容器,并保持溢出自动。 If using Flex no need to use white-space.
如果使用Flex,则无需使用空格。
#scrollimages {
display: flex;
overflow: auto;
}
Instead of attaching background to div
I am creating img
& appending it inside the div. 我不是在
div
上附加背景,而是创建img
并将其附加到div
内。
Also 也
Hope this will be useful 希望这会有用
JS JS
var container = document.getElementById("scrollimages");
var myArray=[image urls];
for(var i=0;i < myArray.length;i++){
var _createElem = document.createElement('div');
var _img = document.createElement('img')
_img.src = myArray[i]
_createElem.id ="block_"+i;
_createElem.appendChild(_img)
_createElem.className = "block";
container.appendChild(_createElem);
}
CSS 的CSS
#scrollimages {
background-color: #00FFFF;
width: 100%;
margin-left:0px;
overflow-x:scroll;
margin-top:20px;
}
.block{
width:auto;
display:table-cell;
height: 100%;
}
you can make further changes if it required 您可以根据需要进行进一步的更改
As per C14L, need to assign class, and since you're using getElementByID, each element should be assigned a seperate ID. 根据C14L,需要分配类,并且由于您使用的是getElementByID,因此应为每个元素分配一个单独的ID。
In the below JS Fiddle, I've swapped backgroundImage for backgroundColor, and removed your margin code (not sure what you were attempted here, but you had 100% margin on the 2nd iteration, 200% for the third... Don't think thats what you had in mind... For demo purposes, height of 'block' was set to 30px instead of 100% since divs have no height by default... 在下面的JS小提琴中,我将backgroundImage替换为backgroundColor,并删除了边距代码(不知道您在这里尝试了什么,但是您在第二次迭代中有100%的余量,在第三次迭代中有200%的余量...不要认为这就是您的想法...出于演示目的,“ div”的高度设置为30px而不是100%,因为divs默认没有高度...
https://jsfiddle.net/7mua0rez/ https://jsfiddle.net/7mua0rez/
var container = document.getElementById("scrollimages");
var array=["red","green", "black"];
for(var i=0;i<array.length;i++)
{
var src=array[i];
var inside = '<div id="block' + i + '" class="block"></div>'
container.innerHTML +=inside;
document.getElementById("block" + i).style.backgroundColor=src;
document.getElementById("block" +i).style.marginLeft=10+"%";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.