首页 > 其他分享 >tooltip

tooltip

时间:2023-05-29 09:35:31浏览次数:46  
标签:option px tooltip position left top angleSize

(function ($)
{
    $.fn.tooltip = function (option)
    {
        /*
        * 提示框jq插件
        * Author by 秋叶博客 http://www.mizuiren.com/494.html
        * option参数
        * {
        * background: "#ddd",(option可选)
        * position: "top|left|bottom|right",(option可选)
        * content: "这是实例提示文字!",(needed必选)
        * event: "hover|click",(event type事件类型, 可选)
        * color: "#3c4751"(font color文字颜色)
        * borderColor: "#ddd",(color 颜色 option可选)
        * fontSize: "14px",(font size 字号 option可选)
        * width:"200px",(width 宽度 option可选)
        * height:"auto"(height 高度 option可选)
        * }
        *
        */
        if (!$.isPlainObject(option))
        {
            return console.error("tooltip传参类型居然不是个纯对象!");
        }
        var _this = this, $tipContent;
        option.borderColor = option.borderColor || "#ddd";
        option.color = option.color || "#444";
        option.fontSize = option.fontSize || "14px";
        option.width = option.width || "auto";
        option.height = option.height || "auto";
        option.background = option.background || "#fff";
        function createTip(_this)
        {
            $tipContent && $tipContent.remove();
            var timeId = "qiuye-tooltip-" + new Date().getTime();
            $("body").append('<div id="' + timeId + '" style="display:none;"><div class="qiuye-con"></div><i></i><i></i><style>#' + timeId + '{z-index:9999;border-radius:4px;position: absolute; padding: 5px 10px; display: inline-block;width: ' + option.width + ';height: ' + option.height + '; min-width: 70px;text-align:center; word-break: break-all; background: rgb(255, 255, 255); border: 1px solid ' + option.borderColor + ';font-size:' + option.fontSize + ';color:' + option.color + ';} #' + timeId + ' i{position:absolute;display:inline-block;}</style></div>');
            $tipContent = $("#" + timeId);
            var $angle = $("i", $tipContent), $angle1 = $angle.eq(0), $angle2 = $angle.eq(1), $angleSize = 8;
            $(".qiuye-con", $tipContent).html(option.content || _this.attr("title") || "");
            var eleVar = {
                width: _this.outerWidth(),
                height: _this.outerHeight(),
                left: _this.offset().left,
                top: _this.offset().top
            }
            var tipVar = {
                width: $tipContent.outerWidth(),
                height: $tipContent.outerHeight()
            }
            var position = {}, offset = $angleSize * 2;
            //Set tip container position and style(设置消息提示框的位置以及样式)
            if (!option.position)
            {
                option.position = "top";
            }
            switch (option.position)
            {
                case "top":
                    position.top = eleVar.top - tipVar.height - offset + "px";
                    position.left = eleVar.left - tipVar.width / 2 + eleVar.width / 2 + "px";
                    break;
                case "bottom":
                    position.top = eleVar.top + eleVar.height + offset + "px";
                    position.left = eleVar.left - tipVar.width / 2 + eleVar.width / 2 + "px";
                    break;
                case "left":
                    position.top = eleVar.top + eleVar.height / 2 - tipVar.height / 2 + "px";
                    position.left = eleVar.left - tipVar.width - offset + "px";
                    break;
                case "right":
                    position.top = eleVar.top + eleVar.height / 2 - tipVar.height / 2 + "px";
                    position.left = eleVar.left + eleVar.width + offset + "px";
                    break;
                default:
                    position.top = eleVar.top - tipVar.height - offset + "px";
                    position.left = eleVar.left - tipVar.width / 2 + eleVar.width / 2 + "px";
                    break;
            }
            position.background = option.background;
            $tipContent.css(position);
            //Set angle position and style(设置指示三角的位置以及样式)
            var angleStyle1 = {}, angleStyle2 = {};
            if (option.position === "left")
            {
                angleStyle1.right = "-" + ($angleSize) + "px";
                angleStyle2.right = "-" + ($angleSize - 1) + "px";
            } else if (option.position === "right")
            {
                angleStyle1.left = "-" + ($angleSize) + "px";
                angleStyle2.left = "-" + ($angleSize - 1) + "px";
            } else if (option.position === "top")
            {
                angleStyle1.bottom = "-" + ($angleSize) + "px";
                angleStyle2.bottom = "-" + ($angleSize - 1) + "px";
            } else if (option.position === "bottom")
            {
                angleStyle1.top = "-" + ($angleSize) + "px";
                angleStyle2.top = "-" + ($angleSize - 1) + "px";
            }
            if (option.position === "left" || option.position === "right")
            {
                $angle.css({
                    "top": "50%"
                });
                $angle1.css($.extend({
                    "margin-top": "-" + $angleSize + "px",
                    "border-top": $angleSize + "px solid transparent",
                    "border-bottom": $angleSize + "px solid transparent"
                }, angleStyle1, option.position === "left" ? {
                    "border-left": $angleSize + "px solid " + option.borderColor
                } : {
                    "border-right": $angleSize + "px solid " + option.borderColor,
                }));
                $angle2.css($.extend({
                    "margin-top": "-" + ($angleSize - 1) + "px",
                    "border-top": $angleSize - 1 + "px solid transparent",
                    "border-bottom": $angleSize - 1 + "px solid transparent"
                }, angleStyle2, option.position === "left" ? {
                    "border-left": $angleSize - 1 + "px solid " + option.background
                } : {
                    "border-right": $angleSize - 1 + "px solid " + option.background
                }));
            } else if (option.position === "top" || option.position === "bottom")
            {
                $angle.css({
                    "left": "50%"
                });
                $angle1.css($.extend({
                    "margin-left": "-" + $angleSize + "px",
                    "border-left": $angleSize + "px solid transparent",
                    "border-right": $angleSize + "px solid transparent"
                }, angleStyle1, option.position === "top" ? {
                    "border-top": $angleSize + "px solid " + option.borderColor
                } : {
                    "border-bottom": $angleSize + "px solid " + option.borderColor
                }));
                $angle2.css($.extend({
                    "margin-left": "-" + ($angleSize - 1) + "px",
                    "border-left": $angleSize - 1 + "px solid transparent",
                    "border-right": $angleSize - 1 + "px solid transparent"
                }, angleStyle2, option.position === "top" ? {
                    "border-top": $angleSize + "px solid " + option.background
                } : {
                    "border-bottom": $angleSize + "px solid " + option.background
                }));
            }
        }
        //Bind event
        if (option.event)
        {
            $(window).on(option.event + ".qiuye", function (e)
            {
                e = e || window.event;
                var index = Array.prototype.slice.call(_this).indexOf(e.target);
                if (index > -1)
                {
                    createTip($(_this[index]));
                    $tipContent.fadeIn(200);
                } else
                {
                    $tipContent && $tipContent.remove();
                }
            });
        
        //    _this.hover(function ()
        //    {
        //    createTip($(_this));
        //    $tipContent.fadeIn(200);
        //    }, function ()
        //    {
        //        $tipContent && $tipContent.remove();
        //    })
        }
        else
        {
            Array.prototype.slice.call(_this).forEach(function (item, index)
            {
                createTip($(item));
                $tipContent.fadeIn(200);
            })
        }
        return _this;
    }
}(jQuery));






