2010-07-27 63 views
2

我寫我自己的工具提示功能,像這樣到中心提示:jQuery的嘗試在懸停的元素

$(".tip_holder").hover(
    function() { 

     var $containerWidth = $(this).width(); 

     var $containerHeight = $(this).height(); 

     var $tipTxt = $(this).text(); 

     var $tipTitle = $(this).attr('title'); 

     var $offset = $(this).offset(); 

     $('#icis_dashboard').prepend('<div id="tooltip">' + $tipTxt + '</div>'); 

     $('#tooltip').css({ 
      'position': 'absolute' 
     }); 

     var $tipWidth = $('#tooltip').width(); 

     var $tipHeight = $('#tooltip').height(); 

     $('#tooltip').css({ 
      'top': $offset.top - ($tipHeight + 5), 
      'left': $offset.left, 
      'padding': '0 5px 5px'    
     }); 

    }, 
    function() { 
     $('#tooltip').remove(); 
    } 
); 

但是我有困難定心了,我正在上空盤旋元素的提示。我需要這個來擴展到任何大小的元素。

我明白,有很多插件來實現這個功能,但我想寫我自己的,所以tooltip div只會出現在代碼中的相同位置,以便它可以由我的測試人員測試。這是他的要求讓他的生活更輕鬆:(

+0

您是否正在使用最新版本的jquery&jquery-ui? – Nalum 2010-07-27 09:27:10

+0

是的。我並不認爲jQuery UI支持工具提示。 – RyanP13 2010-07-27 09:28:51

回答

4

如果您在使用jQuery的最新版本和jQuery的UI,那麼你可以使用位置工具居中刀尖元素之上。

http://jqueryui.com/demos/position/

編輯迴應作者回答

$(".tip_holder").hover(function(){ 
    var currentTipHolder = $(this); 
    var $tipTxt = $(this).text(); 
    var $tipTitle = $(this).attr('title'); 

    $('#icis_dashboard').prepend('<div id="tooltip">' + $tipTxt + '</div>'); 

    // jQueryUI position 
    $('#tooltip').position({ 
     of: currentTipHolder, 
     at: 'center top', 
     my: 'center bottom' 
    }); 
},function() { 
    $('#tooltip').remove(); 
}); 
+0

這是捆綁在覈心還是我需要下載演員? – RyanP13 2010-07-27 09:33:04

+0

它是下載頁面的ui核心部分的附加內容。 http://jqueryui.com/download – Nalum 2010-07-27 09:46:24

+0

神聖的廢話你和jQuery的搖滾。感謝這個答案! – 2011-05-12 21:17:14

-1

下似乎並沒有爲我工作:

$(".tip_holder").hover(
    function() { 

     //var $containerWidth = $(this).width(); 

     //var $containerHeight = $(this).height(); 

     var $tipTxt = $(this).text(); 

     var $tipTitle = $(this).attr('title'); 

     //var $offset = $(this).offset(); 

     $('#icis_dashboard').prepend('<div id="tooltip">' + $tipTxt + '</div>'); 

     // jQueryUI position 
     function position(using) { 

      $('#tooltip').position({ 
       of: $('.tip_holder'),     
       at: 'center top',     
       using: using 
      });  

     } 

    }, 
    function() { 
     $('#tooltip').remove(); 
    } 
); 

它現在只是在屏幕左上方顯示工具提示。

+0

更新了我上面的答案。嘗試一下那個代碼。 – Nalum 2010-07-27 10:09:51

+1

你不應該回答你自己的問題來回復別人。下一次,對他們的答案發表評論,並將代碼發佈到jsfiddle,http://www.jsfiddle.net – 2011-10-30 06:08:45

1

HTML

<div id="icis_dashboard" ></div> 
blah blah<br/> 
blah blah<br/> 

<center> 
    <div class="tip_holder" >ToolTip !!</div> 
</center> 

blah blah<br/> 
blah blah<br/> 

CSS

.tip_holder { 
    color : #0099f9; 
    font : bold 20px Arial; 
    width : 100px; 
    background:#000; 
    border:1px #f0f; 
} 
#tooltip{ 
    color:#f0f; 
    background:#2f2f2f; 
    width:200px; 
    height:20px; 
    border:1px solid #000; 
    display:none; 
    text-align:center; 
    font : bold 15px verdana; 
} 

終於有些的JavaScript

$(".tip_holder").hover(function() { 

     var $containerWidth = $(this).width(); 
     var $offset = $(this).offset(); 

     $('#icis_dashboard') 
      .prepend('<div id="tooltip">' + $(this).text() + '</div>'); 

     var $tipWidth = $('#tooltip').width(); 

     var $tipHeight = $('#tooltip').height(); 

     $('#tooltip').css({ 
      'top': $offset.top - ($tipHeight + 15), 
      'left': $offset.left - ($tipWidth - $containerWidth ) /2 , 
      'position':'absolute', 
      'display':'block' 
     }); 

     },function() { 
     $('#tooltip').remove(); 
}); 

演示http://jsfiddle.net/Nb3uW/