1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

frameset を iframe に置き換える方法

Last updated at Posted at 2021-08-04

背景

frameset は HTML5 で廃止予定のため、iframe に置き換える必要がある。
frameset では フレームの枠を掴んでサイズ変更できるが、
iframe ではサイズ変更できないのでJavaScriptでサイズ変更できるようにする。

実装方法

以下のように実装する。

<!DOCTYPE html>
<html>
<style>
body {
  margin: 0;
}
hr {
  cursor: n-resize;
  height: 2px;
  margin-top: 1px;
  margin-bottom: 5px;
}
#overlay {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 2;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, .3);
  transition: .3s linear;
  display: none;
  cursor: n-resize;
}
iframe {
  width: 100%;
  border: none;
}
</style>
<body>
<div id="overlay"></div>
<iframe id="frame1" src="https://en.wikipedia.org/wiki/IFrame" style="height:70vh"></iframe>
<hr>
<iframe id="frame2" src="https://en.wikipedia.org/wiki/IFrame" style="height:25vh"></iframe>

<script>
//Make the DIV element draggagle:
dragElement(document.querySelector("body > hr"));

function dragElement(elmnt) {
  elmnt.onmousedown = dragMouseDown;

  function dragMouseDown() {
    document.onmouseup = closeDragElement;
    // call a function whenever the cursor moves:
    document.onmousemove = elementDrag;
    document.getElementById("overlay").style.display = "block";
  }

  function elementDrag(e) {
    // set the element's new position:
    document.getElementById("frame1").style.height = (e.clientY - 15) + "px";
    document.getElementById("frame2").style.height = window.innerHeight - e.clientY - 10 + "px";
  }

  function closeDragElement() {
    /* stop moving when mouse button is released:*/
    document.onmouseup = null;
    document.onmousemove = null;
    document.getElementById("overlay").style.display = "none";
  }
}
</script>

</body>
</html>
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?