如何在反应中制作粘性页脚?
How to make a sticky footer in react?
我制作了一个粘性页脚高级组件,将其他组件包裹在自身内部:
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;
用法:
<Footer><Button>test</Button></Footer>
但是它隐藏了页面内容:
这看起来是个常见问题,所以我搜索了一下,发现 this issue, where is FlexBox is recommended for the sticky footer. But at this demo the footer is at the very bottom of the page, while I need the footer to be always displayed on the page and the content being scrolled inside the above area (like in SO chat)。除此之外,还建议使用自定义样式表规则更改所有其他组件。是否可以仅使用页脚组件的样式来实现我需要的功能,以便代码保持模块化?
您可以通过将 margin-bottom: 60px;
添加到您网站的 body
来解决这个问题。 60px
是页脚的高度。
Here's an idea(沙盒示例 link)。
在您的页脚组件中包含一个幻影 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
有一个更简单的方法。我正在用 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;
然后在_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: '[=11=]a0';
display: block;
margin-top: var(--space);
height: 0;
visibility: hidden;
}
最初的解决方案是由 Philip Walton 创建的:https://philipwalton.github.io/solved-by-flexbox/demos/sticky-footer/
我想分享这个有效的解决方案。我从 https://react.semantic-ui.com/modules/sticky 抄袭了这个。滚动到此页面的底部并检查文本 'This is the bottom' 以查看我在哪里偷了它。它是一个基于 React 的网站,因此它应该适合您的情况。
这里是:
{
padding-top: 50vh;
}
从概念上讲,此解决方案正在创建负面 space,就像 jacoballenwood 的幻影 div 一样,将页脚向下推到底部并将其固定在那里。只需将其添加到页脚的 css 样式 class 并根据需要调整值。
更简单的想法(顺应潮流),我导入了 bootstrap 和 reactstrap,使用了 bootstrap 固定底部 class 和类似的解决方法。
class AppFooter extends Component{
render() {
return(
<div className="fixed-bottom">
<Navbar color="dark" dark>
<Container>
<NavbarBrand>Footer</NavbarBrand>
</Container>
</Navbar>
</div>
)
}
.App 将是您加载到 Root 的主要组件。
假设页脚是文档流中 .App 的最后一个子项
.App {
height: 100vh;
display: flex;
flex-direction: column;
}
footer {
margin-top: auto;
}
我发现如果将 'footer' 组件包装在标准 html
中
<footer>
tag,差不多给你整理好了所有的定位
很晚才回答,但有人会发现这很有用。您可以设置工具栏,而不是幻影样式。我为组件构建了一些标准布局,其中 {children} 是来自父组件的组件 - App.js。这是示例:
import React from "react";
import { Route } from "react-router-dom";
import { makeStyles } from "@material-ui/core/styles";
import AppBar from "@material-ui/core/AppBar";
import CssBaseline from "@material-ui/core/CssBaseline";
import Toolbar from "@material-ui/core/Toolbar";
import Header from "../components/header";
import Footer from "../components/footer";
import SideBar from "../components/sidebar";
const useStyles = makeStyles((theme) => ({
root: {
display: "flex",
},
appBar: {
zIndex: theme.zIndex.drawer + 1,
},
content: {
flexGrow: 5,
padding: theme.spacing(3),
},
}));
const StandardLayout = ({ children }) => {
const classes = useStyles();
return (
<div className={classes.root}>
<CssBaseline />
<AppBar position="fixed" className={classes.appBar}>
<Route path="/" component={Header} />
</AppBar>
<SideBar />
<main className={classes.content}>
<Toolbar />
<br />
{children}
<Toolbar/>
</main>
<AppBar className={classes.appBar}>
<Route path="/" component={Footer} />
</AppBar>
</div>
);
};
export default StandardLayout;
.footer{
width: 100%;
position: fixed;
bottom: 0;
}
这应该可以解决问题!干杯! (:
它对我来说是规则
<footer style={{position:"fixed",bottom:"0"}}>
我制作了一个粘性页脚高级组件,将其他组件包裹在自身内部:
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;
用法:
<Footer><Button>test</Button></Footer>
但是它隐藏了页面内容:
这看起来是个常见问题,所以我搜索了一下,发现 this issue, where is FlexBox is recommended for the sticky footer. But at this demo the footer is at the very bottom of the page, while I need the footer to be always displayed on the page and the content being scrolled inside the above area (like in SO chat)。除此之外,还建议使用自定义样式表规则更改所有其他组件。是否可以仅使用页脚组件的样式来实现我需要的功能,以便代码保持模块化?
您可以通过将 margin-bottom: 60px;
添加到您网站的 body
来解决这个问题。 60px
是页脚的高度。
Here's an idea(沙盒示例 link)。
在您的页脚组件中包含一个幻影 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
有一个更简单的方法。我正在用 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;
然后在_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: '[=11=]a0';
display: block;
margin-top: var(--space);
height: 0;
visibility: hidden;
}
最初的解决方案是由 Philip Walton 创建的:https://philipwalton.github.io/solved-by-flexbox/demos/sticky-footer/
我想分享这个有效的解决方案。我从 https://react.semantic-ui.com/modules/sticky 抄袭了这个。滚动到此页面的底部并检查文本 'This is the bottom' 以查看我在哪里偷了它。它是一个基于 React 的网站,因此它应该适合您的情况。
这里是:
{
padding-top: 50vh;
}
从概念上讲,此解决方案正在创建负面 space,就像 jacoballenwood 的幻影 div 一样,将页脚向下推到底部并将其固定在那里。只需将其添加到页脚的 css 样式 class 并根据需要调整值。
更简单的想法(顺应潮流),我导入了 bootstrap 和 reactstrap,使用了 bootstrap 固定底部 class 和类似的解决方法。
class AppFooter extends Component{
render() {
return(
<div className="fixed-bottom">
<Navbar color="dark" dark>
<Container>
<NavbarBrand>Footer</NavbarBrand>
</Container>
</Navbar>
</div>
)
}
.App 将是您加载到 Root 的主要组件。
假设页脚是文档流中 .App 的最后一个子项
.App {
height: 100vh;
display: flex;
flex-direction: column;
}
footer {
margin-top: auto;
}
我发现如果将 'footer' 组件包装在标准 html
中<footer>
tag,差不多给你整理好了所有的定位
很晚才回答,但有人会发现这很有用。您可以设置工具栏,而不是幻影样式。我为组件构建了一些标准布局,其中 {children} 是来自父组件的组件 - App.js。这是示例:
import React from "react";
import { Route } from "react-router-dom";
import { makeStyles } from "@material-ui/core/styles";
import AppBar from "@material-ui/core/AppBar";
import CssBaseline from "@material-ui/core/CssBaseline";
import Toolbar from "@material-ui/core/Toolbar";
import Header from "../components/header";
import Footer from "../components/footer";
import SideBar from "../components/sidebar";
const useStyles = makeStyles((theme) => ({
root: {
display: "flex",
},
appBar: {
zIndex: theme.zIndex.drawer + 1,
},
content: {
flexGrow: 5,
padding: theme.spacing(3),
},
}));
const StandardLayout = ({ children }) => {
const classes = useStyles();
return (
<div className={classes.root}>
<CssBaseline />
<AppBar position="fixed" className={classes.appBar}>
<Route path="/" component={Header} />
</AppBar>
<SideBar />
<main className={classes.content}>
<Toolbar />
<br />
{children}
<Toolbar/>
</main>
<AppBar className={classes.appBar}>
<Route path="/" component={Footer} />
</AppBar>
</div>
);
};
export default StandardLayout;
.footer{
width: 100%;
position: fixed;
bottom: 0;
}
这应该可以解决问题!干杯! (:
它对我来说是规则
<footer style={{position:"fixed",bottom:"0"}}>