【问题标题】:Reactjs: How to move dialog to top of the screen on clickReactjs:如何在点击时将对话框移动到屏幕顶部
【发布时间】:2020-08-30 19:21:49
【问题描述】:

当我点击一个可拖动的对话框时,我想移动到屏幕顶部。

我尝试过使用scrollTo(0, 0),但这似乎对我的对话框没有任何作用。任何关于它的帮助,我怎样才能移动这个对话框。

提前非常感谢。 我正在关注这个 Move draggable objects to top of the screen or Div? 示例,但需要点击它。

    class Homepage extends Component {
      constructor(props) {
        super(props);
        this.state = {
          Pending:[],
          cancelled:[],
        }
        this.myRef = React.createRef();
      }
    
      componentDidMount() {
        this.myRef.current.scrollTo(0, 0);
      }
    
      componentDidUpdate(prevProps, prevState) {
        if(!prevProps.isModerator){
          if(prevProps.Permission === Permission && 
              this.props.Permission === Requested){
          }
        }
      }
    
      render() {
        const {allowRequest, classes} = this.props;
        const { Pending} = this.state;
        return (
            <>
              {Pending.map((user, index) => {
                return <Draggable bounds="parent" key={user.id} ref={this.myRef}>
                  <div className={classes["share-request-content"]} >
                    <Paper className="alert-container" square={true}>
                      <DialogTitle id="alert-title">
                        {/* Title here */}
                      </DialogTitle>
                      <DialogContent>
                      {/* Content here */}
                      </DialogContent>
                      <DialogActions className="dialog-action">
                        <Button autoFocus color="primary" onClick={() => allowRequest(user.id)}>
                          "Allow"
                        </Button>
                      </DialogActions>
                    </Paper>
                  </div>
                </Draggable>
              })
              }
            </>
        )
      }
    }
    
    Homepage.propTypes = {
      Permission: PropTypes.string,
      isModerator: PropTypes.bool.isRequired,
      Pending: PropTypes.array,
      cancelled: PropTypes.array,
    };
    
    export default withStyles(styles)(injectIntl(Homepage))

【问题讨论】:

    标签: javascript reactjs dom dialog draggable


    【解决方案1】:

    使用 position={{x:0, y:0}} 属性手动控制对话框的位置。

    您可以在这里找到参考资料:https://github.com/STRML/react-draggable

    【讨论】:

    • 您好 Jassim,感谢您抽出宝贵时间。但是当我点击它时,我想这样做。但这是关于默认位置的。不是吗?
    • 没有。默认位置有不同的选项 defaultPosition={{x: 0, y: 0}}。在文档中查找 Controlled V/S UnControlled 的部分以获得更好的清晰度
    • 您是否介意建议我如何通过 onclick 实现这一点。我在这里没有找到示例strml.github.io/react-grid-layout/examples/0-showcase.html
    猜你喜欢
    • 2012-02-15
    • 1970-01-01
    • 1970-01-01
    • 2018-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-16
    相关资源
    最近更新 更多