【问题标题】:Using javascript to scroll iframe up and down使用javascript上下滚动iframe
【发布时间】:2009-11-01 08:30:33
【问题描述】:

是否可以使用键或 javascript 从父窗口滚动 iframe 窗口? iframe 内容来自另一个域,与父窗口不同。

【问题讨论】:

    标签: jquery iframe key


    【解决方案1】:

    由于 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 中的代码。

    【讨论】:

    猜你喜欢
    • 2010-11-14
    • 2011-01-22
    • 2014-11-21
    • 1970-01-01
    • 1970-01-01
    • 2011-02-12
    • 2011-07-03
    • 2014-04-19
    • 2013-08-11
    相关资源
    最近更新 更多