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

查看:27
本文介绍了使用 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 何时完成加载,然后您可以使用 scrollTo 函数在 iframe 的 contentWindow 上,从左到上滚动到像素的定义位置(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天全站免登陆