我希望能够在达到指定的屏幕尺寸时更改布局,以便我可以更改网站在点击移动视图时的外观。
目前,我的反应网站使用包Horitzontal Scroll Package
水平滚动我尝试使用react-responsive软件包来实现这一点,它允许我运行媒体查询,但两个软件包之间似乎存在冲突。
所以我希望我的网站在全屏(桌面和桌面)时滚动水平,然后在移动视图中从上到下滚动。我该怎么做?
class Home extends React.Component{
constructor(props) {
super(props);
this.state = {};
}
componentWillMount() {
this.setState({
home: projectsData.filter( p => p.name === "homepage" ),
galleryImages: [
ImgOne,
ImgTwo,
ImgThree,
ImgFour,
ImgFive,
ImgSix
]
});
}
render(){
const test = { color: "black", position: "fixed" }
return(
<div className="row">
<Responsive minWidth={992}>
<HorizontalScroll reverseScroll={true}>
<Header />
<SplashScreen />
<CopyText />
</HorizontalScroll>
</Responsive>
</div>
);
}
}
注意:我知道水平滚动包不支持按百分比调整大小。
答案 0 :(得分:0)
我建议查看媒体查询。 Here是一段简短的视频,向您展示基础知识。
您可以设置一个阈值,在这些条件下触发不同的样式。当您希望UI执行不同大小的不同操作时,它非常有用。
@media only screen
and (*Your threshold criteria goes here* EXAMPLE: max-width : 1000px) {
//css goes here with whatever css you want to fire at this threshold
}
答案 1 :(得分:0)
我不熟悉HorizontalScroll组件,但我可以为您提供一个代码示例,说明如何使用React根据屏幕大小更改布局。
class WindowWidth extends React.Component {
constructor(props) {
super(props);
this.state = {};
this.onResize = this.onResize.bind(this);
}
render() {
return this.props.children({ width: this.state.width });
}
getWindowWidth() {
return Math.max(
document.documentElement.clientWidth,
window.innerWidth || 0
);
}
onResize() {
window.requestAnimationFrame(() => {
this.setState({
width: this.getWindowWidth()
});
});
}
componentWillMount() {
this.setState({
width: this.getWindowWidth()
});
}
componentDidMount() {
window.addEventListener("resize", this.onResize);
}
componentWillUnmount() {
window.removeEventListener("resize", this.onResize);
}
}
const Vertical = props => <div> Vertical component: {props.children}</div>;
const Horizontal = props => <div> Horizontal component: {props.children}</div>;
ReactDOM.render(
<WindowWidth>
{({ width }) => {
if (width >= 992) {
return <Horizontal>{width}px</Horizontal>;
}
return <Vertical>{width}px</Vertical>;
}}
</WindowWidth>,
document.querySelector("#react-app")
);
答案 2 :(得分:0)
You can use Material UI <Hidden>
component.
import React from 'react';
import PropTypes from 'prop-types';
import compose from 'recompose/compose';
import { withStyles } from '@material-ui/core/styles';
import Paper from '@material-ui/core/Paper';
import Hidden from '@material-ui/core/Hidden';
import withWidth from '@material-ui/core/withWidth';
import Typography from '@material-ui/core/Typography';
const styles = theme => ({
root: {
flexGrow: 1,
},
container: {
display: 'flex',
},
paper: {
padding: theme.spacing.unit * 2,
textAlign: 'center',
color: theme.palette.text.secondary,
flex: '1 0 auto',
margin: theme.spacing.unit,
},
});
function BreakpointUp(props) {
const { classes } = props;
return (
<div className={classes.root}>
<Typography variant="subtitle1">Current width: {props.width}</Typography>
<div className={classes.container}>
<Hidden xsUp>
<Paper className={classes.paper}>xsUp</Paper>
</Hidden>
<Hidden smUp>
<Paper className={classes.paper}>smUp</Paper>
</Hidden>
<Hidden mdUp>
<Paper className={classes.paper}>mdUp</Paper>
</Hidden>
<Hidden lgUp>
<Paper className={classes.paper}>lgUp</Paper>
</Hidden>
<Hidden xlUp>
<Paper className={classes.paper}>xlUp</Paper>
</Hidden>
</div>
</div>
);
}
BreakpointUp.propTypes = {
classes: PropTypes.object.isRequired,
width: PropTypes.string.isRequired,
};
export default compose(
withStyles(styles),
withWidth(),
)(BreakpointUp);