window.onload未在IE中启动
window.onload is not firing in IE
这是我的代码:
var scrollOnLoad = function scrollOnLoad(selector, offset) {
window.onload = function() {
if ($(window).height() <= 800) {
var attempts = 100;
var interval = setInterval(function() {
var h = $(selector).height();
console.log("attempt #" + attempts + ", h=" + h);
if (h > 0 || !attempts--) {
$("body,html").animate({scrollTop : h + offset}, '1000');
clearInterval(interval);
}
}, 50)
}
}
}
以下是它的缩放方式:
scrollOnLoad(".content-wrapper", 50);
我注意到window.onload事件没有在IE中启动(控制台没有记录任何内容)-有什么想法吗?提前感谢!
您可以使用document.readyState
属性:
function scrollOnLoad(selector, offset) {
if(document.readyState === 'complete')
x();
else
document.addEventListener('readystatechange', function(){
if(document.readyState === 'complete')
x();
});
function x(){
if ($(window).height() <= 800) {
var attempts = 100;
var interval = setInterval(function() {
var h = $(selector).height();
console.log("attempt #" + attempts + ", h=" + h);
if (h > 0 || !attempts--) {
$("body,html").animate({scrollTop : h + offset}, '1000');
clearInterval(interval);
}
}, 50)
}
}
}
如注释中所述,您的函数scrollOnLoad将在窗口加载后执行。
现在,在更好的场景中,这些事件应该添加到全局范围中。
所以它应该像一样
window.onload = function() {
if(someConditionIsSatisfied){
if ($(window).height() <= 800) {
var attempts = 100;
var interval = setInterval(function() {
var h = $(selector).height();
console.log("attempt #" + attempts + ", h=" + h);
if (h > 0 || !attempts--) {
$("body,html").animate({scrollTop : h + offset}, '1000');
clearInterval(interval);
}
}, 50)
}
}
}
相关文章:
- 从IE中的不同元素调用时,文件输入控件未正确启动
- IE 11未在SVG地图上启动Mouseenter或Mouseleave事件
- 是否可以从在Firefox或IE的网页中运行的Javascript启动Windows进程
- IE窗口失去了对任何XMLHttpRequest启动Angular应用程序的关注
- jQuery.done函数未在IE中启动
- window.onload未在IE中启动
- Mouseover,Mouseout事件未在IE中启动
- jwPlayer sendEvent 未在 IE 中启动
- 当正文调整大小时,IE在调整大小时启动
- 使用javascript在html中启动和停止嵌入音乐仅适用于IE
- IE 10 keyup/keydown未使用jQuery 1.10.1启动
- onChange不在Safari的下拉列表中启动,适用于IE、FF、Chrome和;歌剧
- IE 11企业模式在我们的应用程序中自动启动
- firefox上的Javascript函数问题没有通过jquery键盘输入启动计数器(在Chrome和IE上工作)
- 如何只等待文件.readyState在IE中,而在所有其他浏览器中立即启动
- 如何访问IE XHTML DOM+JS引擎,而无需启动浏览器本身
- 不能在Selenium中启动带有WebDriverJs的ie浏览器
- jQuery flash对象点击事件未在IE或Chrome中启动
- $(document).ready在IE中只启动一次,但在Firefox中有效
- 卸载activex后,以管理员模式重新启动IE浏览器