next.js 如何使用react在滚动条上创建粘性标题

e5njpo68  于 2023-03-29  发布在  React
关注(0)|答案(5)|浏览(150)

所以我尝试按日期对表格进行分类,标题为(今天,昨天,上周,...),我试图根据视口中的当前表格使它们具有粘性。我尝试使用react-sticky库,特别是the stacked example,因为它似乎是我正在寻找的效果,但我无法重新创建它。
请问我是不是错过了图书馆的使用情况。

也欢迎没有库的解决方案

我一直在努力

export default function CustomizedTables() {
  const classes = useStyles();

  return (
    <StickyContainer>
      <Sticky topOffset={20}>
        {(props) => (
          <div className={reduceCSS.tableHistoryTitle_day}>Today</div>
        )}
      </Sticky>
      <TableContainer component={Paper} elevation={0}>
        <Table className={classes.table} aria-label="customized table">
          <TableBody>
            {rows.map((row) => (
              <StyledTableRow key={row.name} elevation={0}>
                <StyledTableCell align="left" className={classes.iconCell}>
                  <AssignmentReturnedSharpIcon className={classes.inputIcon} />
                </StyledTableCell>
                <StyledTableCell align="left">{row.calories}</StyledTableCell>
                <StyledTableCell component="th" scope="row">
                  {row.name}
                </StyledTableCell>
                <StyledTableCell align="right">{row.fat}</StyledTableCell>
                <StyledTableCell align="right">{row.carbs}</StyledTableCell>
                <StyledTableCell align="right">{row.protein}</StyledTableCell>
              </StyledTableRow>
            ))}
          </TableBody>
        </Table>
      </TableContainer>
    </StickyContainer>
  );
}
yqhsw0fo

yqhsw0fo1#

您可以在th中使用position: stickytop: 0https://codepen.io/ipetriniith/pen/JjGeOKQ

xam8gpfp

xam8gpfp2#

按照下面列出的步骤,对于***ReactJs***上的粘性头,
Header.js

const Header = () => {
    
        // Sticky Menu Area
        useEffect(() => {
            window.addEventListener('scroll', isSticky);
            return () => {
                window.removeEventListener('scroll', isSticky);
            };
        });
    
               
        /* Method that will fix header after a specific scrollable */
               const isSticky = (e) => {
                    const header = document.querySelector('.header-section');
                    const scrollTop = window.scrollY;
                    scrollTop >= 250 ? header.classList.add('is-sticky') : header.classList.remove('is-sticky');
                };
            return (
        <>
         <header className="header-section d-none d-xl-block">
              ..add header code
         </header>
        </>
      );   
   }

Header.css -自定义样式(为您的标题)

.is-sticky {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    z-index: 999;
    box-shadow: 0 2px 24px 0 rgb(0 0 0 / 15%);
    background-color: #ffffff !important;
    animation: 500ms ease-in-out 0s normal none 1 running fadeInDown;
    padding-top: 0px;
    padding-bottom: 0px;
}

注意!

已通过链接编译:https://codesandbox.io/s/sticky-header-dyews?file=/src/App.js

bqucvtff

bqucvtff3#

在react中,你需要通过调用useState函数来设置类。
您可以在useEffect中为此设置监听器,该监听器在组件呈现上运行一次。

import React, { useEffect, useState } from "react";

const Header = () => {
  const [sticky, setSticky] = useState("");

  // on render, set listener
  useEffect(() => {
    console.log("hello");
    window.addEventListener("scroll", isSticky);
    return () => {
      window.removeEventListener("scroll", isSticky);
    };
  }, []);

  const isSticky = () => {
    /* Method that will fix header after a specific scrollable */
    const scrollTop = window.scrollY;
    const stickyClass = scrollTop >= 250 ? "is-sticky" : "";
    setSticky(stickyClass);
    console.log(stickyClass);
  };

  const classes = `header-section d-none d-xl-block ${sticky}`;

  return (
    <>
      <header className={classes}>..add header code</header>
    </>
  );
};

export default Header;

Sandbox:https://codesandbox.io/s/sticky-header-forked-ybo6j9
大部分代码都引用了这个答案:https://stackoverflow.com/a/69944800/271932(有正确的CSS但没有react代码)

5hcedyr0

5hcedyr04#

在阅读了大量实现这一点的js代码之后,我实现了这样的...

.header {
    position: sticky;
    top: 0px;
    z-index: 1;
}
daolsyd0

daolsyd05#

position: sticky很酷。
Its compatibility is also ok
只需注意父元素的高度,大多数情况下,您可能需要将其与React.Fragment配合使用。

相关问题