style.marginTop = window.innerHeight不起作用?

时间:2015-06-06 07:56:41

标签: javascript

window.innerHeight不适用于document.getElementById("pic").style.marginTop

 <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Anirban Bhui</title>
    </head>
    <body>
    <img id="pic" src="logonit.png">
    <script>
    document.getElementById("pic").style.marginTop = window.innerHeight;
    </script>
    </body>
    </html>

1 个答案:

答案 0 :(得分:3)

在CSS中你需要有单位,在这种情况下是像素。

document.getElementById("pic").style.marginTop = window.innerHeight + 'px';

https://jsfiddle.net/fn1s4hvu/