简体   繁体   English

使用jquery从主窗口打开中心弹出窗口?

[英]Center popup windows opened from main window using jquery?

In a web application I have lots of popup windows opened at different points.在 Web 应用程序中,我在不同的点打开了许多弹出窗口。 These are normal popup windows opened using window.open() and not Jquery popup windows.这些是使用 window.open() 打开的普通弹出窗口,而不是 Jquery 弹出窗口。 They open at different positions on the browser window and are also of different height and width.它们在浏览器窗口的不同位置打开,也有不同的高度和宽度。

Now, I would like to center all the popup windows when opened on the browser.现在,我想在浏览器上打开时将所有弹出窗口居中。 I have written a function which would return me the top and left values for centering for passed height and width.我已经编写了一个函数,它会返回顶部和左侧的值,用于对传递的高度和宽度进行居中。 Now, I need to call this function once before every time i use window.open to open the popup window, get the top and left values and then pass them to window.open.现在,我需要在每次使用 window.open 打开弹出窗口之前调用此函数一次,获取顶部和左侧的值,然后将它们传递给 window.open。 I might end up doing this in many places in various javascript files.我最终可能会在各种 javascript 文件的许多地方这样做。

Is there a generic way of achieving my requirement with few changes the centering up of all popup windows opened using window.open.有没有一种通用的方法来实现我的要求,而几乎没有改变使用 window.open 打开的所有弹出窗口的居中。 Can Jquery help me to do this easily? Jquery 可以帮助我轻松地做到这一点吗?

I think this is similar to what you currently have, but you might want to take a look.我认为这与您目前拥有的类似,但您可能想看看。 Doesn't seem like there's much need to use jQuery link似乎不太需要使用 jQuery 链接

function PopupCenter(pageURL, title,w,h) {
var left = (screen.width/2)-(w/2);
var top = (screen.height/2)-(h/2);
var targetWin = window.open (pageURL, title, 'toolbar=no, location=no, directories=no,     status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
} 

Just transform your code from只需将您的代码从

var params = getCenteredCoords();
window.open(url, "...", params+"...");

to

myPopup(url, "...", "...");

and declare a global并声明一个全局

function myPopup(url, name, params) {
    var coords = getCenteredCoords();
    return window.open(ul, name, coords+params);
}

And yes, you will need your custom solution in every of those files.是的,您将需要在每个文件中使用自定义解决方案。 So the only way is not to repeat yourself and use one global function.所以唯一的方法是不要重复自己并使用一个全局函数。

OK, there is a way not to change everything in all your files: Overwrite window.open with your custom solution:好的,有一种方法可以不更改所有文件中的所有内容:使用自定义解决方案覆盖window.open

window.open = (function(original){
    return function myPopup(url, name, params) {
        var coords = getCenteredCoords();
        return original(ul, name, coords+params);
    };
})(window.open);

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

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