2011-07-08 237 views
1

我有一個懸停效果的按鈕:懸停和鼠標同時jQuery效果

$('.donations').hover(function() { 
    $(this).find('#donate_expand_top').hide().stop(true, true).animate({ 
     height: 'toggle' 
    }, { 
     duration: 200 
    }); 

我現在需要添加同一個按鈕上點擊的效果,使得它的觸摸友好。我理解這個概念很容易使用$('.donations').click(function(){...,但問題在於將這兩種效應組合在一起無法正常工作。只要我點擊按鈕,我也徘徊,所以它試圖啓動兩個都沒有工作。

有沒有簡單的解決方案呢?

回答

1

他們的方式我已經處理它:

  1. 使用modernizr檢測,如果用戶的設備是觸摸啓用。您還可以使用任何其他方式來檢測這一點。

  2. 相應地應用您的懸停/點擊綁定。

+0

+1 - 我想我喜歡這種方法更好,儘管它可能會涉及更多細節。 – BumbleB2na

0

這還不是最完美的解決方案,但它跟蹤用戶的互動與該按鈕並觸發懸停單擊事件:

var interactingWithBtn = false; 
$('#donate_expand_top').hover(function() { 
    if(!interactingWithBtn) { 
     interactingWithBtn = true; 
     $('#donate_expand_top').trigger('click'); 
    } 
}, 
function() { 
    interactingWithBtn = false; 
}); 

$('#donate_expand_top').click(function() { 
    if(!interactingWithBtn) 
     interactingWithBtn = true; 
    $(this).find('#donate_expand_top').hide().stop(true, true).animate({ 
     height: 'toggle' 
    }, { 
     duration: 200 
    }); 
});