2012-07-28 22 views
0

我有以下代碼:jQuery的 - 解除綁定不會重新結合

$homeSlider.mouseenter(function() { 
    console.log('enter'); 
    $slideInfo.animate({ 
     'bottom': -slideInfoHeight + 'px' 
    }); 
}); 
$homeSlider.mouseleave(function() { 
    console.log('leave'); 
    $slideInfo.animate({ 
     'bottom': '0px' 
    }); 
}); 
$slideInfo.mouseenter(function() { 
    $homeSlider.unbind('mouseenter'); 
    $homeSlider.unbind('mouseleave'); 
}); 
$slideInfo.mouseleave(function() { 
    $homeSlider.bind('mouseenter'); 
    $homeSlider.bind('mouseleave'); 
}) 

我slideinfo DIV絕對定位在的homeSlider DIV的一部分頂部。如果您滾過homeSlider,則slideInfo會隱藏自身(-slideInfoHeight),並在您推出時顯示其自身。如果將鼠標移動到slideInfo div上,它將適當地保持可見狀態,並在您展開時保持可見狀態。但是,當您回滾homeSlider時,它不會再隱藏slideInfo。我究竟做錯了什麼?

+0

您能向我們展示DOM嗎? – Bergi 2012-07-28 15:37:25

+1

未綁定的處理程序丟失。你不能簡單地在沒有函數的情況下調用['bind()'](http://api.jquery.com/bind/)。 – Bergi 2012-07-28 15:38:13

+0

啊 - 謝謝 - 是的,只需要添加功能 – mheavers 2012-07-28 15:38:48

回答

0

我建議你使用一個變量,而不是一次可連續綁定和解除綁定的:

var preventAnimation = false; 
$homeSlider.mouseenter(function() { 
    if (preventAnimation) return; 
    console.log('enter'); 
    $slideInfo.animate({ 
     'bottom': -slideInfoHeight + 'px' 
    }); 
}); 
$homeSlider.mouseleave(function() { 
    if (preventAnimation) return; 
    console.log('leave'); 
    $slideInfo.animate({ 
     'bottom': '0px' 
    }); 
}); 
$slideInfo.mouseenter(function() { 
    preventAnimation = true; 
}); 
$slideInfo.mouseleave(function() { 
    preventAnimation = false; 
}) 

而且,你可能看看到.hover()