Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用react js根据滚动位置添加额外的类属性_Javascript_Reactjs - Fatal编程技术网

Javascript 如何使用react js根据滚动位置添加额外的类属性

Javascript 如何使用react js根据滚动位置添加额外的类属性,javascript,reactjs,Javascript,Reactjs,在react js中,我想根据滚动位置添加一个名为“sticky”的类属性。我的意思是,如果滚动位置达到[div class=“btnGrp”],那么我需要添加class属性“sticky”,否则我需要删除该class属性。如何在react js中解决这个问题。这是我尝试过的示例代码 const HideButton =()=>{ useEffect()=>{ navbar=document.getElementById('navbar'); sticky

在react js中,我想根据滚动位置添加一个名为“sticky”的类属性。我的意思是,如果滚动位置达到[div class=“btnGrp”],那么我需要添加class属性“sticky”,否则我需要删除该class属性。如何在react js中解决这个问题。这是我尝试过的示例代码


const HideButton =()=>{

    useEffect()=>{
    navbar=document.getElementById('navbar');
    sticky=navbar.offsetTop;
    window.onscroll = function(){stickybar()};
}

const stickybar =()=>{
    pageYOffset =window.pageYOffset;
        if(window.pageYOffset <= sticky){
        navbar.classList.add("sticky");
    }else{
        navbar.classList.remove("sticky");
    }
}

    return (
        <>
            <p>What is Lorem Ipsum?
            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
            <p>It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
            .
            .
            .
            .
            .
            .


            <p>Why do we use it?
            It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English.</p>
            <p>Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
            <div class="btnGrp">
                <button>Reset</button>
                <button>Save</button>
            </div>
        </>
    )
}

常量HideButton=()=>{
useEffect()=>{
navbar=document.getElementById('navbar');
粘性=导航条偏移量;
window.onscroll=function(){stickybar()};
}
常量粘滞条=()=>{
pageYOffset=window.pageYOffset;

如果(window.pageYOffset考虑渲染层次结构

  • 重新绘制:更改颜色、不透明度、可见性等
  • 回流:更改宽度、dom结构、字体大小等
  • Rerender:更改通过react组件树传播的状态
  • 每一个都比上一个更需要计算,因此需要非常顺利地进行的事情,如果可能的话,您希望保持较低的级别。我的意思是:您不希望使用React来跟踪这一点,您希望使用vanilla JS触发更改,然后在单独/之后的状态中反映更改

    那就这样做吧

    const stickybar = () => {
      const navBar = useRef()
      
      useEffect(() => {
         navBar.current?.addEventListener("scroll", (e) => {
         //if (e.target.scrollTop is greater than the height of the screen
         // then add the stick class to the navBar
         //optionally set the state accordingly, if other aspects of the component need to know about this change
      }, [])
    
      return (<ref = {navBar}>
                <p>What is Lorem Ipsum?
                Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
                <p>It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
                .
                .
                .
                .
                .
                .
    
    
                <p>Why do we use it?
                It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English.</p>
                <p>Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
                <div class="btnGrp">
                    <button>Reset</button>
                    <button>Save</button>
                </div>
            </>)
    
    );
    
     
    
    const stickybar=()=>{
    const navBar=useRef()
    useffect(()=>{
    navBar.current?.addEventListener(“滚动条”,(e)=>{
    //如果(如target.scrollTop)大于屏幕高度
    //然后将stick类添加到导航栏
    //如果组件的其他方面需要了解此更改,可以选择相应地设置状态
    }, [])
    返回(
    什么是Lorem Ipsum?
    Lorem Ipsum仅仅是印刷和排版行业的虚拟文本。自16世纪以来,Lorem Ipsum一直是该行业的标准虚拟文本,当时一位不知名的印刷商拿起一个打印工具,将其拼凑成一本字体样本书

    它不仅存活了五个世纪,而且还跨越到电子排版,基本上保持不变。它在20世纪60年代随着包含Lorem Ipsum段落的Letraset表单的发布而流行,最近随着Aldus PageMaker等桌面出版软件包括Lorem Ipsum版本

    . . . . . . 我们为什么要用它? 读者在查看页面布局时会被页面的可读内容分散注意力,这是一个长期存在的事实。使用Lorem Ipsum的要点是,它的字母分布或多或少是正态的,而不是使用“此处内容,此处内容”,使其看起来像可读的英语

    许多桌面出版软件包和网页编辑器现在使用Lorem Ipsum作为默认的模型文本,搜索“Lorem Ipsum”将发现许多尚处于起步阶段的网站。多年来,各种版本不断演变,有时是偶然的,有时是故意的(注入幽默等)

    重置 拯救 ) );
    这是否回答了您的问题?