【发布时间】:2009-11-01 08:30:33
【问题描述】:
是否可以使用键或 javascript 从父窗口滚动 iframe 窗口? iframe 内容来自另一个域,与父窗口不同。
【问题讨论】:
是否可以使用键或 javascript 从父窗口滚动 iframe 窗口? iframe 内容来自另一个域,与父窗口不同。
【问题讨论】:
由于 iframe 内容来自另一个域,出于安全原因,您将无法更改其 DOM。
虽然您可以使用箭头键滚动它,但当您激活它时。至少它适用于 Chrome 和 Firefox。
如果您希望能够从 javascript 滚动它,我建议您采用以下方法。 (它假设您知道 iframe 内容和 iframe 的宽度和高度)。 基本上让 DOM 中的 div 负责滚动。
<a href="#" id="scroll">Scroll to (400,400)!</a><br />
<div id="google" style="width: 300px; height: 200px; overflow: auto;">
<iframe width="800" height="600" src="http://www.google.com/" scrolling="no">
</iframe>
</div>
<script type="text/javascript">
$("#scroll").click(function()
{
$("#google").scrollTop(400).scrollLeft(400);
return false;
});
</script>
为了更流畅地滚动 div,您可以尝试this article 中的代码。
【讨论】: