当前位置:主页 > 网页前端 > jquery >

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

时间:2022-03-03 09:47:13 | 栏目:jquery | 点击:

思路

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; });
});

您可能感兴趣的文章:

相关文章