colorboxのiframeを表示コンテンツに応じた可変サイズにするテストです。
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>test</title>
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="https://yarnpkg.com/en/package/normalize.css">
<link rel="stylesheet" href="colorbox.css">
<script src="http://code.jquery.com/jquery-3.1.1.js"></script>
<script src="jquery.colorbox-min.js"></script>
<script>
$(function(){
$(".iframe").colorbox({
iframe: true,
innerWidth: 600,// 幅初期設定
innerHeight: 400,// 高さ初期設定
onComplete : function() {
$('#cboxLoadedContent').children('iframe').on('load',function(){
var $iframe = $(this);
// bodyは特定の幅を持たないので、主要ブロックから最大幅を取得
var W = Math.max.apply(null, $iframe.contents().find('body').find('div,header,footer,main,section').map(function (){
return $(this).outerWidth();
}).get());
// ドキュメント(body)の高さを取得
var H = $iframe.get(0).contentWindow.document.body.clientHeight;
// リサイズ
$iframe.colorbox.resize({
innerWidth: W,
innerHeight: H,
});
});
}
});
});
</script>
</head>
<body>
<a href="modal.html" class="iframe">open iframe modal</a>
</body>
</html>
modal.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>modal</title>
<meta name="viewport" content="width=device-width">
<style>
body {
margin: 0;
}
main {
display: block;
width: 500px;
margin: auto;
padding: 20px;
background-color: #ccc;
box-sizing: border-box;
}
</style>
</head>
<body>
<main>
<p>colorbox iframe test</p>
</main>
</body>
参考
- https://codeday.me/jp/qa/20190402/505312.html
- https://highmoon-miyabi.net/blog/2016/04/21_000398.html
- http://chords.hatenablog.com/entry/2014/10/08/iframe%E3%81%A7%E5%9F%8B%E3%82%81%E8%BE%BC%E3%82%93%E3%81%A0%E3%83%9A%E3%83%BC%E3%82%B8%E3%81%AE%E5%B9%85%E3%83%BB%E9%AB%98%E3%81%95%E3%82%92%E5%8F%96%E5%BE%97%E3%81%99%E3%82%8B
- https://codeday.me/jp/qa/20190109/99770.html
- https://codeday.me/jp/qa/20190614/1014584.html
- https://codeday.me/jp/qa/20181213/50445.html
以上、ありがとうございました。