Jquery实现鼠标移上弹出提示框、移出消失思路及代码_技术学院_宜昌市隼壹珍商贸有限公司

您好,欢迎访问宜昌市隼壹珍商贸有限公司

400 890 5375
当前位置: 主页 > 新闻动态 > 技术学院

Jquery实现鼠标移上弹出提示框、移出消失思路及代码

发布时间:2026-01-17  |  点击率:
思路

1.首先要定位实现这种效果的元素 ,本次通过class

2.如果是动态显示不同的提示内容,需设置title

3.通过JQ给定位到元素加上 mouseover 和mouseout 事件

4.再完善下,弹出框跟随鼠标在目标元素上移动

5.再把 mouseover 、mouseout 合并成 hover
复制代码 代码如下:
//页面加载完成
$(function () {
    var x = 10;
    var y = 20; //设置提示框相对于偏移位置,防止遮挡鼠标
    $(".prompt").hover(function (e) {  //鼠标移上事件
        this.myTitle = this.title; //把title的赋给自定义属性 myTilte ,屏蔽自带提示
        this.title = "";
&nbsp; &nbsp; &nbsp; &nbsp; var tooltipHtml = "<div id='tooltip'>" + this.myTitle + "</div>"; //创建提示框
&nbsp; &nbsp; &nbsp; &nbsp; $("body").append(tooltipHtml); //添加到页面中
&nbsp; &nbsp; &nbsp; &nbsp; $("#tooltip").css({
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "top": (e.pageY + y) + "px",
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "left": (e.pageX + x) + "px"
&nbsp; &nbsp; &nbsp; &nbsp; }).show("fast"); //设置提示框的坐标,并显示
&nbsp; &nbsp; }, function () { &nbsp;//鼠标移出事件
&nbsp; &nbsp; &nbsp; &nbsp; this.title = this.myTitle; &nbsp;//重新设置title
&nbsp; &nbsp; &nbsp; &nbsp; $("#tooltip").remove(); &nbsp;//移除弹出框
&nbsp; &nbsp; }).mousemove(function (e) { &nbsp; //跟随鼠标移动事件
&nbsp; &nbsp; &nbsp; &nbsp; $("#toolti").css({ "top": (e.pageY + y) + "px",
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "left": (e.pageX + x) + "px"
&nbsp; &nbsp; &nbsp; &nbsp; });
&nbsp; &nbsp; });
});

全国统一服务电话

400 890 5375

电子邮箱:879577@qq.com

公司地址:宜昌市西陵区黄河路5号三峡明珠10栋1051室

咨询微信

TEL:13680874598