【问题标题】:scrollBy doesn't work in Firefox and OperascrollBy 在 Firefox 和 Opera 中不起作用
【发布时间】:2011-07-03 22:53:14
【问题描述】:

此 scrollBy 函数在 Internet Explorer 中有效,但被 Firefox 和 Opera 忽略。谁能帮忙解决这个问题?

function scrollLeft(s){
document.frames['my_iframe'].scrollBy(-s,0);
window.frames['my_iframe'].scrollBy(-s,0);
}

function scrollRight(s){
document.frames['my_iframe'].scrollBy(s,0);
window.frames['my_iframe'].scrollBy(s,0);
}

这是一个适用于 Internet Explorer 浏览器,但不适用于 Firefox 和 Opera 的示例:http://igproject.ru/iframe-scrolling/index.htm

【问题讨论】:

    标签: javascript internet-explorer firefox scroll opera


    【解决方案1】:

    在 Firefox 等中,您需要使用scrollTo() 而不是scrollBy()

    见:http://jsfiddle.net/4CkML/

    例子:

    window.scrollTo(50,50);
    

    如果域不匹配,则不能使用 scrollTo/By。您可以在此处看到产生了一个 javascript 错误:

    http://jsfiddle.net/3CbZc/

    访问属性“scrollTo”的权限被拒绝

    编辑 - 更新答案以合并来自长评论链的答案:

    var oIF = document.getElementById('my_iframe').contentWindow; oIF.scrollBy(s, 0);
    

    【讨论】:

    • 但我需要在设置iframe的页面中控制滚动功能。 scrollTo 不支持寻址我要滚动的元素。 (((
    • 我这样做了:document.frames['my_iframe'].scrollTo(s,0); window.frames['my_iframe'].scrollTo(s,0); 但仍然无法正常工作...
    • 如果您的 iframe 位于不同的域中,则无法滚动它。但这与在 IE 中工作不一致。你能发布 HTML/url 吗?我也无法在 IE 中使用它。
    • 我在上面看到了,那么 iframe 的 html 呢?这些函数是如何被调用的?
    • 这是一个在 Internet Explorer 浏览器中工作但在 Firefox 和 Opera 中不工作的示例:igproject.ru/iframe-scrolling/index.htm
    猜你喜欢
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    • 2013-01-16
    • 1970-01-01
    • 2012-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多