如何使用React根据屏幕大小更改布局

5

我希望在特定屏幕尺寸下能够更改我的布局,以便在移动视图时更改我的网站外观。

目前,我的React网站使用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>
    );
  }
}

注意:我知道horizontal-scroll包不支持使用百分比进行大小调整。


也许你可以在componentDidMount中进行一些工作,就像这些答案中所示:https://dev59.com/hmMk5IYBdhLWcg3wvAVo - jmargolisvt
3个回答

5

你可以使用 Material UI 的 <Hidden> 组件。 在 Material Ui 的 Version5 中已经被弃用了。sx 属性取代了它。

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);

1

我不熟悉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")
);

0

我建议您了解一下媒体查询。这里有一个简短的视频,向您展示了基础知识。

您可以设置一个阈值,在这些条件下触发不同的样式。当您希望您的用户界面在不同的尺寸下表现出不同的效果时,这非常有用。

@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
}

3
这将应用新的CSS,但如果他想要完全不同的DOM,这并不能帮助他太多。 - jmargolisvt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接