浮动左侧对一系列的 `div` 元素不起作用。

huangapple go评论79阅读模式
英文:

float-left not working for a series of divs

问题

I have the following Javascript code:

for(var i = 0; i < 8; i++){
  var x = document.createElement('div'); 
  x.setAttribute('class','rowone');
  x.setAttribute('id',i);
  var move = 4;
  x.style.marginLeft = move + "%";
  x.addEventListener('click',clickdata,'false');
  // creating each div block
  
  var p = document.createElement('IMG');
  p.style.width = "80%";
  p.style.height="100%";
  p.src = orderone[i];
  x.appendChild(p);
  // creating the text in block, mostly for test
  document.body.append(x);
  // appending x into the document
} // creating first row and adding event listener

for(var j = 0; j < 8; j++){
  var x = document.createElement('div'); 
  x.setAttribute('class','rowtwo');
  x.setAttribute('id',j+8);
  var moverow = 4;
  x.style.marginLeft = moverow + "%";
  x.addEventListener('click',secondclick, 'false');
  // creating each div block

  var otherp = document.createElement('IMG');
  otherp.style.width = "80%";
  otherp.style.height="100%";
  otherp.src = ordertwo[j];
  x.appendChild(otherp);
  // creating the text in block, mostly for test
  document.body.append(x);
  // appending x into the document
}

(orderone和ordertwo只是图像数组)
它的作用是创建两行div,所有div都有一个图像(来自orderone或ordertwo)。这是它应该看起来的样子:

浮动左侧对一系列的 `div` 元素不起作用。

然而,行经常会随机分开(有时候它会正常工作,其他时候它只会给出其他排列,如下所示):

浮动左侧对一系列的 `div` 元素不起作用。

你应该怎么做?

英文:

I have the following Javascript code:

{
var x = document.createElement('div'); 
x.setAttribute('class','rowone');
x.setAttribute('id',i);
var move = 4;
x.style.marginLeft = move + "%";
x.addEventListener('click',clickdata,'false');
} // creating each div block
{ 
var p = document.createElement('IMG');
p.style.width = "80%";
p.style.height="100%";
p.src = orderone[i];
x.appendChild(p);
} // creating the text in block, mostly for test
document.body.append(x); // appending x into the document
} // creating first row and adding event listener
for(var j = 0; j < 8; j++){
{
var x = document.createElement('div'); x.setAttribute('class','rowtwo');
x.setAttribute('id',j+8);
var moverow = 4;
x.style.marginLeft = moverow + "%";
x.addEventListener('click',secondclick, 'false');
} // creating each div block
{ 
/*var p = document.createElement('p');
p.innerHTML = ordertwo[j];*/
var otherp = document.createElement('IMG');
otherp.style.width = "80%";
otherp.style.height="100%";
otherp.src = ordertwo[j];
x.appendChild(otherp);
} // creating the text in block, mostly for test
document.body.append(x); // appending x into the document
}

(orderone and ordertwo are just the arrays of images)
What it does is that it creates two rows of divs, all divs having an image (from orderone or ordertwo). This is what it should look like:
浮动左侧对一系列的 `div` 元素不起作用。

However, the rows often just break up randomly (sometimes it works right, other times it just gives some other assortment, like below):
浮动左侧对一系列的 `div` 元素不起作用。

What do I do?

答案1

得分: 0

将所有的 div 容器设置为相同的高度,并且像这样隐藏溢出内容:

.myDivs { 
    height: 200px;
    overflow: hidden;
}

将高度更改为与您的图像大部分或最高图像的高度相匹配。任何比您的高度更高的内容将被隐藏。

英文:

Set all the div containers to the same height and overflow to hidden like this:

.myDivs { 
height: 200px;
overflow: hidden;
}

Change the height to match the bulk of your images or the height of the tallest image. Anything that is taller than your height will be hidden

huangapple
  • 本文由 发表于 2023年5月22日 04:52:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76301878.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定