简体   繁体   English

当页面变长(滚动条被激活)时,如何为对话框设置相对 Position?

[英]How to Set Relative Position for Dialog Box when Page Becomes longer (Scroll bars are activated)?

Have a web app which consists of a form and have it set up to launch a dialog box containing information next to the subject label text field.有一个 web 应用程序,它由一个表单组成,并设置为启动一个对话框,其中包含主题 label 文本字段旁边的信息。 Everytime, someone fills out the form and clicks on submit, the form's message body (from the text area of the form) is displayed on top and the form is displayed underneath it.每次,有人填写表单并单击提交,表单的消息正文(来自表单的文本区域)显示在顶部,表单显示在其下方。 Before, I had it set up as fixed (x,y) for the dialog box to appear next to the subject label.之前,我将其设置为固定 (x,y),以便对话框出现在主题 label 旁边。 But, now, when the page becomes longer, the dialog box doesn't appear next to my subject label text field.但是,现在,当页面变长时,对话框不会出现在我的主题 label 文本字段旁边。 It is displayed a lot lower.它显示得低很多。

Here's the code to find the position:这是找到 position 的代码:

// Finds the position and adds 40px to the left axis.
function findPosition(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
        curleft += 42;
//      alert ('[' + curleft + ', ' + curtop + ']');
        return [curleft,curtop];
    }
}

Here's the code that calls this function to display a dialog box at a specific place:这是调用此 function 以在特定位置显示对话框的代码:

function displayDialog() {
    var subjectLabel = document.getElementById("myform.subjectLabel");
     $("#myDialog").dialog({
         open: function(event, ui) {
             jQuery('.ui-dialog-titlebar-close')
                         .removeClass("ui-dialog-titlebar-close")
                         .html('<span padding-right = "16px;">Close</span>');
         },
         hide: true,
         draggable: false,
         position: findPosition(subjectLabel),
         closeOnEscape: false
     });
}

How can I set it so my findPosition() calculation doesn't miscalculate when the page is too long (when scroll bars are needed)?如何设置它,以便我的 findPosition() 计算在页面太长时不会计算错误(当需要滚动条时)?

Is there a way to set the relative positioning to always have the dialog appear 40px right of the subject label?有没有办法将相对定位设置为始终让对话框出现在主题 label 右侧 40 像素处?

Thank you for taking the time to read this.感谢您抽出时间来阅读。

You can accomplish this simply with CSS.您只需使用 CSS 即可完成此操作。 By setting your label's CSS to position: relative and placing a div with position: absolute inside of it, you can put the container anywhere you want relative to the label. By setting your label's CSS to position: relative and placing a div with position: absolute inside of it, you can put the container anywhere you want relative to the label.

Note that if you're going to try to position a div relative to a table cell, you'll need to do some extra work.请注意,如果您要尝试 position 相对于表格单元格的 div,则需要做一些额外的工作。 If the above doesn't work for you, let me know and I can dig up the full solution.如果上述方法对您不起作用,请告诉我,我可以找到完整的解决方案。

.mylabel {
    position: relative;
}
.mylabelfriend {
    display: none; /* if you don't want these 100% of the time */
    position: absolute;
    left: 40px;
    top: 0;
}

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

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