如何使用javascript滚动iFrame [英] How to Scroll an iFrame using javascript

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

问题描述

我有几个iFrame加载外部网页,虽然只有1出现在一次,因此所有的id =iFrame我想添加到按钮(而不是滚动条),所以用户可以向下或向上滚动按下他们,我试图这样做使用javascript,但到目前为止我没有成功,我已经阅读了一些职位在这里,并尝试 答案但是到目前为止没有运气。我也试过:

I have several iFrames that load an external webpage, although only 1 appears at a time, hence all have the id="iFrame" I want to add to buttons (not the scrollbar) so the user can scroll down or up by pressing them, and I am trying to do this using javascript but so far I've been unsuccessful, I've read some posts here and tried those answers but so far no luck. I have also tried:

var newFrame = document.getElementsByTagName('iframe');
if(typeof newFrame !== 'undefined'){
    newFrame.contentWindow.scrollBy(0,250);

var myframe = document.getElementById(window.frameElement[0].id);
myframe.window.scrollBy(0,50);

到目前为止没有什么工作,任何人都可以让我知道我错过了什么或做错了什么?

nothing has worked out so far, could anyone let me know what am I missing or doing wrong? thank you!

推荐答案

尝试使用以下(未测试,但应该可以正常工作):

Try use the following (untested, but should work):

function scrollIFrame(name, x, y)
{
    var frame = document.getElementById(name);        
    frame.contentWindow.scrollTo(x, y);         
}

因此,您将使用:

scrollIFrame('iframe', 0, 250);

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

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