Heim  >  Artikel  >  Web-Frontend  >  Jquery实现鼠标移上弹出提示框、移出消失思路及代码_jquery

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

WBOY
WBOYOriginal
2016-05-16 17:33:291229Durchsuche
思路

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 = "";
        var tooltipHtml = "
" + this.myTitle + "
"; //创建提示框
        $("body").append(tooltipHtml); //添加到页面中
        $("#tooltip").css({
            "top": (e.pageY + y) + "px",
            "left": (e.pageX + x) + "px"
        }).show("fast"); //设置提示框的坐标,并显示
    }, function () {  //鼠标移出事件
        this.title = this.myTitle;  //重新设置title
        $("#tooltip").remove();  //移除弹出框
    }).mousemove(function (e) {   //跟随鼠标移动事件
        $("#toolti").css({ "top": (e.pageY + y) + "px",
            "left": (e.pageX + x) + "px"
        });
    });
});
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn