我正在尝试使用以下代码在单击图像时实现 Lightbox 效果
document.addEventListener('click',function(){
var target = event.target || event.srcElement;
if(target.className == "acv"){
var id = target.id, src= target.src,
type = target.nodeName, imgW, imgH,
temp = document.createElement('div'),
sHeight = screen.availHeight,
sWidth = screen.availWidth,
st = temp.style;
var img = new Image();
img.onload = function(){
imgW = this.width;
imgH = this.height;
}
img.src = src;
var imgH = ( sHeight >= imgH + 20 ? imgH : (sHeight - 20) );
var imgW = ( sWidth >= imgW + 20 ? imgW : (sWidth - 20) );
img.style.height = imgH;
img.style.width = imgW;
img.style.position = "relative";
img.style.left = ( (sWidth - imgW) / 2 ) + "px";
img.style.top = ( (sHeight - imgH) / 2 ) + "px";
temp.appendChild(img);
temp.id = "lightbox";
st.width = sWidth + "px";
st.height = sHeight + "px";
st.backgroundColor = "Gray";
st.zIndex = 123;
st.position = "absolute";
st.top = st.left = 0;
st.margin = "0 auto";
document.body.appendChild(temp);
}
});
请在此处检查工作小提琴。
我觉得我在这方面做错了什么。请更正并帮助我改进代码。