How to control the scrolling of embedded iframe in the page? The method is to use the scroll method of iframe window:
1. Get the window object of iframe
var iwin = document.getElementById('iframe1').contentWindow;
2. Get the window document object of the iframe
var doc = iwin.document;
3. Call the scroll method of the iframe window object
iwin.scroll(0,doc.body.scrollHeight);
The two parameters of scroll are the scrolling amount of x and y axes
doc.body.scrollHeight is the height of the iframe page (including the undisplayed part)
A comprehensive application example is as follows:
Copy the code code as follows:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>hover test</title>
<style type="text/css">
ul{
background-color:#ff00ff;
display:block;
}
.toc li{
position:relative; width:10em;
background-color:#00ff00;
display:block;
}
li a {
/*display:block;*/ /*If displayed in block mode, it will occupy the entire parent element space*/
background-color:#0000ff;
} /*Must make a a block-level element*/
li ai{
display:none;
}
li a:hover{
text-align:left;
}/*The code added here is only for IE6 to display. It has no special functions. It can be written except for text-decoration, color and z-index*/
.toc li a:hover i{
display:block;
width:6em;
position:absolute;
top:0;
left:100%; /* Here 100% refers to the width relative to the element li*/
margin:-1em 0 0 0em;
padding:1em;
background:#cde;
border:1px solid red;
text-align:left;
z-index:10000;
}
</style>
</head>
<body>
<iframe id="iframe1" src=""></iframe>
html code
<ul id="toc">
<li><a href="1.html">Chapter 1<i>In which a dragon is seen</i></a></li>
<li><a href="2.html">Chapter 1<i>In which a knight is summoned</i></a></li>
<li><a href="3.html">Chapter 1<i>In which a failure is disappointed</i></a></li>
<li><a href="4.html">Chapter 1<i>In which a dragon is seen</i></a></li>
<li><a href="5.html">Chapter 1<i>In which a dragon is seen</i></a></li>
<li><a href="6.html">Chapter 1<i>In which a dragon is seen</i></a></li>
<li><a href="7.html">Chapter 1<i>In which a dragon is seen</i></a></li>
</ul>
<script language="javascript">
function getElementAbsPos(e) {
var t = e.offsetTop;
var l = e.offsetLeft;
while(e = e.offsetParent) {
t += e.offsetTop;
l += e.offsetLeft;
}
return {left:l,top:t};
}
function getPosition(obj){
var left = 0;
var top = 0;
while(obj != document.body){
left = obj.offsetLeft;
top = obj.offsetTop;
obj = obj.offsetParent;
}
return left;
}
var lis = document.getElementsByTagName('li');
var iwin = document.getElementById('iframe1').contentWindow;
var doc = iwin.document;
for(var i=0;i<lis.length;i++){
lis[i].onmouseover = function(){
var obji = this.childNodes[0].childNodes[1];
doc.writeln('<br>'+ obji.innerText + ',' + getElementAbsPos(document.getElementById('toc')).left);
doc.writeln('<br>'+ obji.offsetLeft + ',' + getElementAbsPos(obji).left + ',' + obji.offsetWidth+ ',' + obji.style.left);
doc.writeln('<br><b>'+ doc.body.scrollHeight + '</b>')
iwin.scroll(0,doc.body.scrollHeight);
//iwin.scrollTo(10000); //Invalid
}
}
</script>
</body>
</html>