gpt4 book ai didi

javascript - fadeIn 和 fadeOut 功能不起作用

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

我正在使用纯 javascript 处理淡入淡出函数,这里是代码:

(function() {
var fx = {
easing: {
linear: function(progress) {
return progress;
},
quadratic: function(progress) {
return Math.pow(progress, 2);
},
swing: function(progress) {
return 0.5 - Math.cos(progress * Math.PI) / 2;
},
circ: function(progress) {
return 1 - Math.sin(Math.acos(progress));
},
back: function(progress, x) {
return Math.pow(progress, 2) * ((x + 1) * progress - x);
},
bounce: function(progress) {
for (var a = 0, b = 1, result; 1; a += b, b /= 2) {
if (progress >= (7 - 4 * a) / 11) {
return -Math.pow((11 - 6 * a - 11 * progress) / 4, 2) + Math.pow(b, 2);
}
}
},
elastic: function(progress, x) {
return Math.pow(2, 10 * (progress - 1)) * Math.cos(20 * Math.PI * x / 3 * progress);
}
},
animate: function(options) {
var start = new Date;
var id = setInterval(function() {
var timePassed = new Date - start;
var progress = timePassed / options.duration;
if (progress > 1) {
progress = 1;
}
options.progress = progress;
var delta = options.delta(progress);
options.step(delta);
if (progress == 1) {
clearInterval(id);
options.complete();
}
}, options.delay || 10);
},
fadeOut: function(element, options) {
var to = 1;
this.animate({
duration: options.duration,
delta: function(progress) {
progress = this.progress;
return fx.easing.swing(progress);
},
complete: options.complete,
step: function(delta) {
element.style.opacity = to - delta;
}
});
},
fadeIn: function(element, options) {
var to = 0;
this.animate({
duration: options.duration,
delta: function(progress) {
progress = this.progress;
return fx.easing.swing(progress);
},
complete: options.complete,
step: function(delta) {
element.style.opacity = to + delta;
}
});
}
};
window.fx = fx;
})()

我正在使用以下代码来激活该功能:

document.getElementById('in').addEventListener('click', function() {
FX.fadeIn(document.getElementById('type'), {
duration: 2000,
});
}, false);

但是当我加载页面时,我收到激活码错误。

谁知道我做错了什么?

非常感谢。

最佳答案

通过一些调整,您的代码应该可以正常工作...

  1. 作为Richard Dalton注意,用 fx 而不是 FX 调用对象。
  2. 为所有选项添加默认值,以防未提供选项(您的示例代码抛出错误,因为 options.complete 未定义)。示例:

    this.animate({
    duration: options.duration || 1000,
    ...
    complete: options.complete || function() { },
    ...
    });
  3. 在设置动画之前调整样式(假设使用 display: none 而不是 opacity: 0 隐藏了一个元素)...示例(对于 淡入):

    element.style.opacity = 0;
    element.style.visibility = "visible";
    element.style.display = "block";
  4. 检查是否需要一个 Action (例如调用 fadeIn 尽管元素已经可见):

    isVisible: function(element) {
    return element.style.display !== "none" &&
    element.style.visibility !== "hidden" &&
    element.style.opacity !== "0";
    }

    // In fadeIn:
    if (!this.isVisible(element)) {
    ...
    }

下面是更正后的代码示例:JSFiddle

当然可以进一步调整它,处理一些极端情况(例如,如果在元素当前正在淡出时调用 fadeIn 会发生什么?),我很确定它不适用于所有浏览器(旧 IE)...但我希望我指出了正确的方向。 :)

关于javascript - fadeIn 和 fadeOut 功能不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19272429/

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