我正在尝试创建一个图库,我正在尝试将图像加载到div中,当它完成加载时,它会淡化它,尽管它不是很有用。如果我删除显示:无图像确实加载到div中,这是fadeIn函数无法正常工作。我能得到的任何帮助都会很棒,谢谢。
这是js:
function fadeIn(obj) {
$(obj).fadeIn(1000);
}
$('.enlarge').click(function() {
var jthis = this; // save the reference to the $('.enlarge') that was clicked
var id = $(this).find(".enlarged_txt").attr('id');
$('#full_image').animate({
height: "100%"
}, 300, function() {
if ( $(jthis).hasClass("v") ) {
$('#preload').prop('src', 'http://www.klossal.com/klossviolins/instruments/violins/full/' + id + '.jpg');
fadeIn('#' + id);
}
});
});
这是带按钮的容器:
<div class="enlarge bv">
<img class="enlarged_unselected" src="http://www.klossal.com/klossviolins/elements/fullscreen_unselected.png"/>
<img class="enlarged_selected" src="http://www.klossal.com/klossviolins/elements/fullscreen_selected.png"/>
<div id="ESaitory_408" class="enlarged_txt">Enlarge Image</div>
</div>
这是我正在加载的容器:
<div id="full_image">
<img id="preload" onload="fadeIn(this)" src="#" style="display:none;"/>
</div>
答案 0 :(得分:0)
您需要检查complete
img属性,因为如果已加载图像,则不会触发onload。我做了自己的方法,因为你的代码感觉不好:
HTML
<button id="enlarge" data-target="big" data-src="http://placehold.it/400x250">Click to enlarge</button>
<button id="shrink" data-src="http://placehold.it/350x150" data-target="small">Click, to shrink down</button>
<div id='show'>
<div id="big" class="images"><img/></div>
<div id="small" class="images"><img src="http://placehold.it/350x150"/></div>
</div>
JS
$('button').click(function () {
var $this = $(this);
var target = $this.data('target');
var src = $this.data('src');
var image = $('#' + target + '.images img');
image.one('load', show_parent_hide_siblings);
image.attr('src', src);
if (image.get(0).complete) { //TAKE A LOOK HERE!
image.trigger('load');
}
});
function show_parent_hide_siblings(image) {
var $this = $(this);
$this.parent().fadeIn(1000);
$this.parent().siblings().fadeOut(1000);
}