我悬停的位置上的工具提示

时间:2012-05-31 15:24:35

标签: javascript jquery tooltip

我在我的项目中使用了this工具提示它工作得很好但问题是我需要工具提示出现在我悬停而不是固定位置的地方。目前它出现在固定位置。我需要出现工具提示我在哪里。

如何修饰此代码

  /* Position the tooltip relative to the class 
       associated with the tooltip                */  
    setTip = function(top, left){  
        var topOffset = tip.height();  
        var xTip = (left-30)+"px";  
        var yTip = (top-topOffset-60)+"px";  
        tip.css({'top' : yTip, 'left' : xTip});  
    }  

完整的代码在这里

  $.fn.betterTooltip = function(options){

/* Setup the options for the tooltip that can be 
   accessed from outside the plugin              */
var defaults = {
    speed: 200,
    delay: 300
};

var options = $.extend(defaults, options);

/* Create a function that builds the tooltip 
   markup. Then, prepend the tooltip to the body */
getTip = function() {
    var tTip = 
        "<div class='tip'>" +
            "<div class='tipMid'>"  +
            "</div>" +
            "<div class='tipBtm'></div>" +
        "</div>";
    return tTip;
}
$("body").prepend(getTip());

/* Give each item with the class associated with 
   the plugin the ability to call the tooltip    */
$(this).each(function(){

    var $this = $(this);
    var tip = $('.tip');
    var tipInner = $('.tip .tipMid');

    var tTitle = (this.title);
    this.title = "";

    var offset = $(this).offset();
    var tLeft = offset.left;
    var tTop = offset.top;
    var tWidth = $this.width();
    var tHeight = $this.height();

    /* Mouse over and out functions*/
    $this.hover(
        function() {
            tipInner.html(tTitle);
            setTip(tTop, tLeft);
            setTimer();
        }, 
        function() {
            stopTimer();
            tip.hide();
        }
    );         

    /* Delay the fade-in animation of the tooltip */
    setTimer = function() {
        $this.showTipTimer = setInterval("showTip()", defaults.delay);
    }

    stopTimer = function() {
        clearInterval($this.showTipTimer);
    }

    /* Position the tooltip relative to the class 
       associated with the tooltip                */
    setTip = function(top, left){
        var topOffset = tip.height();
        var xTip = (left-30)+"px";
        var yTip = (top-topOffset-60)+"px";
        tip.css({'top' : yTip, 'left' : xTip});
    }

    /* This function stops the timer and creates the
       fade-in animation                          */
    showTip = function(){
        stopTimer();
        tip.animate({"top": "+=20px", "opacity": "toggle"}, defaults.speed);
    }
});
    };

2 个答案:

答案 0 :(得分:4)

.hover(handlerIn,handlerOut) .mouseenter(handlerIn).mouseleave(handlerOut)的缩写。这意味着您可以访问mousenter()的鼠标坐标。

更改.hover绑定以使用悬停入口点的鼠标坐标:

/* Mouse over and out functions*/
$this.hover(
    function(e) {
        tipInner.html(tTitle);
        setTip(e.clientY, e.clientX);  //<--- using mouseenter coords here
        setTimer();
    }, 
    function() {
        stopTimer();
        tip.hide();
    }
).mousemove (function(e) {
    setTip(e.pageY, e.pageX);
});

答案 1 :(得分:2)

给它鼠标坐标而不是静态div坐标:

$(document).mousemove (function(e) {
    mouseX = e.pageX;
    mouseY = e.pageY;
});

jQuery docs:http://docs.jquery.com/Tutorials:Mouse_Position