当前位置:Gxlcms > JavaScript > 使用iframewindow的scroll方法控制iframe页面滚动_javascript技巧

使用iframewindow的scroll方法控制iframe页面滚动_javascript技巧

时间:2021-07-01 10:21:17 帮助过:18人阅读

在页面中如何控制内嵌的iframe滚动呢?方法是使用iframe window的scroll方法:

1、获取iframe的窗口对象

var iwin = document.getElementById('iframe1').contentWindow;

2 、获取iframe的窗口document对象

var doc = iwin.document;

3、调用iframe window对象的scroll方法

iwin.scroll(0,doc.body.scrollHeight);

scroll两个参数为x,y轴的滚动量

doc.body.scrollHeight为iframe页面的高度(包含未显示的部分)

一个综合应用例子如下:
代码如下:





hover test






html代码


  • Chapter 1In which a dragon is seen

  • Chapter 1In which a knight is summoned

  • Chapter 1In which a proncess is disappointed

  • Chapter 1In which a dragon is seen

  • Chapter 1In which a dragon is seen

  • Chapter 1In which a dragon is seen

  • Chapter 1In which a dragon is seen