Tooltip Positioning

I am trying to make tooltips on the left side of the cursor instead of the right side using a jquery plugin called EasyTooltip.

I am trying to give a negative value in a header call, the purpose of which will affect the positioning on the x axis, but without effects (nothing appears, while a positive value on both axes should work fine):

<script type="text/javascript">
$(document).ready(function() {

    $(".middle img").easyTooltip({
        tooltipId: "easyTooltip2",
        xOffset: -300,
        yOffset: 50
    });
});
</script>

I am new to javascript and I think I would have to hack the script, but I need your advice here. This is the script:

/*
 *  Easy Tooltip 1.0 - jQuery plugin
 *  written by Alen Grakalic    
 *  http://cssglobe.com/post/4380/easy-tooltip--jquery-plugin
 *
 *  Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
 *  Dual licensed under the MIT (MIT-LICENSE.txt)
 *  and GPL (GPL-LICENSE.txt) licenses.
 *
 *  Built for jQuery library
 *  http://jquery.com
 *
 */

(function($) {

    $.fn.easyTooltip = function(options){

        // default configuration properties
        var defaults = {    
            xOffset: 10,        
            yOffset: 20,
            tooltipId: "easyTooltip",
            clickRemove: false,
            content: "",
            useElement: ""
        }; 

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

        this.each(function() {                  
            var title = $(this).attr("title");              
            $(this).hover(function(e){                                                                         
                content = (options.content != "") ? options.content : title;
                content = (options.useElement != "") ? $("#" + options.useElement).html() : content;
                $(this).attr("title","");                                                   
                if (content != "" && content != undefined){         
                    $("body").append("<div id='"+ options.tooltipId +"'>"+ content +"</div>");      
                    $("#" + options.tooltipId)
                        .css("position","absolute")
                        .css("top",(e.pageY - options.yOffset) + "px")
                        .css("left",(e.pageX + options.xOffset) + "px")                     
                        .css("display","none")
                        .fadeIn("fast")
                }
            },
            function(){ 
                $("#" + options.tooltipId).remove();
                $(this).attr("title",title);
            }); 
            $(this).mousemove(function(e){
                $("#" + options.tooltipId)
                    .css("top",(e.pageY - options.yOffset) + "px")
                    .css("left",(e.pageX + options.xOffset) + "px")                 
            }); 
            if(options.clickRemove){
                $(this).mousedown(function(e){
                    $("#" + options.tooltipId).remove();
                    $(this).attr("title",title);
                });             
            }
        });

    };

})(jQuery);

Thanks so much for any input. Cheers

+3
source share
3 answers

-200 , probalems, . , - , ( demo). css :

CSS

#easyTooltip{
 padding:5px 10px;
 border:1px solid #195fa4;
 background:#195fa4 url(bg.gif) repeat-x;
 color:#fff;
 position: absolute;
 top:0;
 left: -9999px;
}

Script:

:

.css("left",(e.pageX + options.xOffset) + "px")

( )

.css("left",(e.pageX - $('#'+ options.tooltipId).width() - options.xOffset - 20) + "px")

, display:none, . script , (20) , .

+3

alert((e.pageX + options.xOffset) + "px") , css $(this).hover, - NaNpx, -300, -1, -10, -100?

parseInt(options.xOffset) .

, script, .

+1

I changed js so that now it can change the x axis when it does not match the window size (but I also remove some parameters and optimized the script)

Anyway, if this can help someone:

/*
 *  Easy Tooltip 1.0 - jQuery plugin
 *  written by Alen Grakalic    
 *  adapt from http://cssglobe.com/post/4380/easy-tooltip--jquery-plugin
 *
 *  Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
 *  Dual licensed under the MIT (MIT-LICENSE.txt)
 *  and GPL (GPL-LICENSE.txt) licenses.
 *
 *  Built for jQuery library
 *  http://jquery.com
 *
 *
 *  Renew by Spektrum media  http://spektrummedia.com
 *  Now the tooltip goes to the left when it out of X scope
 *
 */

(function ($) {

    $.fn.easyTooltip = function (options) {

        // default configuration properties
        var defaults = {
            xOffset: 10,
            yOffset: 35,
            tooltipId: "easyTooltip",
            content: "",
            useElement: ""
        };

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

        this.each(function () {
            $(this).hover(function (e) {
                content = (options.useElement != "") ? $("#" + options.useElement).html() : content;
                if (content != "" && content != undefined) {
                    var $tooltip = $("<div id='" + options.tooltipId + "'>" + content + "</div>");
                    $("body").append($tooltip);

                    if ($(window).width() < (e.pageX + $tooltip.width() + options.xOffset)) {
                        $tooltip
                            .css("top", (e.pageY - options.yOffset) + "px")
                            .css("left", (e.pageX - $tooltip.outerWidth() - options.xOffset) + "px")
                            .show();
                    } else {
                        $tooltip
                            .css("top", (e.pageY - options.yOffset) + "px")
                            .css("left", (e.pageX + options.xOffset) + "px")
                            .show();
                    }
                }
            },
            function () {
                $("#" + options.tooltipId).remove();
            });
            $(this).mousemove(function (e) {
                var $tooltip = $("#" + options.tooltipId);
                if ($(window).width() < (e.pageX + $tooltip.width() + options.xOffset)) {
                    $tooltip
                            .css("top", (e.pageY - options.yOffset) + "px")
                            .css("left", (e.pageX - $tooltip.outerWidth() - options.xOffset) + "px")
                            .show();
                } else {
                    $tooltip
                            .css("top", (e.pageY - options.yOffset) + "px")
                            .css("left", (e.pageX + options.xOffset) + "px")
                            .show();
                }
            });
        });

    };

})(jQuery);

And don't forget to put this in your CSS:

#easyTooltip
{
    position: absolute;
    display: none;
}
0
source

Source: https://habr.com/ru/post/1726127/


All Articles