如何使粘性页脚反应?

Pol*_*off 8 css footer jsx reactjs react-jsx

我已经制作了一个粘性页脚更高级别的组件,它将自身内部的其他组件包装起来:

Footer.js

//this is a higher-order component that wraps other components placing them in footer

var style = {
    backgroundColor: "#F8F8F8",
    borderTop: "1px solid #E7E7E7",
    textAlign: "center",
    padding: "20px",
    position: "fixed",
    left: "0",
    bottom: "0",
    height: "60px",
    width: "100%",
};

const Footer = React.createClass({
    render: function() {
        return (
            <div style={style}>
                {this.props.children}
            </div>
        );
    }
});

export default Footer;
Run Code Online (Sandbox Code Playgroud)

用法:

<Footer><Button>test</Button></Footer>
Run Code Online (Sandbox Code Playgroud)

但它隐藏了页面的内容:

隐藏内容页脚

这看起来像是一个常见的问题,所以我搜索了一下并发现了这个问题,建议将FlexBox用于粘性页脚.但是在这个演示中,页脚位于页面的最底部,而我需要页脚始终显示在页面上,内容在上面的区域内滚动(如在SO聊天中).除此之外,还建议使用自定义样式表规则更改所有其他组件.是否有可能使用仅仅设置页脚组件的样式来实现我需要的代码,以便代码保持模块化?

jac*_*ood 31

这是一个想法(沙箱示例链接).

在页脚组件中包含一个幻像div,表示其他dom元素将遵循的页脚位置(即通过不存在影响页面流position: 'fixed';).

var style = {
    backgroundColor: "#F8F8F8",
    borderTop: "1px solid #E7E7E7",
    textAlign: "center",
    padding: "20px",
    position: "fixed",
    left: "0",
    bottom: "0",
    height: "60px",
    width: "100%",
}

var phantom = {
  display: 'block',
  padding: '20px',
  height: '60px',
  width: '100%',
}

function Footer({ children }) {
    return (
        <div>
            <div style={phantom} />
            <div style={style}>
                { children }
            </div>
        </div>
    )
}

export default Footer
Run Code Online (Sandbox Code Playgroud)

  • 即使在 2020 年,这也有效,但如果页脚上方的内容很小,它并不能解决问题。如果页眉+正文只有 50 像素,页脚将不会粘在底部。 (2认同)

Nic*_*lva 15

更简单的想法(遵循趋势),我同时导入了 bootstrap 和 reactstrap,使用了 bootstrap 固定的底层类和像这样的解决方法。

class AppFooter extends Component{
render() {
    return(
        <div className="fixed-bottom">  
            <Navbar color="dark" dark>
                <Container>
                    <NavbarBrand>Footer</NavbarBrand>
                </Container>
            </Navbar>
        </div>
    )
}
Run Code Online (Sandbox Code Playgroud)

  • 在 IE 11、Chrome 74 和 Edge 14 中完美运行。使用 create-react-app 版本 3.0.1、React v16.8.6、reactstrap v8.0.0 和 bootstrap v4.3.1 (2认同)

Mar*_*ell 7

有一种更简单的方法。我正在使用React创建一个投资组合站点,我的某些页面不是很长,因此在某些设备中,例如kindle fire hd,页脚不会停留在底部。当然,以传统方式设置此功能是行不通的,因为它们会被包裹在其中。而且我们不想要那样。这就是我所做的:

在App.js中:

import React, { Component } from 'react';
import {Header} from './components/Header';
import {Main} from './components/Main';
import {Footer} from './components/Footer';

class App extends Component {
    render() {
        return (
            <div className="App Site">
                <div className="Site-content">
                    <div className="App-header">
                        <Header />
                    </div>
                    <div className="main">
                        <Main />
                    </div>
                </div>
                <Footer />
            </div>
        );
    }
}

export default App;
Run Code Online (Sandbox Code Playgroud)

然后在_sticky-footer.css中(我使用POSTCSS):

:root {
    --space: 1.5em 0;
    --space: 2em 0;
}

.Site {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

.Site-content {
    flex: 1 0 auto;
    padding: var(--space) var(--space) 0;
    width: 100%;
}

.Site-content:after {
    content: '\00a0';
    display: block;
    margin-top: var(--space);
    height: 0;
    visibility: hidden;
}
Run Code Online (Sandbox Code Playgroud)

Philip Walton为此创建了原始解决方案:https : //philipwalton.github.io/solved-by-flexbox/demos/sticky-footer/