如何使用React实现scrollspy [英] How to implement a scrollspy with React

查看:64
本文介绍了如何使用React实现scrollspy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个没有引导程序的 scrollspy .

I want to implement a scrollspy without the bootstrap.

我已经在线检查了很多代码,它们都是由 jQuery 实现的.

I have checked a lot of code online, all of them are implemented by jQuery.

如何仅使用React的力量实现 scrollspy ?

How to implement the scrollspy only with the power of React?

推荐答案

我制作了一个React 包装器(用于实时Codepen示例


I've made a React Wrapper (used with Render-Props):

⚡ Live Codepen Example


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>

这并不完美,因为滚动方向确实很重要,因为它暗示了用户的意图.

It's not perfect because scroll direction does matter, since it implies on the intentions of the user.

这篇关于如何使用React实现scrollspy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