Created
August 26, 2012 01:45
-
-
Save hysysk/3473035 to your computer and use it in GitHub Desktop.
Expand image to full page with JavaScript and CSS3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>fullpage</title> | |
<script> | |
window.onload = function() { | |
var body = document.getElementsByTagName('body')[0]; | |
body.addEventListener("click", openFullPage, false); | |
function openFullPage(e) { | |
if(e.target && e.target.nodeName == 'IMG') { | |
var fullpage = document.createElement('div'); | |
body.appendChild(fullpage); | |
fullpage.style.backgroundImage = 'url("' + e.target.src + '")'; | |
fullpage.style.backgroundSize = 'contain'; | |
fullpage.style.backgroundRepeat = 'no-repeat'; | |
fullpage.style.backgroundColor = '#ffffff'; | |
fullpage.style.width = '100%'; | |
fullpage.style.height = '100%'; | |
fullpage.style.backgroundPosition = 'center center'; | |
fullpage.style.position = 'absolute'; | |
fullpage.style.top = body.scrollTop + 'px'; | |
fullpage.style.left = '0'; | |
fullpage.addEventListener("click", closeFullPage, false); | |
} | |
} | |
function closeFullPage(e) { | |
body.removeChild(e.target); | |
} | |
}; | |
</script> | |
</head> | |
<body> | |
<img src="http://placehold.it/400x400" /> | |
<img src="http://placehold.it/400x400" /> | |
<img src="http://placehold.it/400x400" /> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment