Javascript在事件上显示图像中心屏幕

时间:2016-01-16 21:32:18

标签: javascript image

我正在编写一个脚本,等待某人输入代码(向上向下左下方右下方)然后播放声音并在屏幕上显示一个gif,然后在gif为时移除完成了比赛。

我的问题是我不知道如何在没有document.body.appendChild()的情况下显示gif,它将它放在网站的左下角。此外,我不知道如何让它在完成时消失。

npm install

1 个答案:

答案 0 :(得分:0)

有多种可能的解决方案,但没有看到您的网页代码(假设您正在尝试使用vanilla javascript进行所有操作),唯一的解决方案就是使用javascript来设置图片&# 39;将位置属性设置为绝对值,然后通过检索屏幕尺寸来定位图像。

 var winW = 630, winH = 460;
if (document.body && document.body.offsetWidth) {
 winW = document.body.offsetWidth;
 winH = document.body.offsetHeight;
}
if (document.compatMode=='CSS1Compat' &&
    document.documentElement &&
    document.documentElement.offsetWidth ) {
 winW = document.documentElement.offsetWidth;
 winH = document.documentElement.offsetHeight;
}
if (window.innerWidth && window.innerHeight) {
 winW = window.innerWidth;
 winH = window.innerHeight;
}

console.log('Window center X = '+(winW/2));
console.log('Window center Y = '+(winH/2));