2010-09-13 74 views
2

我在加載頁面時將調整圖像背景的功能,jQuery的:取消綁定加載功能

$(document).ready(function(){ 
loadBackground(); 
}); 

,我想「卸載」或「刪除」這加載功能,當我點擊一個按鈕,這樣我就可以重新加載,而不是在加載頁面的頂部的這個功能,

$('.get-image').click(function(){ 
     $(window).unbind("resize",loadBackground); 
     loadBackground(); 
return false; 
}); 

,但我不能使它工作!有什麼想法嗎?

感謝, 劉

+0

你這是什麼意思,「加載此功能重新」從那裏如何 – Pointy 2010-09-13 16:33:27

+0

這是很難猜測正確的'.unbind(「調整」)'行不能夠看到加載???你的'.bind()'或'.resize()'行。:-) – 2010-09-13 17:08:05

回答

0

根據您的意見,我不認爲你需要在所有的解除綁定:

您首先要加載的背景時,頁面加載,然後你想點擊發生時加載背景。

發生時,調整圖像的大小應該是綁定到$(window).resize()另一個單獨的函數的背景是什麼...所以整件事看起來就像這樣:

$(function() { ... });的含義一樣$(document).ready(function() { ... })它只是更快寫:

$(function() { 
     // Define load BG 
    function loadBackground() { 
     /// ... 
    } 

     // Define what happens to the BG image on resize 
     // this doesn't have to change. It should refer 
     // to the generic `background-image` 
    $(window).resize(function() { 
     // ... 
    }); 

     // load BG when page is ready: 
    loadBackground(); 

     // load another BG if there's a click: 
     // This will effectively cancel the previous load bg 
    $('.get-image').click(function() { 
     loadBackground(); 
     return false; 
    });  
}); 

// I'm assuming you're somehow changing which BG image is loaded 
// something like loadBackground(image)... and you could initially 
// load a default image, and then, if there's a click, you could 
// determine what image should be based on what was clicked. 
+0

謝謝。只是嘗試過,但它仍然工作...圖像不會再調整這個$(窗口).resize(loadBackground); loadBackground函數必須位於$(document).ready(function(){loadBackground();}); – laukok 2010-09-13 16:58:32

+1

@lauthiamkok - 這是因爲你必須創建一個對你的函數的引用,像這樣:'var loadBackground = function(){...}'...看看代碼在[jsFiddle](http ://jsfiddle.net/xTfCX/) – 2010-09-13 17:01:36

+0

@lauthimakok - 我的答案中的所有代碼都在'$(document).ready(function(){...})裏面;' – 2010-09-13 17:09:52