x.style.height
is an empty string because it's looking at the #movie
element's style
attribute (which is initially empty). The error likely stems from attempting to divide a string by a number.
To figure out what styles are applied via a style sheet or <style>
block, you could look at document.getComputedStyle()
. However, where you're interested in the height of the element, you might find it preferable to look at the element's offsetHeight
property instead. Once you've computed the new height, you'll also need to make sure you append the correct units (e.g. px
) before adding it as a style to the element.
x.style.height = x.offsetHeight / ar + 'px';
window.onload = function() {
var x = document.getElementById('movie');
var ar = 1.33;
x.style.height = x.offsetHeight / ar + 'px';
};
body {
background: black;
margin: 0 auto;
max-width: 200px;
height: auto;
}
#movie {
width: 200px;
height: 100px;
background: navy;
}
.t {
-webkit-animation: change 48s infinite;
-moz-animation: change 48s infinite;
-o-animation: change 48s infinite;
animation: change 48s infinite;
}
.name {
font-family: 'Alegreya Sans', Helvetica, sans-serif;
font-size: 13px;
color: gold;
text-align: center;
letter-spacing: 3px;
margin-top: 36px;
opacity: 0.3;
}
@keyframes change {
from {
background-color: black;
}
to {
background-color: white;
}
}
<body>
<div id="movie" class="t"></div>
<p class="name">Pedro Costa</p>
</body>
Alternatively, you could seed the #movie
element's height as an inline style, but this is generally considered poorer practice.
x.style.height = parseInt(x.style.height) / ar + 'px';
window.onload = function() {
var x = document.getElementById('movie');
var ar = 1.33;
x.style.height = parseInt(x.style.height) / ar + 'px';
};
body {
background: black;
margin: 0 auto;
max-width: 200px;
height: auto;
}
#movie {
width: 200px;
background: navy;
}
.t {
-webkit-animation: change 48s infinite;
-moz-animation: change 48s infinite;
-o-animation: change 48s infinite;
animation: change 48s infinite;
}
.name {
font-family: 'Alegreya Sans', Helvetica, sans-serif;
font-size: 13px;
color: gold;
text-align: center;
letter-spacing: 3px;
margin-top: 36px;
opacity: 0.3;
}
@keyframes change {
from {
background-color: black;
}
to {
background-color: white;
}
}
<body>
<div id="movie" class="t" style="height: 100px;"></div>
<p class="name">Pedro Costa</p>
</body>