ฉันจะทำให้ flexbox นี้ขยายพร้อมกับรายการดิ้นได้อย่างไร [ทำซ้ำ]

ในโค้ดตัวอย่างของฉัน โปรดคลิกที่ปุ่มสร้างเนื้อหาเพื่อทำความเข้าใจปัญหา

เมื่อคุณคลิกที่ปุ่ม คุณจะเห็นรายการ Flex(.each-result) ทั้งหมดที่สร้างขึ้น พวกมัน เกือบ ถูกล้อมด้วย div/flexbox (.result-container) โดยสมบูรณ์ ซึ่งระบุด้วยเส้นขอบจุดสีน้ำเงิน ถ้าฉันลบระยะขอบออกจากรายการดิ้น มันจะพอดีกับ div อย่างสมบูรณ์ อย่างไรก็ตาม เมื่อฉันเพิ่มระยะขอบ div หลัก (เช่น flexbox) จะไม่ขยายจนเต็มความกว้าง ยังคงมีความกว้างเท่าเดิมเมื่อไม่มีขอบ

มีวิธีการเปลี่ยนแปลงนี้เพื่อให้ div ขยายเมื่อเพิ่มระยะขอบหรือไม่

const leftArrow = document.querySelector('#left-arrow');
const rightArrow = document.querySelector('#right-arrow');
const rootDiv = document.querySelector('#root');
const generateButton = document.querySelector("#button-generate");
var navMargin = '';
let rootContainerWidth = window.getComputedStyle(rootDiv, null).getPropertyValue("width");
console.log(`Window size onload: ${rootContainerWidth}`);
window.addEventListener('resize', () => {
  rootContainerWidth = window.getComputedStyle(rootDiv, null).getPropertyValue("width");
  console.log(`The new window size is ${rootContainerWidth}`);
})
//This code basically generates the content within the div
generateButton.addEventListener("click", () => {
  for (let i = 0; i < 10; i++) {
    const newDiv = document.createElement("div");
    newDiv.classList.add("each-result");
    newDiv.appendChild(addImg("https://uk.usembassy.gov/wp-content/uploads/sites/16/please_read_icon_150x150.jpg"));
    rootDiv.appendChild(newDiv);  
  }
  rootDiv.firstElementChild.classList.add('nav-margin');
  navMargin = document.querySelector('.nav-margin');
});

//These enable the arrow to scroll through the dynamically generated content
// function navArrow () {
//   leftArrow.addEventListener('click', () => {
 
//   });
// rightArrow.addEventListener('click', () => {
//   if ()
//   });
// }




//Simple function to create and image element with the src attribute set in one line
function addImg(url) {
  const newImg = document.createElement("img");
  newImg.setAttribute("src", url);
  return newImg;
}
html, body {
  height: 100%;
}

button {
  position: relative;
  z-index: 1
  width: auto;
  height: 50px;
}

.container {
  display: flex;
  justify-content: center;
  position: relative;
  top: 15%;
  z-index: 0
}

.each-result {
  height: 150px;
  width: 150px;
  border: 3px dotted red;
  margin: 0 1%;
}
img {
  height: 100%;
  width: auto;
}
.nav-arrows {
  display: flex;
  justify-content: space-between;
  width: 100%;
  height: auto;
  position: absolute;
  background: clear;
  pointer-events: none;
}

#left-arrow, #right-arrow {
  pointer-events: auto;
}

#root-container {
  display: flex;
  align-items: center;
  border: 1px solid black;
  height: 200px;
  position: relative;
  flex-flow: row no-wrap;
/*   overflow: hidden; */
  width: 100%;
}

.result-container {
  display: flex;
  border: 2px blue dotted;
  
}
<script src="https://use.fontawesome.com/releases/v5.0.6/js/all.js"></script>
<div class="container">
  <div class="nav-arrows">
      <button id="left-arrow"><i class="fas fa-arrow-alt-circle-left"></i>
      </button>
        <button id="right-arrow"> <i class="fas fa-arrow-alt-circle-right"></i>
      </button>
    </div>
  <div id="root-container">
  <div id="root" class="result-container">
  </div>
  </div>

</div>
<button id="button-generate">Generate Content</button>


