鼠标悬浮提示

鼠标悬浮提示

需求:添加一个帮助悬浮说明

css代码:

 .question {
            width: 20px;
            margin-left: 20px;
            padding-bottom: 4px;
        }

        .tip {
            width: 200px;
            border: 2px solid #ddd;
            padding: 8px;
            background: #f1f1f1;
            color: #666;
        }

        img {
            border: none;
        }

html代码:

<body>
    <!--这里我是用的icon问号图标,tips为要提示的内容-->
    <img id="tips" tips="我是提示!"
        class="question" src="/resource/images/question-circle.png">
</body>

js代码:

var tip = {
            $: function (ele) {
                if (typeof (ele) == "object")
                    return ele;
                else if (typeof (ele) == "string" || typeof (ele) == "number")
                    return document.getElementById(ele.toString());
                return null;
            },
            mousePos: function (e) {
                //获取提示展示的坐标
                var e = e || window.event;
                return {
                    x: e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft,
                    y: e.clientY + document.body.scrollTop + document.documentElement.scrollTop
                };
            },
            start: function (obj) {
                //动态添加一个div作为提示,可以定义id为tip-prompt的全局样式
                $('body').append('<div id="tip-prompt" class="tip" style="position:fixed;left:0;top:0;display: none"></div>');
                var self = this;
                var t = $("#tip-prompt");
                //鼠标移入事件
                obj.mouseenter(function () {
                    var mouse = self.mousePos();
                    t.css("left", mouse.x + 10 + 'px');
                    t.css("top", mouse.y + 10 + 'px');
                   //获取提示消息,这里为了支持格式,使用的html
                    t.html($(this).attr("tips"));
                    t.css("display", "block");
                    t.css("z-index", "99999");
                })
                //鼠标移除事件
                obj.mouseleave(function () {
                    t.css("display", "none");
                })
            }
        }
$(function(){
   //启用提示
    tip.start($(".question"))
})    

备注:这里还有很多可能需要处理的细节,如宽高如果内容过多,或者目标元素在边角的处理。

代码参考别人博客DIV+js+css跟随鼠标的浮动提示框:

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

niubility锐

觉得有用的话鼓励鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值