Use the scroll method of the iframe window to control the scrolling of the iframe page

  • 2020-03-30 02:16:15
  • OfStack

How do I control the scrolling of an embedded iframe on a page? Method is to use the scroll method of the iframe window:

1. Gets the window object of the iframe

Var iwin = document. GetElementById (' iframe1). ContentWindow;

2. Gets the iframe window document object

Var doc = iwin. Document;

3. Call the scroll method of the iframe window object

Iwin. Scroll (0, doc. Body. ScrollHeight);

Scroll is the amount of scroll in the y axis

Doc.body.scrollheight is the height of the iframe page (containing undisplayed parts)

A comprehensive application example is 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 { 
  
background-color:#0000ff; 
}  

li a i{ 
display:none; 
} 

li a:hover{ 
text-align:left; 
} 

.toc li a:hover i{ 
display:block; 
width:6em; 
position:absolute; 
top:0; 
left:100%;  
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="" width="400" height="300"></iframe> 

html code  

<ul class="toc" 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 proncess 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> 

Related articles: