gpt4 book ai didi

javascript - 如何在 Material-UI 中将对话框置于顶部

转载 作者:行者123 更新时间:2023-12-01 21:43:01 25 4
gpt4 key购买 nike

我目前正在创建一个弹出对话框。但是,我的对话框位于中间弹出。

当我按下弹出按钮时,如何使位置在页面的最顶部?

这是我的代码

      <div>
<div id="so_reg" className="buy_btn" onClick={this.onClickCashBuy.bind(this)}>
Pop up button
</div>
<Dialog className="dialog" modal={false} open={this.state.buy_cash_popup} onRequestClose={this.onClickBuyCashCancel} style={{color: 'green'}} >
<Test product={{price: this.state.buy_cash_type}} />
</Dialog>
</div>

最佳答案

您可以在<Dialog /> 中覆盖scrollPaper 的样式

功能组件

const useStyles = makeStyles({
scrollPaper: {
alignItems: 'baseline' // default center
}
});
const classes = useStyles();

经典组件

const styles = theme => createStyles({
scrollPaper: {
alignItems: 'baseline' // default center
}
});
const { classes } = props;
export default withStyles(styles)(YourComponent);

用法

<Dialog classes={{scrollPaper: classes.scrollPaper }}>

引用:Material-UI文档Dialog CSS API

在开发工具中可以看到的Dialog的HTML结构

从下面选择MuiDialog-scrollPaper

<div
class="MuiDialog-container MuiDialog-scrollPaper makeStyles-dialog-163"
role="none presentation"
tabindex="-1"
style="opacity: 1; transition: opacity 225ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;"
>
<div
class="MuiPaper-root MuiDialog-paper MuiDialog-paperScrollPaper MuiDialog-paperWidthSm MuiPaper-elevation24 MuiPaper-rounded"
role="dialog"
aria-labelledby="simple-dialog-title"
>
...
</div>
</div>

enter image description here


在你的代码中

import { withStyles } from '@material-ui/styles';

const styles = theme => createStyles({ // change to this
scrollPaper: {
alignItems: 'baseline'
}
});

class Shop extends Component {
render(){

const { classes } = this.props; // add this

return(
<Dialog className="dialog" classes={{scrollPaper: classes.scrollPaper }} modal={false} open={this.state.buy_cash_popup} onRequestClose={this.onClickBuyCashCancel} style={{color: 'green'}} >
<Test product={{price: this.state.buy_cash_type}} />
</Dialog>
export default withStyles(styles)(Shop); // `styles` here rather than `class`

关于javascript - 如何在 Material-UI 中将对话框置于顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61094220/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com