我正在尝试创建一个包含图像的弹出窗口,并且我希望弹出窗口与图像的纵横比相同。但我只知道 image.onload 上的图像尺寸。所以我需要的是 image.onload 之后的弹出窗口。但是浏览器将其视为不需要的弹出窗口并将其阻止。即使它是用户点击后的唯一弹出窗口。
我曾尝试先创建弹出窗口,然后再更改内容和大小,但即使这样,有时也会导致弹出窗口隐藏在后台,无法再次将其恢复到前面(windows上的chrome)
有没有办法确保延迟的弹出仍然hooked
是用户的点击?或者其他一些建议。
我想要工作的代码:
selectURL: function(url) {
// Check for images, otherwise just simply open the file by the browser.
if(url.match(/(jpe?g|png|gif|bmp)$/gim)) {
// Check if colorbox exists and use if so.
if($.colorbox) {
$.colorbox({
maxWidth:'80%',
maxHeight:'80%',
fixed: true,
scrolling: false,
href: url,
open: true,
photo: true
});
} else {
// New image object so we can calculate the required popup size on load
var myImage = new Image();
myImage.src = url;
// Close current window to make sure focus works
if(typeof(popupWindow) !== 'undefined') {
popupWindow.close();
}
// Temporary loading image
popupWindow = window.open('/images/loading.gif','popupWindow','height=400,width=600,resizable=no,scrollbars=no,top=200,left=200');
if (window.focus) {
popupWindow.focus();
}
// Calculator and actual image opener :)
myImage.onload = function() {
var maxWidth = 600,
maxHeight = 600;
// Close existing popup
if(typeof(popupWindow) !== 'undefined') {
popupWindow.close();
}
if(this.width < maxWidth && this.height < maxHeight) {
popupWindow = window.open(this.src,'popupWindow','height='+this.height+',width='+this.width+',resizable=no,scrollbars=no,top=200,left=200');
} else if(this.width === this.height) {
popupWindow = window.open(this.src,'popupWindow','height='+maxHeight+',width='+maxWidth+',resizable=yes,scrollbars=yes,top=200,left=200');
} else if(this.width < this.height) {
popupWindow = window.open(this.src,'popupWindow','height='+maxHeight+',width='+(maxHeight / this.height * this.width)+',resizable=yes,scrollbars=yes,top=200,left=200');
} else {
popupWindow = window.open(this.src,'popupWindow','height='+(maxWidth / this.width * this.height)+',width='+maxWidth+',resizable=yes,scrollbars=yes,top=200,left=200');
}
if (window.focus) {
popupWindow.focus();
}
}
}
} else {
window.open(url);
}
}