gpt4 book ai didi

javascript - IE 引发无效参数错误,firefox 没有

转载 作者:数据小太阳 更新时间:2023-10-29 05:36:58 26 4
gpt4 key购买 nike

当我想炫耀 JavaScript 的强大功能时,我正在帮助一个 friend 学习 HTML、CSS 等。我们制作了一些包含云图像的 DIV,然后将它们移动到整个浏览器(从右到左,然后当它离开页面时它返回到窗口 innerWidth 的起点)所以它看起来像天空 - 非常简单脚本。现在在 Firefox 中一切正常,但稍后在 IE 中查看时,我注意到一个错误。这不是真正的问题,因为脚本可以正常工作,但我想知道它为什么会发生以及如何阻止它!

这是我在 window.onload 事件上使用不显眼的加载器调用的脚本

    function moveCloud(cloudID, TimeOut, CloudWidth, thisLeft){

var elem = document.getElementById(cloudID);
var xpos = parseInt(elem.style.left);

if(xpos > -CloudWidth){
xpos--;
}else{
xpos = window.innerWidth;
}

elem.style.left = xpos + "px";
movement = setTimeout("moveCloud('" + cloudID + "'," + TimeOut + "," + CloudWidth + "," + thisLeft+ ")",TimeOut);
}

我认为这可能是由于将一个整数连接到一个字符串,但我认为 JavaScript 处理了这个转换?有什么想法吗?

这是错误

Line: 38 Error: Invalid argument.

*更新***

这就是我调用函数的方式,首先是这个

function StartCloud(cloudID, TimeOut, CloudWidth, thisLeft, thisTop){


var elem = document.getElementById(cloudID);
if(elem){ // make sure item exists then position it via the JavaScript not the CSS
elem.style.position = "absolute";
elem.style.left = parseInt(thisLeft) + "px";
elem.style.top = parseInt(thisTop) + "px";
movement = setTimeout("moveCloud('" + cloudID + "'," + TimeOut + "," + CloudWidth + "," + thisLeft+ ")",TimeOut); // start the animation function
}
}

这是我的决定 - 有 4 朵云 DIVS

window.onload = function (){
//unobtrussive event handler
StartCloud('cloud1', 60, 717, 450, 30);
StartCloud('cloud2', 35, 349, 950, 230);
StartCloud('cloud3', 30, 335, 300, 260);
StartCloud('cloud4', 15, 290, 600, 450);
}

最佳答案

呃! IE 不支持 Window.innerWidth,所以我不得不使用 document.documentElement.clientWidth 编写条件

关于javascript - IE 引发无效参数错误,firefox 没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5686452/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com