使用react-sticky制作粘性标题

时间:2019-03-16 15:36:22

标签: javascript css reactjs react-sticky

我正在尝试使用react-sticky软件包制作粘性标头,但是我的标头一直滚动到视线之外。这是软件包:https://www.npmjs.com/package/react-sticky 我不确定我是否正确使用了StickyContainer或Sticky组件。实际上,我对应该传递给Sticky容器的“样式”道具感到有些困惑。

如果有人可以帮助,将不胜感激。谢谢!

这是App.js的代码:

import React, { Component } from 'react';
import './App.css';
import Header from './components/Header';
import Footer from './components/Footer';
import HomePage from './components/pages/HomePage';
import OurWork from './components/pages/OurWork';
import ContactUs from './components/pages/ContactUs';
import { BreakpointProvider } from 'react-socks';
import { StickyContainer, Sticky } from "react-sticky";
import { setDefaultBreakpoints } from 'react-socks';

setDefaultBreakpoints([
  { small: 0 },
  { medium: 700 }
]);

class App extends Component {
  pageStyle = {
    display: 'flex',
    flexDirection: 'column'
  }

  render() {
    return (
      <BreakpointProvider>
        <StickyContainer>
          <div className="App">
            <Sticky>
              {({style}) => <Header style={style}/>}
            </Sticky>
            <div className="page" style={this.pageStyle}>
                <HomePage />
              <OurWork />
              <ContactUs />
            </div>
          <Footer />
        </div>
        </StickyContainer>
      </BreakpointProvider>
    );
  }
}

export default App;

这是Header组件:

import React, { Component } from 'react';
import Logo from './Logo'
import NavBar from './NavBar';
import logo from '../images/transparent.png';

class Header extends Component {
    headerStyle = {
        height: 100,
        margin: 20,
        display: 'flex',
        justifyContent: 'space-between',
        zIndex: 10
    };

    render() {
        return (
            <div className="header" style={this.headerStyle}>
                <Logo logo={logo}/>
                <NavBar />
            </div>
        );
    }

};

export default Header;

1 个答案:

答案 0 :(得分:1)

粘性标头不需要外部库,请检查此资源React Table Sticky Header without external library

演示

enter image description here

窍门就像

1。分割标题和数据部分

  1. 两者都使用固定宽度

  2. 用div包裹数据容器,给该容器div固定高度, 允许

    .container {   overflox-y:滚动;   高度:300像素; }

相关问题