單擊按鈕時,我的網站將打開一個包含單個圖像的窗口。如何在彈出窗口中居中放置圖像
我已經得到了一切工作,而不是最後一點 - 如何將圖像居中彈出?
我不是問如何居中新窗口,只是新窗口的內容。
var img = document.createElement("img");
img.src = cont.getAttribute("data-img");
img.style.boxShadow = "1px 1px 3px #333;"
//as per https://css-tricks.com/snippets/css/absolute-center-vertical-horizontal-an-image/
img.style.position="absolute";
img.style.top= "50%";
img.style.left= "50%";
img.style.marginTop= Number((img.height/2) * - 1) + "px"; /* Half the height */
img.style.marginLeft= Number((img.width/2) * - 1) + "px"; /* Half the width */
showImage(img);
});
function showImage(imgEle) {
var w = imgEle.width + (10/100 * imgEle.width);
var h = imgEle.height+ (10/100 * imgEle.height);
var url=imgEle.getAttribute('src');
window.open(url,"Image Viewer",'width='+w+',height='+h+',resizable=1, titlebar=no, toolbar=no');
}
在新窗口顯示和我的形象,上述工作是在頁面的唯一的事,但圖像卡在左上方,我想它爲中心。
我知道這種方法存在問題(彈出窗口阻止程序)。
按使用Javascript的標籤,我清楚地只使用JavaScript中的事實,我以後JavaScript解決方案(在任何庫/框架不依賴),請
,您可以批准一個答案? –