使用JavaScript滚动iframe? [英] Scrolling an iframe with JavaScript?

查看:243
本文介绍了使用JavaScript滚动iframe?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用JavaScript动态加载iframe。加载后,如何让它向下滚动特定数量的像素(即在iframe中的页面加载后,如何将iframe自身滚动到页面的指定区域?)

I dynamically load an iframe with JavaScript. After it's loaded, how can I make it scroll down a specific number of pixels (ie. after the page in the iframe has loaded, how can I make the iframe scroll by itself to the a specified region of the page?)

推荐答案

您可以使用 onload 事件来检测iframe何时加载完毕,在那里你可以使用iframe的contentWindow上的 scrollTo 功能来滚动到左边和上边的像素的定义位置(x,y):

You can use the onload event to detect when the iframe has finished loading, and there you can use the scrollTo function on the contentWindow of the iframe, to scroll to a defined position of pixels, from left and top (x, y):

var myIframe = document.getElementById('iframe');
myIframe.onload = function () {
    myIframe.contentWindow.scrollTo(xcoord,ycoord);
}

你可以查看一个有效的例子这里

You can check a working example here.

注意:如果两个页面都驻留,这将有效在同一个域名上。

Note: This will work if both pages reside on the same domain.

这篇关于使用JavaScript滚动iframe?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