代码之家  ›  专栏  ›  技术社区  ›  Mark Moore

在视口中居中弹出窗口

  •  0
  • Mark Moore  · 技术社区  · 7 年前

    如何在视口中居中放置React材质UI弹出框?

    我的应用程序是基于Next.js框架构建的,它是基于React构建的。(我在下面包含了我的package.json中的完整依赖项。)

            <Popover
                id="video-popover"
                open={Boolean(video)}
                onClose={this.handleClose}
                anchorOrigin={{
                    vertical: 'center',
                    horizontal: 'center'
                }}
                transformOrigin={{
                    vertical: 'center',
                    horizontal: 'center'
                }}
            >
                <video controls autoPlay src={`/static/${video}`}>
                    Your browser does not support the
                        <code>video</code> element.
                    </video>
            </Popover>
    

    我想让视频在视图中居中。

    通常,我会设置Popover元素的anchorEl属性,Popover将位于锚元素的中心。但是,我应该使用什么元素在视口中居中?


      "dependencies": {
        "@material-ui/core": "^3.6.2",
        "next": "^7.0.2",
        "react": "^16.6.3",
        "react-dom": "^16.6.3",
        "react-icons": "^3.2.2"
      },
    
    1 回复  |  直到 7 年前
        1
  •  5
  •   karianpour    6 年前

    还有另一种方法:

    Popove已经有了一个 Div fixed 包含popover内容的位置,因此我们可以为其提供样式,并设置 anchorReference 到 none

    import React from 'react';
    import {
      makeStyles,
      Popover,
    } from '@material-ui/core';
    
    const useStyles = makeStyles (theme => ({
      popoverRoot: {
        display: 'flex',
        justifyContent: 'center',
        alignItems: 'center',
      },
    }));
    
    const CenteredPopOver = ({open}) => {
      const classes = useStyles();
    
      return (
        <Popover
          open
          anchorReference={"none"}
          classes={{
            root: classes.popoverRoot,
           }}
        >
          // what ever should be in the popover
          <div>I am in the center</div>
        </Popover>
      )
    }
    
    
        2
  •  1
  •   webdevdani    7 年前