Here you are:
'use strict';
var my_image,
my_imageSrc = 'https://cdn.spacetelescope.org/archives/images/large/heic1509a.jpg',//https://cdn.spacetelescope.org/archives/images/large/heic1509a.jpg
y = document.getElementById('LargeImage');
function showImage(){
var stats = document.getElementById('stats'),
y = document.getElementById('LargeImage');
stats.innerHTML = 'Ready, rendering...';
while(!y.complete){
setTimeout(function() {showImage()}, 10);
}
y.style.display = 'block';
setTimeout(function() {stats.innerHTML = '';}, 3000);
}
function PreloadImage(theElement) {
var my_image,
my_imageSrc = 'https://cdn.spacetelescope.org/archives/images/large/heic1509a.jpg',
stats = document.getElementById('stats'),
y = document.getElementById('LargeImage');
stats.innerHTML = 'Loading...';
y.style.display = 'none';
my_image = new Image();
my_image.src = my_imageSrc;
y.src = my_imageSrc+"?"+Date.now();
}
function ShowThumbnail() {
var y = document.getElementById('LargeImage');
y.style.display = 'none';
}
div.gallery img{
/*height: 70px;*/ /* EDIT - commented to keep aspect ratio */
width: 140px;
}
#LargeImage{
display: none;
/*height:100vh;*/ /* EDIT - commented to keep aspect ratio */
left:0px;
position:absolute;
top:0px;
width:100vw;
}
<div class="gallery">
<img src="https://cdn.spacetelescope.org/archives/images/large/heic1509a.jpg" onclick="PreloadImage(this)">
</div>
<span id="stats"></span>
<img src="" id="LargeImage" onclick="ShowThumbnail()" onload="showImage()">
Once your image have a width higher than height you need to set only the new width and let the browser do the trick with the new height
EDIT
and if you don't want scroll bars when the large image comes up, add to your css(consider the snippet scope):
/* this will avoid the scroll bar */
body{overflow:hidden}
EDIT 2
if the width of the image needs to fit the screen/page:
'use strict';
var my_image,
my_imageSrc = 'https://cdn.spacetelescope.org/archives/images/large/heic1509a.jpg',//https://cdn.spacetelescope.org/archives/images/large/heic1509a.jpg
y = document.getElementById('LargeImage');
function showImage(){
var stats = document.getElementById('stats'),
y = document.getElementById('LargeImage');
stats.innerHTML = 'Ready, rendering...';
while(!y.complete){
setTimeout(function() {showImage()}, 10);
}
y.style.display = 'block';
setTimeout(function() {stats.innerHTML = '';}, 3000);
}
function PreloadImage(theElement) {
var my_image,
my_imageSrc = 'https://cdn.spacetelescope.org/archives/images/large/heic1509a.jpg',
stats = document.getElementById('stats'),
y = document.getElementById('LargeImage');
stats.innerHTML = 'Loading...';
y.style.display = 'none';
my_image = new Image();
my_image.src = my_imageSrc;
y.src = my_imageSrc+"?"+Date.now();
}
function ShowThumbnail() {
var y = document.getElementById('LargeImage');
y.style.display = 'none';
}
div.gallery img{
/*height: 70px;*/ /* EDIT - commented to keep aspect ratio */
width: 140px;
}
#LargeImage{
display: none;
/*height:100vh;*/ /* EDIT - commented to keep aspect ratio */
left:0px;
position:absolute;
top:0px;
width:100%;
}
<div class="gallery">
<img src="https://cdn.spacetelescope.org/archives/images/large/heic1509a.jpg" onclick="PreloadImage(this)">
</div>
<span id="stats"></span>
<img src="" id="LargeImage" onclick="ShowThumbnail()" onload="showImage()">