window.top.document.body.scrollTop无法在Chrome或Firefox工作

问题描述 投票:9回答:5

我有下面的代码将打开一个模态窗口。这在IE 8,但无法在Chrome或FF。我是新来的跨浏览器功能的世界。

function ShowModal(WindowID,FramesetID)
{
    window.onscroll = function () { window.top.document.getElementById(WindowID).style.top = window.top.document.body.scrollTop; };
    window.top.document.getElementById(WindowID).style.display = "block";
    window.top.document.getElementById(WindowID).style.top = document.body.scrollTop;

    widthv=parseInt(parseInt(screen.width)/1.50);
    heightv=parseInt(parseInt(screen.height)/1.50); 

    window.top.document.getElementById(FramesetID).style.width=widthv;
    window.top.document.getElementById(FramesetID).style.height=heightv;
}

使这个代码Chrome浏览器和FF兼容任何人都可以帮忙吗? 我试图改变window.topwindow.parent但没有运气另外,多个浏览器编码时(我已经浏览过,但并没有完全找到跨浏览器兼容的任何一套规则)的任何规则要记住?

更新: 问题是,在IE浏览器,这个模态窗口出现在大约一半的屏幕尺寸。在FF和Chrome,出现模态窗口约一元硬币的大小。

javascript firefox google-chrome
5个回答
-1
投票

我建议jQuery的像chrispanda已建议。 jQuery的有一个内置的滚动事件,其余的可以写在短短的几行操纵HTML / CSS。

http://api.jquery.com/


27
投票

根据您的浏览器当前的渲染模式,您可能需要代替document.documentElement.scrollTop的(且同样document.body.scrollTop)使用scrollLeft

有一个关于在an Evolt article by Peter-Paul Koch这个问题的一些好的背景(的quirksmode.org名望),但它从2002年的,现在有点过时。

正如其他人在这里所提出的建议,要解决这类问题在2011年的最简单的方法是只使用现有的JavaScript框架。 jQuery颇为流行(尤其是在StackOverflow的用户),但there are many others as well


17
投票

另一种解决方案:

(document.documentElement.scrollTop || document.body.scrollTop)

7
投票

您可以使用:

window.pageYOffset


1
投票

如果你没有看到不必要的第三方库的需要,你可以尝试:

var scrollTop = (document.documentElement || document.body.parentNode || document.body).scrollTop;

这是哈克,但它仍然比JQuery的更好。

© www.soinside.com 2019 - 2024. All rights reserved.