【发布时间】:2023-01-09 11:40:06
【问题描述】:
我有一个p 标签列表,我想循环浏览这个列表,方法是淡入一个p 标签,然后淡出,然后在替换它后再次淡入。
这是 jQuery 中的代码笔:https://codepen.io/motion333/pen/EBBGVM
我正在尝试通过以下方式在 React 中执行此操作:
useEffect(() => {
(function() {
var quotes = document.getElementsByClassName('tagline-text');
var quoteIndex = -1;
function showNextQuote() {
++quoteIndex;
document.querySelectorAll(".tagline-text")[quoteIndex % quotes.length].fadeIn(1000).delay(1000).fadeOut(1000, showNextQuote);
}
showNextQuote();
})();
}, []);
这是容器:
<div className="tagline h-100 d-flex flex-column align-items-center justify-content-center">
<p className="tagline-text">Your Business</p>
<p className="tagline-text">Your Brand</p>
<p className="tagline-text">Your Content</p>
<p className="tagline-text">Your Portfolio</p>
<p className="tagline-text">You.</p>
</div>
但它给了我这个错误:
Uncaught TypeError: document.querySelectorAll(...)[(quoteIndex % quotes.length)].fadeIn is not a function
【问题讨论】:
-
fadeIn是一个 jQuery 方法,但您在这里处理的不是 jQuery 对象,而是普通的 HTMLElement 对象。 -
是的,我已经意识到了。 JS 中淡入淡出方法的实际等价物是什么?
-
没有本地等价物。您要么必须自己实施它们,要么去检查 React 可能明确提供哪些替代方案(?)。
-
无法做到:(
标签: javascript jquery css reactjs