//<a id="qiuye-shang">鼠标悬浮上面显示提示</a>
//$("#qiuye-shang").tooltip({
//    position: "top",
//    content: '这里放文本',
//    background: "#3c4751",
//    event: "hover",
//    color: "yellow"
//});







标签:option,px,tooltip,position,left,top,angleSize
From: https://www.cnblogs.com/0624zfz/p/17439496.html

相关文章

  • 折线图 最大值显示白点,默认显示tooltip框框,自定义tooltip
    效果图: 代码如下:(直接放整个vue文件了,省事)<template><div><divstyle="width:406px;height:220px"id="zz_r_two1"></div><divclass="xsTitle"><divclass="fang"></div>......
  • 【Echarts】tooltip自定义提示框
    1组件可直接使用。2tooltip:{3show:true,4trigger:'axis',5confine:true,6padding:0,7borderWidth:0,8backgroundColor:'rgba(1,1,1,0)',9//axisPointer:{10......
  • react echarts tooltip 区域新加一个输入框,可以编辑保存数据
     //demo页面//需求:产品要求在折线图的tooltip上新加一个输入框,可以编辑这个输入框保存备注信息,需要两种交互方式:1.鼠标滑过展示备注信息。2.鼠标点击某一个日期时,鼠标可以滑到tooltip上做保存/编辑操作。//思路:1.保留初始鼠标滑过echarts图效果。//2.主要难点是点击时to......
  • 动态改变tooltip背景及动态回显tooltip
    废话不说,看网上那些大聪明写的都是echart上的基本的配置,真好笑tooltip:{backgroundColor:"transparent",//修改背景颜色borderColor:"transparent",//修改边框颜色padding:0,formatter:function(e){console.log('e,:>>',e,)//这里,如果数......
  • echarts 数据密集,如果设置sampling: 'average' 会导致提示框(tooltip)无法正常显示,但是
    如果数据比较密集,设置sampling:'average'确实可以加速绘图,但同时也可能导致提示框无法正常显示的问题。这个问题的原因是,sampling会对数据进行抽样,因此不会显示原始的数据点,而是将数据点以一定规律进行采样,取平均值或最大或其他值,因此提示框的内容可能不准确。不过,有一个简单的......
  • antd 表单里加tooltip不生效问题
    antd的form表单的label后面加一个问号icon,hover展示提示,但是加进去之后,hover不提示,最后发现原因是因为labelCol,不清楚为啥,反正去掉就能提示,加上就不提示,所以最后把这个labelCol去掉了,自己在CSS里写的样式 修改antdform表单自己的样式,需要在global.less文件里写  ......
  • element Plus 中el-tooltip 和 el-popover超出宽度显示文字提示 否则不提示
    说明:设置固定宽度。文字超出宽度,用...显示。鼠标悬停到文字上,用el-tooltip显示全部文字内容如果文字未超出宽度,el-tooltip、el-popover隐藏。html:1<el-tooltip:content="node.label"placement="bottom"effect="light":disabled="isShowTooltip">2<span......
  • echarts如何在x轴的tooltip上加上描述文字
     想要实现的效果,如下图: 实现该操作的代码如下:1tooltip:{2formatter:"month:{b0}<br>"34}实现该效果的实例图如下:   ......
  • echarts中的tooltip属性
     echarts鼠标放上去显示提示框属性详解tooltip tooltip={//提示悬浮框框组件trigger:'item',......
  • ElementUi使用el-tooltip实现超出部分显示省略号
    效果前:效果后:创建myTooltio.vue组件<template><divclass="tooltip-container"><el-tooltipclass="my-tooltip":disabled="showTooltip":content="t......