0
我正在创建我的网站封面。我使用js将图像显示在垂直中心。这里是代码:Safari无法通过js获取图像的高度
function middle(){
var windowHeight=window.innerHeight;
var infographic=document.querySelector("#infographic img");
var imgHeight=parseFloat(window.getComputedStyle(infographic).height);
infographic.style.maxHeight=(windowHeight-120)+"px";
infographic.style.marginTop=(windowHeight-imgHeight)/2+"px";
console.log(windowHeight);
console.log(imgHeight);
console.log(infographic.style.marginTop);
console.log(infographic);
}
middle();
在Chrome上一切正常。但是当我在Safari上打开该网站时,它不能很好地工作。有时图像显示在底部,“imgHeight”的控制台日志为0,而我没有更改任何代码,只需点击刷新按钮即可。
https://440creates.com 这是链接到网站。 –
试试这个:https://stackoverflow.com/a/1944298/5737771 –