Last active
June 1, 2022 19:04
-
-
Save jgdev/11027309 to your computer and use it in GitHub Desktop.
Get percentage of image loading.
This file contains hidden or 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 lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <title>Get percentage loaded from image</title> | |
| <script type="text/javascript"> | |
| Image.prototype.onChangeSize; | |
| Image.prototype.load = function(url){ | |
| var _this = this, | |
| req = ((XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP")); | |
| req.open('GET', url, true); | |
| req.responseType = 'arraybuffer'; | |
| req.onload = function(e) { | |
| if(req.response) { | |
| var blob = new Blob([req.response]); | |
| _this.src = window.URL.createObjectURL(blob); | |
| } | |
| } | |
| req.onprogress = function(e) { | |
| if(_this.onChangeSize) { | |
| _this.onChangeSize(e.loaded, e.total); | |
| } | |
| } | |
| req.send(); | |
| } | |
| var img = new Image(); | |
| img.onChangeSize = function(loaded, total) { | |
| var _l = parseInt((loaded / total) * 100), | |
| loadSpan = document.getElementById('data-loaded'); | |
| console.log(loaded + " of " + total); | |
| loadSpan.innerHTML = 'Loaded image data %' + _l; | |
| } | |
| img.onload = function() { | |
| document.getElementById('data-loaded').style.display = 'none'; | |
| document.getElementsByTagName('body')[0].style.background = 'url(' + img.src + ')'; | |
| } | |
| img.load('http://jgdev.info/public/header-bg.png'); | |
| </script> | |
| </head> | |
| <body> | |
| <span id="data-loaded"></span> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment