The read-only naturalHeight
property returns the original height of an image.
naturalHeight |
Yes | Yes | Yes | Yes | Yes |
var v = imageObject.naturalHeight
A Number type value representing the original height of an image in pixels.
The difference between the naturalHeight property and the height property.
<!DOCTYPE html>
<html>
<body>
<img id="myImg" src="http://java2s.com/style/demo/border.png" style="width:100px;height:100px;">
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<p id="demo2"></p>
<script>
function myFunction() {<!-- ww w. j a v a 2s . c om-->
var x = document.getElementById("myImg").naturalHeight;
document.getElementById("demo").innerHTML = "The original height: " + x + " pixels";
var y = document.getElementById("myImg").height;
document.getElementById("demo2").innerHTML = "The 'styled' height: " + y + " pixels";
}
</script>
</body>
</html>
The code above is rendered as follows:
The following code shows how to Return the original height of an image.
<!DOCTYPE html>
<html>
<body>
<img id="myImg" src="http://java2s.com/style/demo/border.png" style="width:100px;height:100px;">
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<!--from w ww .jav a2 s.c om-->
<script>
function myFunction() {
var x = document.getElementById("myImg").naturalHeight;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
The code above is rendered as follows: