【发布时间】:2022-01-08 10:16:14
【问题描述】:
然后,如果您向下滚动到 B 部分的最开头
我预计 B 部分会变得粘滞,如果滚动,currentPercentage 状态将从 0% 开始逐渐增加。
当currentPercentage状态达到100%时,粘滞效果将不再适用于B区,此时您可以向下滚动至C区或页面底部。
有点像circle-ci主页
我认为我们需要解决一些问题
- 当滚动位置到达 B 部分时如何使 B 部分变得粘滞
- 到达B区时如何滚动和增加百分比
我尝试过用 css 和 javascript 的方式思考,仍然找不到解决方案
这几天我也对这个问题进行了大量研究,但仍然没有弄清楚。
我相信很多开发人员都想知道如何在他们的前端做这种效果,这对其他开发人员将来有更快的解决方案来处理这种用户体验是有益的,因为据我所知注意到没有人提到过这种 UX 处理。
如果有人能用一个例子回答这个问题,我将不胜感激,以供其他开发人员参考。
App.js
import "./styles.css";
import "bootstrap/dist/css/bootstrap.min.css";
import ProgressBar from "react-bootstrap/ProgressBar";
import { useState } from "react";
export default function App() {
const [currentProgress, setCurrentProgress] = useState(100);
return (
<div className="App">
<div className="sectionA">Section A</div>
<div className="sectionB">
Section B
<ProgressBar
now={currentProgress}
label={`${currentProgress}%`}
style={{ marginBottom: "16px" }}
/>
</div>
<div className="sectionC">Section C</div>
</div>
);
}
代码沙盒
https://codesandbox.io/s/mutable-fire-sz19b?file=/src/App.js:0-619
更新 1
我更新了代码,可以让容器 B 变粘,但不知道如何继续 UX
App.js
import "./styles.css";
import "bootstrap/dist/css/bootstrap.min.css";
import ProgressBar from "react-bootstrap/ProgressBar";
import { useState } from "react";
export default function App() {
const [currentProgress, setCurrentProgress] = useState(3);
return (
<div className="App">
<div className="sectionA">Section A</div>
<div style={{ position: "sticky", top: 0 }}>
<div className="sectionB">
Section B
<ProgressBar
now={currentProgress}
label={`${currentProgress}%`}
style={{ marginBottom: "16px" }}
/>
</div>
<div className="sectionC">Section C</div>
</div>
<div style={{ minHeight: "400vh", maxHeight: "calc(200vh - 466px)" }} />
</div>
);
}
代码沙盒
https://codesandbox.io/s/elated-hawking-n3rvw?file=/src/App.js:0-778
更新 2
添加库react-scroll-percentage 滚动更新
currentProgress
但是,当整个 containerB 暴露在视口中时,currentProgress 将开始更新,这不是我想要的

我想要的是当 SectionB 到达视口顶部时,滚动会让用户感到粘滞,然后 currentProgress 将从 0% 开始更新
App.js
import "./styles.css";
import "bootstrap/dist/css/bootstrap.min.css";
import React, { useState, useEffect } from "react";
import { useScrollPercentage } from "react-scroll-percentage";
import ProgressBar from "react-bootstrap/ProgressBar";
export default function App() {
const [currentProgress, setCurrentProgress] = useState(0);
const [ref, percentage] = useScrollPercentage({
threshold: 1.0
});
useEffect(() => {
setCurrentProgress(percentage * 100);
}, [percentage]);
return (
<div className="App">
<div className="sectionA">Section A</div>
<div style={{ position: "sticky", top: 0 }}>
<div className="sectionB" ref={ref}>
Section B
<ProgressBar
now={currentProgress}
label={`(${currentProgress})%`}
style={{ marginBottom: "16px" }}
/>
</div>
<div className="sectionC">Section C</div>
</div>
<div style={{ minHeight: "400vh", maxHeight: "calc(200vh - 466px)" }} />
</div>
);
}
代码沙盒
https://codesandbox.io/s/elated-hawking-n3rvw?file=/src/App.js:0-1029
【问题讨论】:
-
溢出-x:滚动;到 B 部分,通过使用 getBoundingClientRect() 属性和 onScroll 事件侦听器,您可以将粘性位置添加到 B 部分。
-
@drinos 你能提供一些例子吗?
-
或者你可以使用IntersectionObserver:它比滚动事件监听器性能要好得多。
-
@Terry 它与性能无关。我什至无法完成功能
-
我认为您误解了我的评论。这是对@drinos 关于使用滚动事件监听器的评论的回应。
标签: javascript html css reactjs