嗨,朋友,我想淡入淡出当我点击另一个股利,为此,我使用下面的代码。 Code1工作正常,但我需要使用Code2。Pure JavaScript淡入功能
我知道有jQuery的,但我需要做到这一点在JavaScript
你能指导我,什么样的错误,我在做或者我需要改变什么......
代码1 - - 工作正常
function starter() { fin(); }
function fin()
{
for (i = 0; i <= 1; i += 0.01)
{
i=Math.round(i*100)/100;
setTimeout("seto(" + i + ")", i * 1000);
}
}
function seto(opa)
{
var ele = document.getElementById("div1");
ele.style.opacity = opa;
}
代码2 ---不工作
function starter()
{
var ele = document.getElementById("div1");
fin(ele);
}
function fin(ele)
{
for (i = 0; i <= 1; i += 0.01)
{
i=Math.round(i*100)/100;
setTimeout("seto(" + ele + "," + i + ")", i * 1000);
}
}
function seto(ele,opa)
{
ele.style.opacity = opa;
}
这里是一个小的宝石,将帮你http://youmightnotneedjquery.com/ – series0ne
任何控制台错误? – laaposto
你应该使用'setInterval'和'clearInterval'来代替。看看http://stackoverflow.com/questions/2695462/fade-html-element-with-raw-javascript – dave