Javascript IE7/IE8需要一些时间才能返回正确的滚动位置

Javascript IE7/IE8需要一些时间才能返回正确的滚动位置,javascript,internet-explorer,scroll,restore,scrolltop,Javascript,Internet Explorer,Scroll,Restore,Scrolltop,为了显示对话框,我需要知道窗口的当前滚动位置。这是在jQuery的$(document.ready块中完成的 然而,当必须记住滚动位置时,Internet Explorer会破坏一些东西 您可以在IE中打开下面的HTML代码,向下滚动一点,然后点击reload(很抱歉,我没有创建小提琴–因为滚动在那里不起作用) $(文档).ready(函数(){ 警报(document.body.scrollTop); 警报(document.body.scrollTop); }); 第一个警报显示0,第二

为了显示对话框,我需要知道窗口的当前滚动位置。这是在jQuery的
$(document.ready
块中完成的

然而,当必须记住滚动位置时,Internet Explorer会破坏一些东西

您可以在IE中打开下面的HTML代码,向下滚动一点,然后点击reload(很抱歉,我没有创建小提琴–因为滚动在那里不起作用)


$(文档).ready(函数(){
警报(document.body.scrollTop);
警报(document.body.scrollTop);
});
第一个
警报显示
0
,第二个显示正确的数字。如果我使用
setTimeout
并等待(在我的情况下)20秒,它也可以在没有第一个警报的情况下工作-在其他情况下,这可能需要另一段时间,因此不是解决方案

我认为问题在于IE首先滚动顶部,做一些事情,然后,在一段时间后,恢复以前的滚动位置(可能它必须先渲染一些东西,以确保这个滚动位置仍然存在)


有没有合适的方法可以让浏览器一次滚动到正确的位置?

有几个属性可以让页面滚动,每个浏览器都有不同的属性。 以下是我编写的一个函数,用于接收正确的滚动:

function getScroll(type) { type = type || 'top'; var result = 0; var scroll = 0; if (type === 'top') { if (ISQ.Http.browser.app !== 'ie' || ISQ.Http.browser.isIE9) scroll = window.pageYOffset; if (ISQ.Http.browser.app !== 'ie') scroll = returnMaxScroll(scroll, window.scrollY); scroll = returnMaxScroll(scroll, document.body.scrollTop); scroll = returnMaxScroll(scroll, document.documentElement.scrollTop); scroll = returnMaxScroll(scroll, document.body.parentNode.scrollTop); } else if (type === 'left') { if (ISQ.Http.browser.app !== 'ie' || ISQ.Http.browser.isIE9) scroll = window.pageXOffset; if (ISQ.Http.browser.app !== 'ie') scroll = returnMaxScroll(scroll, window.scrollX); scroll = returnMaxScroll(scroll, document.body.scrollLeft); scroll = returnMaxScroll(scroll, document.documentElement.scrollLeft); scroll = returnMaxScroll(scroll, document.body.parentNode.scrollLeft); } else if (type === 'height') { switch (ISQ.Http.browser.app) { case "chrome": case "ff": case "opera": case "safari": // iframe if (window.top === window) scroll = document.documentElement.scrollHeight; // top window else scroll = document.body.scrollHeight; break; default: scroll = returnMaxScroll(scroll, document.body.scrollHeight); scroll = returnMaxScroll(scroll, document.documentElement.scrollHeight); scroll = returnMaxScroll(scroll, document.body.parentNode.scrollHeight); break; } } else if (type === 'width') { scroll = returnMaxScroll(scroll, document.body.scrollWidth); scroll = returnMaxScroll(scroll, document.documentElement.scrollWidth); scroll = returnMaxScroll(scroll, document.body.parentNode.scrollWidth); } return scroll; } function returnMaxScroll(value1, value2) { if (value1 > value2) return value1; return value2; } 函数getScroll(类型) { 类型=类型| |“顶部”; var结果=0; var=0; 如果(类型=='top') { 如果(ISQ.Http.browser.app!='ie'| | ISQ.Http.browser.isIE9) 滚动=window.pageYOffset; 如果(ISQ.Http.browser.app!=“ie”) 滚动=返回最大滚动(滚动,窗口。滚动); scroll=returnMaxScroll(滚动,document.body.scrollTop); scroll=returnMaxScroll(滚动,document.documentElement.scrollTop); scroll=returnMaxScroll(滚动,document.body.parentNode.scrollTop); } else if(type==='left') { 如果(ISQ.Http.browser.app!='ie'| | ISQ.Http.browser.isIE9) 滚动=window.pageXOffset; 如果(ISQ.Http.browser.app!=“ie”) scroll=returnMaxScroll(滚动,window.scrollX); scroll=returnMaxScroll(滚动,document.body.scrollLeft); scroll=returnMaxScroll(滚动,document.documentElement.scrollLeft); scroll=returnMaxScroll(滚动,document.body.parentNode.scrollLeft); } else if(类型==‘高度’) { 交换机(ISQ.Http.browser.app) { 案例“铬”: 案例“ff”: “歌剧”一案: “狩猎”一案: //iframe 如果(window.top==窗口) 滚动=document.documentElement.scrollHeight; //顶窗 其他的 滚动=document.body.scrollHeight; 打破 违约: scroll=returnMaxScroll(滚动,document.body.scrollHeight); scroll=returnMaxScroll(滚动,document.documentElement.scrollHeight); scroll=returnMaxScroll(滚动,document.body.parentNode.scrollHeight); 打破 } } else if(类型==‘宽度’) { scroll=returnMaxScroll(滚动,document.body.scrollWidth); scroll=returnMaxScroll(滚动,document.documentElement.scrollWidth); scroll=returnMaxScroll(滚动,document.body.parentNode.scrollWidth); } 返回滚动条; } 函数returnMaxScroll(值1、值2) { 如果(value1>value2)返回值1; 返回值2; } 仅供参考:您可能需要实现以下布尔值: -ISQ.Http.browser.app!='ie' -ISQ.Http.browser.isIE9
我希望它能帮到你:)

你能排除$().ready()是造成延迟的原因吗?不,我不认为它是造成延迟的原因,而是调用/挂接得太早了:即在
ready()时似乎还没有完全准备好
调用回调。您的web应用程序的性能是否非常慢?我认为这不是问题所在-请尝试上面的示例(性能不应该很慢,对吧?)。非常感谢!我会尽快试一试的!ie8的测试是什么? function getScroll(type) { type = type || 'top'; var result = 0; var scroll = 0; if (type === 'top') { if (ISQ.Http.browser.app !== 'ie' || ISQ.Http.browser.isIE9) scroll = window.pageYOffset; if (ISQ.Http.browser.app !== 'ie') scroll = returnMaxScroll(scroll, window.scrollY); scroll = returnMaxScroll(scroll, document.body.scrollTop); scroll = returnMaxScroll(scroll, document.documentElement.scrollTop); scroll = returnMaxScroll(scroll, document.body.parentNode.scrollTop); } else if (type === 'left') { if (ISQ.Http.browser.app !== 'ie' || ISQ.Http.browser.isIE9) scroll = window.pageXOffset; if (ISQ.Http.browser.app !== 'ie') scroll = returnMaxScroll(scroll, window.scrollX); scroll = returnMaxScroll(scroll, document.body.scrollLeft); scroll = returnMaxScroll(scroll, document.documentElement.scrollLeft); scroll = returnMaxScroll(scroll, document.body.parentNode.scrollLeft); } else if (type === 'height') { switch (ISQ.Http.browser.app) { case "chrome": case "ff": case "opera": case "safari": // iframe if (window.top === window) scroll = document.documentElement.scrollHeight; // top window else scroll = document.body.scrollHeight; break; default: scroll = returnMaxScroll(scroll, document.body.scrollHeight); scroll = returnMaxScroll(scroll, document.documentElement.scrollHeight); scroll = returnMaxScroll(scroll, document.body.parentNode.scrollHeight); break; } } else if (type === 'width') { scroll = returnMaxScroll(scroll, document.body.scrollWidth); scroll = returnMaxScroll(scroll, document.documentElement.scrollWidth); scroll = returnMaxScroll(scroll, document.body.parentNode.scrollWidth); } return scroll; } function returnMaxScroll(value1, value2) { if (value1 > value2) return value1; return value2; } FYI: you may need to implement these booleans: - ISQ.Http.browser.app !== 'ie' - ISQ.Http.browser.isIE9