【问题标题】:How to changing layout based on screen size with react如何通过反应根据屏幕尺寸更改布局
【发布时间】:2018-04-26 14:48:32
【问题描述】:

我希望能够在达到给定屏幕尺寸时更改布局,这样我就可以更改网站在移动视图时的外观。

目前我的反应站点使用包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>
    );
  }
}

注意:我知道水平滚动包不支持使用百分比调整大小。

【问题讨论】:

标签: javascript reactjs responsive-design


【解决方案1】:

You can use Material UI &lt;Hidden&gt; 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);

【讨论】:

    【解决方案2】:

    我建议查看媒体查询。 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
    }
    

    【讨论】:

    • 这将应用新的 CSS,但如果他想要一个完全不同的 DOM,这不会让他走得太远。
    【解决方案3】:

    我不熟悉 Horizo​​ntalScroll 组件,但我可以给你一个代码示例,说明如何使用 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")
    );
    

    【讨论】:

      猜你喜欢
      • 2018-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-17
      相关资源
      最近更新 更多