html 动态调整大小和静态元素的大小调整

qc6wkl3g  于 11个月前  发布在  其他
关注(0)|答案(2)|浏览(141)

我是新的sof所以请让我知道,如果我给的信息是不完整或不足。
我得把iFrames并排坐着。我希望能够在x轴上调整它们的大小,当我调整其中一个时,另一个相应地调整大小以适应屏幕。
我得到了调整其中一个部分下来,但其他元素不调整大小,特别是包含“docWindow”的iFrame。
以下是相关的html(如果不够,请告知):

.resizable {
  display: flex;
  margin: 0;
  padding: 0;
  resize: horizontal;
  overflow: hidden
}

.resizable>.resized {
  flex-grow: 1;
  margin: 0;
  padding: 0;
  border: 0
}

.ugly {
  background: red;
  border: 4px dashed black;
}

#container2 {
  flex: 0 0 225px;
  left: 25px;
  top: 108px;
  height: 828px;
  border: thin solid black;
  position: absolute;
  width: 225px;
  margin-bottom: 100px;
  background-color: #e9e9ed;
  word-wrap: break-word;
}

#container1 {
  flex: 1;
  border-left: none;
  border-right: thin solid black;
  border-bottom: thin solid black;
  border-top: thin solid black;
  position: relative;
  left: 245px;
  top: 100px;
  background-color: #e9e9ed;
}

个字符

ws51t4hk

ws51t4hk1#

对于这种情况,Grid比flexbox工作得更好,因为您可以将左列的宽度设置为auto,以便调整以适应其内容(可调整大小的iframe),并将右列的宽度设置为1fr以占用所有剩余空间。给予左边的iframe指定宽度,并使右边的iframe为100%宽度。

.d1 {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 5px;
}
.d1 iframe {
  display: block;
  border: 5px solid blue;
  box-sizing: border-box;
  background: yellow;
  min-width: 80px;
}
.d1 iframe:first-child {
  width: 200px;
  max-width: 100%;
  resize: horizontal;
}
.d1 iframe:last-child {
  width: 100%;
}

个字符

xienkqul

xienkqul2#

不需要使用那些复杂的相对和绝对定位。对于iframe,您只需要两个flex元素,分别具有felx-grow:1width="100%"height="100%"。就这样

.resizable {
  display: flex;
  margin: 0;
  padding: 0;
  resize: horizontal;
  overflow: hidden;
  border:1px solid red;
  flex-basis: 0;  /* Aded for Safari */
}

#container2 {
  flex-grow:1;
  height: 400px;
}

#container1 {
  flex-grow:1;
  height: 400px;
}

个字符
最后,如果你在Safari中调整resizable元素的大小有问题,你可以使用这个JS来处理调整大小:

<script>

 function isSafari() {
    return /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
  }

// Check if the browser is Safari and then execute the code
if (isSafari()) {
  const container1 = document.getElementById('container1');
  const container2 = document.getElementById('container2');
  const dragHandle = document.createElement('div');
  dragHandle.classList.add('drag-handle');
  container1.appendChild(dragHandle);

  let startX, startWidth;

  dragHandle.addEventListener('mousedown', (e) => {
    startX = e.pageX;
    startWidth = container1.offsetWidth;
    document.addEventListener('mousemove', doDrag);
    document.addEventListener('mouseup', stopDrag);
  });

  function doDrag(e) {
    const newWidth = startWidth + e.pageX - startX;
    container1.style.flexBasis = `${newWidth}px`;
  }

  function stopDrag() {
    document.removeEventListener('mousemove', doDrag);
    document.removeEventListener('mouseup', stopDrag);
  }
}
</script>

相关问题