简体   繁体   English

调整页面大小时更改位置p:对话框

[英]Change position p:dialog when I resize the page

i'm a problem with ap:dialog. 我是ap:dialog的问题。

Here the code: 这里的代码:

        <p:dialog id="loginDialog" header="Login" width="400" widgetVar="dlg" visible="true"
            rendered="#{loginBean.f_loginRendered}" closable="false" showEffect="clip" draggable="false" resizable="false"
            style="box-shadow: 7px 10px 5px #303030; position:absolute;">
            <h:panelGrid id="panelGrid" columns="2" cellpadding="5" style="margin: 0 auto;">
                <h:outputLabel for="username" value="Username: *" />
                <p:inputText id="username" value="#{loginBean.f_username}" required="true" label="Username"/>
                <h:outputLabel for="password" value="Password: *" />
                <p:password id="password" value="#{loginBean.f_password}" required="true" label="Password"/>

<center>            <p:commandButton id="loginButton" value="Login" update="globalGrowl" actionListener="#{loginBean.checkDown}" /> </center>
        </h:panelGrid > 

        </p:dialog>

It's a simple login form. 这是一个简单的登录表格。 The problem is that when I resize the webpage, the dialog is fixed and it does not move by following the page, unlike the pictures. 问题是,当我调整网页大小时,对话框是固定的,并且与图片不同,它不会随着页面的移动而移动。

How I can do it? 我该怎么办?

I would recommend to place it with css instead of jquery. 我建议用CSS而不是jquery放置它。 The jquery resize event in this case, can lead to performance lags. 在这种情况下,jquery的resize事件可能会导致性能下降。

In case your web page is resizing only horizontally, you can do it using JavaScript by adding this code in h:body element of your page 如果您的网页仅水平调整大小,则可以通过在页面的h:body元素中添加以下代码来使用JavaScript进行调整

<script type="text/javascript">
            window.onresize = function(event) {
                console.log("Screen is resized");
                var dialog = document.getElementById("loginDialog");
                var windowHeight=window.innerHeight;
                var windowWidth=window.innerWidth;
                console.log("Window W/H=" + windowWidth + "/" + windowHeight);
                //width of your dialog set in p:dialog 
                var dialogWidth=400;
                //position your dialog x px left from browser window left border
                dialog.style.left=((windowWidth-dialogWidth)/2)+"px"
            }
</script>

Beware that 当心

  • if your p:dialog is wrapped up by h:form element with id defined, you will need to change one line of script above like this 如果您的p:dialog被定义了id的h:form元素包裹起来,则需要像上面这样更改一行脚本

     var dialog = document.getElementById("yourFormId:loginDialog"); 
  • if your web page is resizing vertically too, you will need to set/calculate p:dialog height and to apply additional line inside JavaScript function 如果您的网页也是垂直调整大小,则需要设置/计算p:dialog的高度,并在JavaScript函数中应用其他行

     dialog.style.top = ((windowHeight-dialogHeight)/2)+"px" 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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