与jQuery中的工具提示交互

时间:2013-05-26 14:05:46

标签: javascript jquery html

如何与jQuery中的工具提示进行交互? 您知道,当您悬停<a>元素或<img>时,会出现一个小弹出窗口。 当我移动到那个标签时,我想让那个跟随我的光标。完全像this

3 个答案:

答案 0 :(得分:2)

您可能需要查看jQuery UI's tooltipQTip plugin

答案 1 :(得分:1)

鼠标跟踪工具提示的一部分:Mouse tracking

我没有尝试过,但看起来不错:one more

答案 2 :(得分:0)

这是用于自定义工具提示的简单jquery插件。 jsFiddle 您可以指定mouseFollow: true以实现跟随光标的可移动工具提示。

<强> JS

(function ($) {
    $.fn.tooltip = function (options) {

        var defaults = {
            background: '#fff',
            border: '1px solid #999',
            color: 'black',
            rounded: false,
            mouseFollow: false,
            textChangeOnClick: false
        },
        settings = $.extend({}, defaults, options);

        this.each(function () {

            var $this = $(this),
                title = null,
                hovering = null;

            //set class
            if (!settings.textChangeOnClick) {
                $this.addClass('_tooltip');
            }

            $(this).data('title', $this.attr('title'))
            $(this).attr('title', '');

            $this.hover(
                // mouse over 
                function (e) {

                //check change
                if ($this.attr('title') != '') {

                    if ($this.attr('title') != $this.data('title')) {

                        $this.data('title', $this.attr('title'));
                        $this.attr('title','');
                    }

                } else {

                    $this.removeAttr('title');
                }

                $this.attr('title', '');

                hovering = true;
                $('#tooltip').remove();

                //create box
                if ($this.data('title') != '') {

                    $('<div id="tooltip" />')
                        .appendTo('body')
                        .text($this.data('title'))
                        .hide()
                        .css({
                            backgroundColor: settings.background,
                            color: settings.color,
                            border: settings.border,
                            top: e.pageY + 10,
                            left: e.pageX + 20
                        })
                        .fadeIn(500);
                }

                if (settings.rounded) {
                    $('#tooltip').addClass('rounded');
                }

             },
             //mouse out
             function () {
                hovering = false;
                $('#tooltip').remove();
             });

            //text change
            if (settings.textChangeOnClick) {

            //on click
            $this.on('click', function () {

                if (hovering) {

                    //set new
                    $this.data('title',$(this).attr('title'));

                    $(this).attr('title', '');
                    $('#tooltip').text($this.data('title'));
                }
            });
        }

        //mouse follow
        if (settings.mouseFollow) {

            $this.mousemove(function (e) {
                $('#tooltip').css({
                    top: e.pageY + 10,
                    left: e.pageX + 20
                });
            });
        }
    });

    return this;
}


})(jQuery);

SET ELUGIN FOR ELEMENT

$('a').tooltip({
    mouseFollow: true
});

<强> HTML

<a href="#" title="Hello"></a>

<强> CSS

#tooltip
{
    border: 1px solid #BFBFBF;
    float: left;
    font-size: 11px;
    max-width: 250px;
    padding: 5px;
    position: absolute;
    color: #464646;
    z-index: 999999;
}
.rounded
{
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
}