我有一个函数预加载。jQuery防止图像被点击,直到其他功能完成
我想使它成为这样,直到预加载完成,我的页面上的图像才会执行任何操作。
- 页面加载
- 预压开始 - 点击主图像上并什么
- 装载完成 - 点击主图像上会现在所说的预定功能
这怎么可能实现呢?
谢谢
我有一个函数预加载。jQuery防止图像被点击,直到其他功能完成
我想使它成为这样,直到预加载完成,我的页面上的图像才会执行任何操作。
这怎么可能实现呢?
谢谢
一旦预加载完成,您可以简单地添加单击事件侦听器。
写在回调函数你的逻辑,这样它会等待它在完成
$("p").hide(1000,function(){
alert("The paragraph is now hidden"); // write your code here
});
这应该对图像做
$(window).load(
function() {
// weave your magic here.
}
);
当页面加载时:
var image = $('mainImage');
//this is your function binded to that image
image.bind('click', function() {
//do stuff only when loaded is set to true
if ($(this).data('loaded') === true) {
/your stuff
}
});
image.data('loaded', false);
另一种方式 - 在HTML设置中,将类not-loaded
添加到您的图像。并在load
处理程序中删除该类。然后你的主图像点击处理程序必须检查,如果类设置 - 如果没有,你可以做你的东西。
这个问题已经得到解答,但在$('img.someimage').load()
不适用的情况下,这是一个更一般的方法。这使您可以指定单击处理程序,但是当一个变量代表状态设置为true,其中的逻辑只会工作:
var loader = {
isLoaded : false, // state
clickHandler : function(){
if(!loader.isLoaded) return false;
real_functionality_goes_here();
},
setLoaded : function(which){ // utility function to turn handler on/off
loader.isLoaded = !!which;
}
}
然后你必须:
$('.your_element').click(loader.clickHandler);
无论指标是代表准备,你必须运行它:
loader.setLoaded(true); // turn handler on
loader.setLoaded(!loader.isLoaded); // toggle handler on/off
我可以用一个简单的变量做到这一点,但我认为有一个更好的方式使用jQuery :) – asdf 2011-02-18 18:23:55