2

我有一个功能可以在页面加载时调整图像背景的大小,

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

当我点击一个按钮时,我想“卸载”或“删除”这个加载的函数,这样我就可以重新加载这个函数,而不是把它加载到页面顶部,

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

但我不能让它工作!请问有什么想法吗?

谢谢,刘

4

1 回答 1

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.
于 2010-09-13T16:30:47.483 回答