使用javascript上下滚动iframe [英] Using javascript to scroll iframe up and down

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

问题描述

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

Is it possible to scroll the iframe window from the parent window using keys or javascript? The iframe content is from another domain, different from the parent window.

推荐答案

由于iframe内容来自其他域,您将由于安全原因,无法改变它的DOM。

Since the iframe content is from another domain, you will not be able to alter it's DOM due to security reasons.

虽然你可以使用箭头键滚动它,但是当你激活它时。至少它适用于Chrome和Firefox。

Although you can scroll it by using the arrow keys, when you have it activated. At least it works for me in Chrome and Firefox.

如果你想能够从javascript滚动它,我建议采用以下方法。 (它假设您知道iframe内容和iframe的宽度和高度)。
基本上让你的DOM中的div来处理滚动。

If you want to be able to scroll it from javascript, I would suggest the following approach. (it assumes you know the width and height of the iframe content and your iframe). Basically let a div in your DOM take care of scrolling.

<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,您可以尝试这篇文章

For smoother scrolling of the div you could try the code from this article.

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

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