[英]Is there a better way to check if the bottom of a div is reached
我制作了一个条款和条件应用程序,如果用户已经到达div的底部,它会启用“我同意”按钮。
function App() {
const [content, setContent] = useState("");
const [status, setStatus] = useState(true);
const fetchContent = () => {
fetch('https://jaspervdj.be/lorem-markdownum/markdown.txt')
.then(response => response.text())
.then((text) => setContent(text))
}
const handleScroll = (e) => {
const bottom = e.target.scrollHeight - e.target.scrollTop === e.target.clientHeight;
return bottom && setStatus(false);
}
useEffect(() => fetchContent(), []);
return (
<div className="App">
<Document
title="Terms and Conditions"
content={content}
onScroll={handleScroll}
status={status} />
</div>
);
}
这是文档组件:
const Document = ({ title, content, onScroll, status }) => {
return (
<div>
<h1 className="title">{title}</h1>
<div className="content" onScroll={onScroll}>{content}</div>
<button className="button" type="button" disabled={status}>I Agree</button>
</div>
)
}
它按预期工作,但有没有更好的方法来实现逻辑?
使用 jQuery windows 事件可能是这样的,
$("html, body").animate({ scrollTop: $("#myID").scrollTop() }, 1000);
请把它放在第一位。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.