我有一个页面,可以将大约 150+ 张图像加载到一个页面中。图像的结构是这样的:
网页代码
<table class="form-table">
<input type="hidden" name="bgtype" id="bgtype" value="pattern"/>
<tr class="wpbp-field-patternbg">
<th style="width:20%"><label for="patternbg">Pattern</label></th>
<td><div id="wpbp-pattern-select">
<div class="description">Select a pattern you would like to be applied as the background. Click on a pattern to select it.<br>
</div>
<input class="radio" type="radio" name="patternbg" id="patternbg2" value="45degreee_fabric.png" checked='checked'>
<label for="patternbg2" class="is-loading"><a class="pattern_preview" data-img="45degreee_fabric.png" data-content="45degreee fabric"><img src="45degreee_fabric.png" alt="" class="wpbp-pattern-img "/></a></label>
<input class="radio" type="radio" name="patternbg" id="patternbg3" value="patterns/60degree_gray.png" >
<label for="patternbg3" class="is-loading"><a class="pattern_preview" data-img="60degree_gray.png" data-content="60degree gray"><img src="60degree_gray.png" alt="" class="wpbp-pattern-img "/></a></label>
<input class="radio" type="radio" name="patternbg" id="patternbg4" value="always_grey.png" >
<label for="patternbg4" class="is-loading"><a class="pattern_preview" data-img="always_grey.png" data-content="always grey"><img src="always_grey.png" alt="" class="wpbp-pattern-img "/></a></label>
</div></td>
</tr>
</table>
(我已经删除了图像的完整路径,以使上面的代码至少)
CSS 代码
#wpbp-pattern-select.is-loading {
background-image: url('../images/loading.gif');
}
#wpbp-pattern-select .is-loading img {
opacity: 0;
}
简讯
var $container = $('#wpbp-pattern-select');
$imgs = $container.find('img'),
// use ImagesLoaded
$container.imagesLoaded()
.progress( onProgress )
// reset progress counter
imageCount = $container.find('img').length;
console.log( imageCount + ' properly loaded images' );
// triggered after each item is loaded
function onProgress( imgLoad, image ) {
var $item = $container.find('img');
$item.removeClass('is-loading');
}
我想要实现的是在每个单独的图像上显示预加载微调器 (.is-load),直到加载了某个图像。加载完成后,微调器将删除显示原始图像的内容。
我尝试使用jQuery图像加载库来使其工作,但我无法正确处理。这能做到吗?
任何帮助将不胜感激,谢谢
好的,
首先在你的CSS中你定义了#wpbp-pattern-select .is-loading img
但是在你的JS中,你尝试从图像本身中删除类:
function onProgress( imgLoad, image ) {
var $item = $container.find('img');
$item.removeClass('is-loading');
}
这是设计使然吗? 但是,请尝试以下 JS:
$(document).ready(function(){
var $container = $('#wpbp-pattern-select');
$imgs = $container.find('img');
// all images are hidden by css
$imgs.each(function() {
var el = $(this),
checkforloaded = setInterval(function() {
var image = el.get(0);
if (image.complete || image.readyState == 'complete' || image.readyState == 4) {
clearInterval(checkforloaded);
el.removeClass('is-loading'); // or: el.closest('label').removeClass('is-loading');
}
}, 20);
});
});
不过,我只是不确定这将如何处理许多图像