加载图片/内容后从外部URL的内容
我想从另一个URL使用pushstate/html5加载内容...我想加载的内容有一个大的背景图片,需要时间加载...加载图片/内容后从外部URL的内容
所以,当动画快满形象似乎要拿出和幻灯片中,但随后又形象重新加载...
我尝试使用
image preloader/
$(document).ready()
他们都打破了脚本,不能完全似乎弄清楚如何在这里加入它
function goTo(href) {
var left = $(window).width();
$('#id').css("left", left);
$.ajax({
url: href,
success: function (data) {
var content = $(data).find('#id').html();
// Windows Load Function
$(window).load(function() {
$("#id").html(content).animate({
left: '0',
}, 'fast');
var title = $('#id').find('h1').text();
$('head').find('title').text(title);
});
}
});
}
// check for support before we move ahead
if (typeof history.pushState !== "undefined") {
var historyCount = 0;
// On click of link Load content and animate
$('.access a').live('click', function() {
var href = $(this).attr('href');
goTo(href);
history.pushState(null, null, href);
return false;
});
window.onpopstate = function() {
if (historyCount) {
goTo(document.location);
}
historyCount = historyCount + 1;
};
}
蒙特克里斯托,
我觉得这是最好的解决Deferreds。
在下面的代码中,将一个Deferred对象作为第二个参数animator
传递给goTo()
。当imgSelector
级别的所有图像都可疑时,此延迟将在goTo()
之内解决。在解析animator
时,动画会触发并且页面的标题被更改。
从外部创建animator
并将它传递给goTo()
允许将此Deferred从调用goTo()
的范围中拒绝。如果在动画触发之前发生另一次点击或弹出事件,则会包含此功能以禁止动画。
为了完成这项工作,您需要为每个内容页面提供一个假屏幕前景<img>
,其中src
与有问题的大背景图像和class="critical"
相同。这是加载背景图像时触发事件的最简单方法。
$(function() {
var $id = $("#id");
var imgClass = 'critical';
function goTo(href, animator) {
$id.stop().css("left", $(window).width());
$.ajax({
url: href,
success: function(data) {
var deferreds = [];
$id.html($(data).find('#id').html());
$("img." + imgClass, $id).each(function() {
var d = new $.Deferred();
deferreds.push(d);
$(this).on('load error', function(){
d.resolve();
});
});
$.when.apply(null, deferreds).done(function() {
animator.resolve();
});
$.when(animator).done(function() {
$id.animate({
left: '0',
}, 'fast');
$('head title').text($('h1', $id).text());
});
}
});
}
var historyCount = 0,
animator = null;
function getDeferred() {
if(animator) {
animator.reject();
}
animator = new $.Deferred();
return animator;
};
if (typeof history.pushState !== "undefined") {
$('.access a').live('click', function() {
var href = $(this).attr('href');
goTo(href, getDeferred());
history.pushState(null, null, href);
return false;
});
window.onpopstate = function() {
if(historyCount) {
goTo(document.location, getDeferred());
}
historyCount++;
};
}
});
未经检验
您可能需要调试和/或修改代码以得到你想要的东西。
你需要$(window).load()
宁可$(document).ready()
如果要等到所有的内容和图片都加载之后。
所以,你可能想是这样的:
$(window).load(function() {
$('some element').css('background-image', 'url(big-background-image-file)');
});
这是我做了什么,不工作?我有一种感觉,我在这里做错了什么? http://pastebin.com/BDM5ZNuV – MonteCristo 2012-08-01 20:11:56
@MonteCristo:说实话,我不知道那个pastebin里发生了什么。你能修整缩进吗?我无法跟踪哪些功能在哪里... – KRyan 2012-08-01 20:22:44
对不起...希望现在好点? http://pastebin.com/YBHUahWN – MonteCristo 2012-08-01 20:27:32
什么是'图像预加载/行'应该是?我怀疑,如果没有别的东西,就会把这些作品搞砸了。不要以为JavaScript会接受。 – KRyan 2012-08-01 20:04:18
在这种情况下,“href”是什么?它不是外在的吗? – 2012-08-01 20:06:43
抱歉,我的意思是说我试过preloader方法...我不知道我在哪里把preloader在 – MonteCristo 2012-08-01 20:09:05