一、iframe
元素会创建包含另外一个文档的内联框架(即行内框架)。
二、有以下方式对iframe进行设置高度和宽度
1、iframe元素标签可以使用width和height属性,
<iframe
src
="/index.html"
width="400"
height="300"></iframe>
2、可以使用css来定义宽度和高度
<iframe
src
="/index.html"
style="width:400pxheight:300px"></iframe>
三、补充:
iframe的属性如下:
index.html 和 iframe.html 必须同域且使用http协议
整个页面 index.html
<!doctype html><html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.stage{
margin:auto 50px
}
.dropDownPanel{
position:fixed
width:200px
height:200px
background-color:green
}
iframe{
display:block
width:100%
height:200px
border:1px solid
margin-top:20px
}
</style>
<script>
window.$showDropDown = function(top,left){
var dropDownPanel = window.$dropDownPanel
if(!dropDownPanel){
dropDownPanel = document.createElement('div')
document.body.appendChild(dropDownPanel)
dropDownPanel.className = 'dropDownPanel'
dropDownPanel.addEventListener('click',function(e){
e.stopPropagation()
})
document.addEventListener('click',function(){
window.$hideDropDown()
})
}
dropDownPanel.style.top = top + 'px'
dropDownPanel.style.left = left + 'px'
dropDownPanel.style.display = 'block'
window.$dropDownPanel = window.$dropDownPanel || dropDownPanel
}
window.$hideDropDown = function(){
window.$dropDownPanel && (window.$dropDownPanel.style.display = 'none')
}
window.$getOffset = function(el){
var offset = { top:el.offsetTop,left:el.offsetLeft,height:el.offsetHeight,width:el.offsetWidth }
var parent = el.offsetParent
while(parent){
offset.top += parent.offsetTop
offset.left += parent.offsetLeft
parent = parent.offsetParent
}
return offset
}
window.$bindDropDownHandler = function(iframe){
iframe.contentWindow.$iframeOffset = window.$getOffset(iframe)
iframe.contentWindow.document.addEventListener('click',function(){
window.$hideDropDown()
})
}
</script>
</head>
<body>
<div class="stage">
<iframe src="iframe.html" frameborder="0" onload="window.$bindDropDownHandler(this)"></iframe>
<iframe src="iframe.html" frameborder="0" onload="window.$bindDropDownHandler(this)"></iframe>
</div>
</body>
</html>
iframe页面 iframe.html
<!doctype html><html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
html{
width:100%
height:200px
background-color:gray
}
button{
position:fixed
bottom:20px
left:50px
}
</style>
<script>
window.addEventListener('load',function(){
document.getElementsByTagName('button')[0].addEventListener('click',function(e){
e.stopPropagation()
var offset = window.top.$getOffset(this)
window.top.$showDropDown(
offset.height + offset.top + window.$iframeOffset.top,
offset.left + window.$iframeOffset.left
)
})
})
</script>
</head>
<body>
<button>showdropdown</button>
</body>
</html>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)