【发布时间】:2014-05-11 15:58:50
【问题描述】:
我已经修改了 Visual Idiot 的 Unslider (http://unslider.com/) 以适应基于图像比例的多宽度幻灯片,但在此过程中,我遇到了一个错误,导致幻灯片加载为小缩略图(我认为在图像没有完全加载的情况)。这就是问题#1。所以我试图等到所有图像都加载完毕。但是我还没有找到成功的方法。这是问题 #2。
这是我在 Unslider init() 函数中添加的代码:
_.init = function(el, o) {
// Check whether we're passing any options in to Unslider
_.o = $.extend(_.o, o);
_.el = el;
_.ul = el.find(_.o.items);
_.max = [el.outerWidth() | 0, el.outerHeight() | 0];
_.max = [100, 100];
_.li = _.ul.find(_.o.item).each(function(index) {
var me = $(this);
var an_image = me.find('img');
an_image.load(function() {
width = an_image[0].naturalWidth,
height = an_image[0].naturalHeight;
setSlideHeight(an_image, width, height, 300, 20);
if ( an_image.parent().parent().is(":last-child") ) {
_.el.css("width", 900);
}
})
// Set the max values
//if (width > _.max[0]) _.max[0] = width;
//if (height > _.max[1]) _.max[1] = height;
});
这是我的 Ruby 部分,其中包含一个内联脚本,用于在加载所有图像时触发 Unslider。
<% if @page.cap_gallery_images.any? %>
<script>
$(document).ready(function() {
var numLoaded = 0;
var numToLoad = $('#gallery img').length;
alert("To Load: " + numToLoad);
$('#gallery img').load(function(){
numLoaded += 1;
alert("Loaded: " + numLoaded);
if (numLoaded == numToLoad) {
var gallery = $('#gallery').unslider(),
data = gallery.data('unslider');
data.to(0);
}
});
$("#slidecontainer .slide a").click(function(e) {
destination = $(this).parent().index();
data.to(destination);
e.preventDefault;
});
$(window).scroll(function() {
checkGalleryAgainstLogo();
});
$(window).resize(function() {
checkGalleryAgainstLogo();
});
checkGalleryAgainstLogo();
$('#logo.small').addClass('white');
});
</script>
<% end %>
问题在于,图像加载处理程序似乎并未全部触发。在我使用它的页面的上下文中查看它:http://penumbra-2.herokuapp.com/education
【问题讨论】:
-
即使我要“预加载”,知道图像何时完成加载的问题不是问题吗?
-
您可以通过在标题标签中预加载(HTML5 方式)来缓解此问题。我还没有使用它,但我认为 if 与预渲染、下一个 html 等在同一个文档中。
标签: jquery css ruby-on-rails