2010-01-10 87 views
1

我有以下問題。我正在研究一個簡單的jQuery工具提示,現在我正在處理一些對我來說很陌生的事情。每次我將鼠標懸停在元素上時,鼠標懸停和鼠標懸停的事件都會被觸發 - 所以工具提示會消失(但如果我保持交出狀態,它會在一秒內閃爍很多)。有我的代碼。jQuery懸停仍然觸發

var hint = $('<div id="hint-wrap"><div id="hintbox-top"><div id="hintbox-bottom"><div id="hintbox-topleft"><div id="hintbox-bottomleft"><div id="hint-innerwrap"><div id="hint"></div></div></div></div></div></div></div>'); // Funny I know :-D 

    hint.hide(); 
    $('body').append(hint); // I append it 

    $('.hashint').hover(function(e){ 

// Set position to cursor's 
hint.css('left', e.pageX); 
hint.css('top', e.pageY - 60); 

// animated append 
hint.css('opacity', 0.2); 
hint.show(); 
hint.animate({ 'opacity' : 1 }, 100); 

// set text 
$("#hint" , hint).html($('#' + $(this).attr('id') + '-hint').html()); 

}, 
function(){ // there is the problem 
    hint.hide(); 
    }); 

和HTML:

<div id="usernamelabel-hint" class="hinttext">Lorem Ipsum is simply dummy text of the printing and type.Lorem. <a href="#">Sign up!</a></div> <!-- This is hint text (and code) --> 
<p><label><span class="hashint" id="usernamelabel">Username</span><span class="asterisk">*</span></label> <!-- ... stuff --> </p> 

請,沒有任何人知道爲什麼鼠標離開事件仍然引發和隱藏我的盒子?

非常感謝,的Ondrej

回答

1

它可能是,你的提示,DIV被覆蓋的觸發DIV?在這種情況下,出現的tooltip-div會觸發trigger-div的mouseleave ..至少這就是我在某些div之前發生的事情。我用一個隱形的觸發器覆蓋了所有問題 - 不是很漂亮,但爲我工作。

+0

哦,你說得對!那就是問題所在。好的,thx的解決方案,但我想這將是更好的我的情況*以某種方式*使一些條件,如(如!ishover(el1)或!ishover(el2))el2.hide() – A123321 2010-01-10 16:52:12

+0

很高興聽到.. .. :] – tillinberlin 2010-01-10 17:11:34

1

首先,你爲什麼不只是把對HTML文檔中的提示的HTML?
而在CSS中,您只需設置display:none?
然後當顯示提示時,您可以使用hint.showhint.fadeIn

我只是使用$.mouseenter$.mouseleave

例子:

$.('#usernamelabel-hint').mouseenter(function() { 
    // show or fade inn hint text 
}); 

$.('#usernamelabel-hint').mouseleave(function() { 
    // Hide or fade out hinttext 
}); 
+0

好吧,我把提示框放到文檔中。 我不使用fadeIn因爲我想從opacity 0.2開始:o)無論如何,這並不重要。 而mouseenter和mouseleave事件讓我和懸停一樣困難。 (在我的例子中,必須使用$('。hasint')。mouseleave) – A123321 2010-01-10 16:43:37