如何使用反应在滚动上创建粘性标题

bih*_*ris 5 javascript reactjs react-sticky next.js

所以我有一些表格,我试图按日期分类,标题像(今天,昨天,上周,......),我试图根据视口中的当前表格使它们具有粘性。我尝试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>
    );
}
Run Code Online (Sandbox Code Playgroud)

Rig*_*eka 17

按照下面列出的步骤,在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>
        </>
      );   
   }
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

笔记!

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

  • 这段代码是一个 React 反模式,原因有两个: 1. 这个 `useEffect` 将在 *每次组件重新渲染时 * 添加一个滚动侦听器。(你可以将 `console.log` 添加到回调中以查看它的触发) 2. 你不应该在 React 中操作 DOM - `classList.add`。正确的结构是在滚动回调中设置状态。 (3认同)

小智 14

你可以只使用position: stickytop: 0您的th。这是一个例子:https : //codepen.io/ipetriniith/pen/JjGeOKQ


小智 7

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

.header {
    position: sticky;
    top: 0px;
    z-index: 1;
}
Run Code Online (Sandbox Code Playgroud)