0
バックグラウンドイメージのロードの進捗をどのように追跡できますか?私はthis articleをフォローしていて、これまでこのコードを書いていました。バックグラウンド画像JavaScriptを使用して進捗トラックをロードしています
var progressbar = document.querySelector('#imgload');
var img = document.createElement('img');
img.onloadstart = function(){
\t progressbar.innerHTML=0;
};
img.onload = function(){
document.body.style.backgroundImage='url("High-Res-Wallpaper-HD-For-Desktop.jpg")';
\t // img.parentElement.removeChild(img);
};
img.onprogress = function(e){
\t if(e.lengthComputable){
\t \t progressbar.innerHTML = e.loaded/e.total * 100;
\t }
};
img.onloadend = function(){
\t progressbar.innerHTML=100;
};
img.src = 'High-Res-Wallpaper-HD-For-Desktop.jpg';
.container{
margin: 0 auto;
width: 900px;
}
<!DOCTYPE html>
<html>
<head>
\t <title>Background image load progress</title>
</head>
<body>
\t <div class="container">
\t <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
\t tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
\t quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
\t consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
\t cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
\t proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
\t </div>
\t <div class='progress'>
\t \t percentage of image load : <span id='imgload'> </span>
\t </div>
</body>
</html>
しかし、それは正常に動作していません。進捗状況は、画像のロード中に進捗値を表示する必要があります。誰か私にそれをする方法をいくつか考えを与えることができますか?
「正しく動作していません」という意味はどうですか? – Rayon
'img'要素が' DOM'に追加されることはありません。どうすれば削除できますか? – Rayon
@Rayon私はメモリリークを防止しようとしていました。その行を修正してみましょう。 –