ブートストラップなしでscrollspyを実装したいと思います。
私は多くのコードをオンラインでチェックしましたが、それらはすべてjQueryによって実装されています。
Reactの力だけでscrollspyを実装する方法は?
const {useState, useEffect, useCallback, useRef} = React;
/**
*
* @param {Object} scrollParent [DOM node of scrollable element]
* @param {Array} _targetElements [Array of nodes to spy on]
*/
const spyScroll = (scrollParent, _targetElements) => {
if (!scrollParent) return false;
// create an Object with all children that has data-name attribute
const targetElements =
_targetElements ||
[...scrollParent.children].reduce(
(map, item) =>
item.dataset.name ? { [item.dataset.name]: item, ...map } : map,
{}
);
let bestMatch = {};
for (const sectionName in targetElements) {
if (Object.prototype.hasOwnProperty.call(targetElements, sectionName)) {
const domElm = targetElements[sectionName];
const delta = Math.abs(scrollParent.scrollTop - domElm.offsetTop); // check distance from top, takig scroll into account
if (!bestMatch.sectionName)
bestMatch = { sectionName, delta };
// check which delet is closest to "0"
if (delta < bestMatch.delta) {
bestMatch = { sectionName, delta };
}
}
}
// update state with best-fit section
return bestMatch.sectionName;
};
/**
* Given a parent element ref, this render-props function returns
* which of the parent's sections is currently scrolled into view
* @param {Object} sectionsWrapperRef [Scrollable parent node React ref Object]
*/
const CurrentScrolledSection = ({ sectionsWrapperRef, children }) => {
const [currentSection, setCurrentSection] = useState();
// adding the scroll event listener inside this component, and NOT the parent component, to prever re-rendering of the parent component when
// the scroll listener is fired and the state is updated, which causes noticable lag.
useEffect(() => {
const wrapperElm = sectionsWrapperRef.current;
if (wrapperElm) {
wrapperElm.addEventListener('scroll', e => setCurrentSection(spyScroll(e.target)));
setCurrentSection(spyScroll(wrapperElm));
}
// unbind
return () => wrapperElm.removeEventListener('scroll', throttledOnScroll)
}, []);
return children({ currentSection });
};
function App(){
const sectionsWrapperRef = useRef()
return <CurrentScrolledSection sectionsWrapperRef={sectionsWrapperRef}>
{({ currentSection }) => <div ref={sectionsWrapperRef}>
<section
data-name="section-a"
className={currentSection === "section-a" ? 'active' : ''}
>Section A</section>
<section
data-name="section-b"
className={currentSection === "section-b" ? 'active' : ''}
>Section B</section>
<section
data-name="section-c"
className={currentSection === "section-c" ? 'active' : ''}
>Section C</section>
</div>
}
</CurrentScrolledSection>
}
ReactDOM.render(
<App />,
document.getElementById('root')
)
html, body, #root{ height: 95%; overflow:hidden; }
#root > div{
padding-bottom:20em;
height: 100%;
overflow:auto;
box-sizing: border-box;
}
section{
height: 50vh;
border-bottom: 1px solid red;
}
section.active{ background:lightyellow; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.10.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.10.0/umd/react-dom.production.min.js"></script>
<div id='root'></div>
スクロールの方向はユーザーの意図を暗示するため、重要であるため、これは完全ではありません。
React-scrollspy
を使用できます。このgitリポジトリにアクセスしてください React-scrollspy 。
https://codesandbox.io/s/gallant-wing-ue2ks
npm i react-scrollspy
yarn add react-scrollspy
<section id="section-1">
<!-- Content goes here -->
</section>
<section id="section-2">
<!-- Content goes here -->
</section>
<!-- .... -->
<section id="section-n">
<!-- Content goes here -->
</section>
items
に追加します<Scrollspy items={ ['section-1', 'section-2', ..., 'section-n'] } />
このドキュメントを確認してください https://makotot.github.io/react-scrollspy/#section-
'react-scrollspy-nav'のnpmを使用して、そのドキュメントに従ってください。 リンク