person WonkasWilly    schedule 25.02.2018    source แหล่งที่มา
comment
ตามที่คนหลอกลวงแนะนำ สิ่งที่คุณต้องทำก็แค่เช่น ใช้หน่วยวิวพอร์ตแทนเปอร์เซ็นต์สำหรับระยะขอบ margin: 0 1vw   -  person Ason    schedule 25.02.2018
comment
การเพิ่ม margin: 0 1vw; ไม่ได้ให้ผลลัพธ์ที่ต้องการ   -  person Brett DeWoody    schedule 25.02.2018
comment
การใช้หน่วยวิวพอร์ตช่วยได้อย่างแน่นอน ฉันชื่นชมคำตอบอื่นของคุณเช่นกัน   -  person WonkasWilly    schedule 25.02.2018
comment
@BrettDeWoody แน่นอนว่าเป็นเช่นนั้น (และ OP ก็บอกว่า) ในกรณีนี้ 1% จะถูกหยิบมาจากพาเรนต์ซึ่งมีความกว้างวิวพอร์ตเต็ม และด้วย 1% นั้น = 1 vw   -  person Ason    schedule 26.02.2018
comment
เห็นด้วยที่จะไม่เห็นด้วย @LGSon ด้วย 1vw ระยะห่างไม่เท่ากัน   -  person Brett DeWoody    schedule 26.02.2018
comment
@BrettDeWoody คุณหมายถึงในกรณีข้างต้นโดยที่ 1% ไม่ได้สร้างระยะห่างเหมือนกับที่ 1vw ทำใช่ไหม   -  person Ason    schedule 26.02.2018
comment
@BrettDeWoody หรือคุณหมายถึงว่า 1% ขององค์ประกอบที่รับ 100% ของวิวพอร์ตไม่เหมือนกับ 1vw?   -  person Ason    schedule 26.02.2018
comment
แบบแรก - 1vw ไม่ได้ให้ผลลัพธ์เหมือนกับ 1%   -  person Brett DeWoody    schedule 26.02.2018
comment
@BrettDeWoody แน่นอน... และสิ่งที่ซ้ำกันอธิบายว่าทำไมและในกรณีนี้โดยที่พาเรนต์ใช้ความกว้างวิวพอร์ตเต็ม 1% จะอยู่ในการตั้งค่าที่ไม่ยืดหยุ่นจะเหมือนกับ 1vw ดังนั้น 1vw จึงสามารถใช้เพื่อบรรลุ ต้องการเอาต์พุต   -  person Ason    schedule 27.02.2018


คำตอบ (1)


หาก margin สามารถเป็นค่าคงที่ได้ (แทนที่จะเป็นเปอร์เซ็นต์) เราสามารถ calc() ความกว้างขององค์ประกอบเพื่อพิจารณาระยะขอบได้ ตัวอย่างเช่น หากเราต้องการระยะขอบเป็น 20px เราจะดำเนินการต่อไปนี้กับองค์ประกอบ .each-result:

.each-result {
  width: calc(10% + 20px);
  margin: 0 20px;
}

นี่คือการสาธิตการทำงาน:

const leftArrow = document.querySelector('#left-arrow');
const rightArrow = document.querySelector('#right-arrow');
const rootDiv = document.querySelector('#root');
const generateButton = document.querySelector("#button-generate");
var navMargin = '';
let rootContainerWidth = window.getComputedStyle(rootDiv, null).getPropertyValue("width");
console.log(`Window size onload: ${rootContainerWidth}`);
window.addEventListener('resize', () => {
  rootContainerWidth = window.getComputedStyle(rootDiv, null).getPropertyValue("width");
  console.log(`The new window size is ${rootContainerWidth}`);
})
//This code basically generates the content within the div
generateButton.addEventListener("click", () => {
  for (let i = 0; i < 10; i++) {
    const newDiv = document.createElement("div");
    newDiv.classList.add("each-result");
    newDiv.appendChild(addImg("https://uk.usembassy.gov/wp-content/uploads/sites/16/please_read_icon_150x150.jpg"));
    rootDiv.appendChild(newDiv);  
  }
  rootDiv.firstElementChild.classList.add('nav-margin');
  navMargin = document.querySelector('.nav-margin');
});

//These enable the arrow to scroll through the dynamically generated content
// function navArrow () {
//   leftArrow.addEventListener('click', () => {
 
//   });
// rightArrow.addEventListener('click', () => {
//   if ()
//   });
// }




//Simple function to create and image element with the src attribute set in one line
function addImg(url) {
  const newImg = document.createElement("img");
  newImg.setAttribute("src", url);
  return newImg;
}
html, body {
  height: 100%;
}

button {
  position: relative;
  z-index: 1
  width: auto;
  height: 50px;
}

.container {
  display: flex;
  justify-content: center;
  position: relative;
  top: 15%;
  z-index: 0
}

.each-result {
  height: 150px;
  width: calc(10% + 20px);
  margin: 0 20px;
  border: 3px dotted red;
}

img {
  height: 100%;
  width: auto;
}
.nav-arrows {
  display: flex;
  justify-content: space-between;
  width: 100%;
  height: auto;
  position: absolute;
  background: clear;
  pointer-events: none;
}

#left-arrow, #right-arrow {
  pointer-events: auto;
}

#root-container {
  display: flex;
  align-items: center;
  border: 1px solid black;
  height: 200px;
  position: relative;
  flex-flow: row no-wrap;
/*   overflow: hidden; */
  width: 100%;
}

.result-container {
  display: flex;
  border: 2px blue dotted;
}
<script src="https://use.fontawesome.com/releases/v5.0.6/js/all.js"></script>
<div class="container">
  <div class="nav-arrows">
      <button id="left-arrow"><i class="fas fa-arrow-alt-circle-left"></i>
      </button>
        <button id="right-arrow"> <i class="fas fa-arrow-alt-circle-right"></i>
      </button>
    </div>
  <div id="root-container">
  <div id="root" class="result-container">
  </div>
  </div>

</div>
<button id="button-generate">Generate Content</button>

person Brett DeWoody    schedule 25.02.2018
comment
ไม่มีเหตุผลใดที่จะเริ่มยุ่งกับ calc(); สิ่งนี้เกี่ยวข้องกับปัญหาโดยสิ้นเชิงเมื่อใช้เปอร์เซ็นต์กับลูกดิ้น ดังนั้นฉันจึงปิดสิ่งนี้เนื่องจากซ้ำกัน - person Ason; 25.02.2018