我想創建一個彈出式窗口,其中的圖像,我希望彈出與圖像相同的長寬比。但我只知道image.onload上的圖像尺寸。 所以我需要的是在image.onload之後的彈出窗口。但是,瀏覽器將其視爲不需要的彈出窗口,並會阻止它。即使它是用戶點擊後的唯一彈出窗口。彈出後圖像onload
我曾嘗試創建彈出窗口,並稍後修改內容和尺寸,但即使在工作時它有時也會導致彈出窗口隱藏在背景中,無法將其重新放回到前端(窗口上的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);
}
}
當我不首先創建加載圖像時,問題不會消失。所以它不是關於第二個彈出窗口,而是關於image.onload。除此之外,我的確在考慮將圖像加載腳本放置在打開圖像的其他文件中,但是您確定window.resizeTo在所有瀏覽器的窗口中都可以工作嗎? – 2012-07-20 11:57:08
我對在不同瀏覽器中使用'resizeTo'或'moveTo'沒有多少經驗,但我會留下給你測試,因爲我剛剛以我爲你做的一個例子編輯了我的答案。讓我知道,如果我能以任何其他方式提供幫助。 – Spooky 2012-07-20 13:01:15
經過一些測試後,我發現在我的例子中,即使網址欄稍微覆蓋圖片,彈出窗口也會調整圖片大小。如果你不喜歡這個,你可能希望添加一些填充到它將改變的大小和/或找到客戶區域並從窗口區域中減去它,以便找到你必須在每個方向填充多少。 – Spooky 2012-07-20 13:06:15