我正在使用纯javascript处理fadein和fadeout函数,这里是代码:
<script>
function fadeIn(el, ms) {
var opacity = 1,
interval = 50,
gap = interval / ms;
window.setInterval(function{
opacity -= gap;
el.style.opacity = opacity;
if(opacity <= 0) {
window.clearInterval(fading);
el.style.display = 'none';
}
}, interval);
}
function fadeOut(el, ms){
var opacity = 0,
interval = 50,
gap = interval / ms;
el.style.display = 'block';
el.style.opacity = opacity;
window.setInterval(function{
opacity += gap;
el.style.opacity = opacity;
if(opacity >= 1) {
window.clearInterval(fading);
}
}, interval);
}
</script>
在正文部分,代码是:
<input type="button" onclick="fadeOut(this, 50);" value="hi" />
当页面加载时,会立即显示错误“错误意外令牌(”:
window.setInterval(function{
当我尝试单击输入按钮时,我收到错误:“无法找到变量fadeIn”
答案 0 :(得分:0)
您缺少函数声明的括号:
setTimeout(function() {
});
请注意()
关键字后添加function
。您需要对setTimeout
和fadeIn
fadeOut
个函数执行此操作
答案 1 :(得分:0)
这是正确的代码
你需要在window.setInterval(function{
<script>
function fadeIn(el, ms) {
var opacity = 1,
interval = 50,
gap = interval / ms;
window.setInterval(function(){
opacity -= gap;
el.style.opacity = opacity;
if(opacity <= 0) {
window.clearInterval(fading);
el.style.display = 'none';
}
}, interval);
}
function fadeOut(el, ms){
var opacity = 0,
interval = 50,
gap = interval / ms;
el.style.display = 'block';
el.style.opacity = opacity;
window.setInterval(function(){
opacity += gap;
el.style.opacity = opacity;
if(opacity >= 1) {
window.clearInterval(fading);
}
}, interval);
}
</script>