简体   繁体   中英

Dynamically calculate value in styling material-UI

I am using Material-UI style to set class .

Now I want to set height according to browser width .

Is it possible to calculate dynamically in makeStyles() or some work-around??

This is the code what I want.

const useStyles = makeStyles({
 videoContainer:{
    height: this.width / 16 * 9 // dynamically calculate height
  }
}

const Info = (props) => {
  const classes = useStyles();
  const { ...rest } = props;
  return (
    
    <div class={classes.videoContainer}>
    <video src={require("assets/180226.mp4")} 
        autoPlay muted loop>
    </video>
    </div>
  );
}

Yes you can set height dynamically inside makeStyles()

const useStyles = makeStyles({
 videoContainer:{
    height: window.innerWidth / 16 * 9 // dynamically calculate height
  }
}

Or You can set a variable at beginning and use it inside makeStyles()

const ht = window.innerHeight // do some calculation
const useStyles = makeStyles({
 videoContainer:{
    height: `${ht}`
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM