jQuery实现鼠标悬停显示提示框的两种方法

作者:竹影时间:2026-10-29 05:22:33浏览:499

本文介绍两种使用jQuery实现鼠标悬停显示提示框的方法。第一种通过CSS定位与animate动画实现菜单项悬停效果;第二种结合绝对定位、鼠标事件及AJAX异步加载,实现鼠标划过用户名时动态显示用户详细资料。

本文分享利用jQuery实现鼠标悬停显示提示框的技术方案,供开发者参考。

jQuery菜单悬停提示框实现

一、jQuery鼠标悬停显示提示框实例

1、效果展示

jQuery菜单悬停提示框实现

2、实现代码(需自行引入jquery.js及背景图片)

HTML结构

代码如下:

<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Animated Menu Hover 1</title>
<script type="text/javascript" src="jquery。js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".menu li").hover(function() {
$(this).find("em").animate({opacity: "show", top: "-75"}, "slow");
}, function() {
$(this).find("em").animate({opacity: "hide", top: "-85"}, "fast");
 });
});
</script>
<style type="text/css">
body {
 margin: 10px auto;
 width: 570px;
 font: 75%/120% Arial, Helvetica, sans-serif;
}
.menu {
 margin: 100px 0 0;
 padding: 0;
 list-style: none;
}
.menu li {
 padding: 0;
 margin: 0 2px;
 float: left;
 position: relative;
 text-align: center;
}
.menu a {
 padding: 14px 10px;
 display: block;
 color: #000000;
 width: 144px;
 text-decoration: none;
 font-weight: bold;
 background: url('背景图片1') no-repeat center center;
}
.menu li em {
 background: url('背景图片2') no-repeat;
 width: 180px;
 height: 45px;
 position: absolute;
 top: -85px;
 left: -15px;
 text-align: center;
 padding: 20px 12px 10px;
 font-style: normal;
 z-index: 2;
 display: none;
}
</style>
</head>
<body>
<ul class="menu">
 <li>
 <a href="https://www.jb51.net">Web Designer Wall</a> 
 <em>A wall of design ideas, web trends, and tutorials</em>
 </li>
 <li>
 <a href="https://www.jb51.net">Best Web Gallery</a>
 <em>Featuring the best CSS and Flash web sites</em>
 </li>
 <li>
 <a href="https://www.jb51.net">N.Design Studio</a>
 <em>Blog and design portfolio of WDW designer, Nick La</em>
 </li>
</ul>
</body>
</html>

jQuery用户资料动态提示框实现

二、jQuery鼠标悬停显示提示框实例二

实现鼠标划过用户名时,在鼠标右下角显示div以展示用户资料的效果。

1、效果展示

jQuery菜单悬停提示框实现

2、实现思路

主要包含三个部分:首先是div的定位,这是该效果的核心难点;其次是通过ajax异步加载数据;最后是利用js的onmouseover和onmouseout属性,分别处理鼠标经过和离开的行为。 3、实现步骤

(1)、首先设计好要显示用户资料的div的样式, 这里要注意的该方法不是给每个用户名的旁边都绑定一个div,当鼠标经过时显示,鼠标离开时隐藏,网页里就一个显示信息的div,哪里需要显示时就定位在哪里,这要就需要把该div的定位方式设置为绝对定位。 HTML代码:

代码如下:

<div class="blockdiv" id="blockdiv">
<div class="pic">
<img src="../../Users/images/899。png" id="imguserhead" />
</div>
<div class="box">
<table width="220" border="0" style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap">
<tr>
<td style="width: 70px;">用户名:</td>
<td>
<label id="lblusername"></label>
</td>
</tr>
<tr>
<td>真实姓名:</td>
<td>
<label id="lblrealname"></label>
</td>
</tr>
<tr>
<td>性别:</td>
<td>
<label id="sex"></label>
</td>
</tr>
<tr>
<td>所属地区:</td>
<td>
<label id="lbladdress"></label>
</td>
</tr>
<tr>
<td>邮箱:</td>
<td>
<label id="lblemall"></label>
</td>
</tr>
</table>
<div style="text-align: left; color: green; line-height: 40px; height: 30px; display: none;" id="messagediv ">正在加载中...</div>
</div>
</div>

(2)、相应css代码

代码如下:

#blockdiv{
width:380px;
height:160px;
float:left;
display:none;
border: 1px solid #ccc; position: absolute; z-index: 1; opacity: 0.1; background: white
}
.pic{
width:100px;
height:100px;
border:1px solid #ccc;
border-radius:10px;
float:left; margin:10px;
overflow:hidden;
}
.box{
width:240px;
height:140px;
margin:10px 0 10px 10px;
float:left;
overflow:hidden;text-overflow:ellipsis; white-space:nowrap;}

(3)、定位,为了能够精确的定位并且能够方便的调用,所以先在页面中放了两个标签,分别用于保存当前鼠标的坐标

代码如下:

<input type="hidden" id="pagex" />
<input type="hidden" id="pagey" />

然后用js获取当前坐标并保存到标签中:

代码如下:

jQuery(document).ready(function ($) {
$(document).mousemove(function (e) {
document.getElementById("pagex").value = e.pageX;//pageX() 属性是鼠标指针的位置,相对于文档的左边缘。
document.getElementById("pagey").value = e.pageY;//pageY() 属性是鼠标指针的位置,相对于文档的上边缘。
    });
});

(4)、鼠标经过和离开事件js代码

代码如下:

function ShowInfo(username) {
$("#blockdiv").css({
"display": "block",
"left": document.getElementById('pagex').value,
"top": document.getElementById('pagey').value,
});
$("#messagediv").css("display", "block");
$.getJSON("../ashx/GetUserInfo。ashx?name=" + username,
function (data) {
if (data != null) {
$("#lblusername").html(data[0].User_Count)
$("#lblrealname").html(data[0].User_name);
$("#sex").html(data[0].User_Sex);
$("#lbladdress").html(data[0].User_Address)
$("#lblemall").html(data[0].User_Email);
if (data[0].User_HeadImg != null&&data[0].User_HeadImg != "") {
$("#imguserhead").attr("src", "../../Users/" + data[0].User_HeadImg.toString().substring(3));
  }
else {
$("#imguserhead").attr("src", "../../Users/images/900.png");
  }
$("#messagediv").css("display", "none");
     }
else
$("#messagediv").html("未加载到数据!");
 });
}
function HiddenInfo() {
$("#blockdiv").css({
"display": "none",
    });
$("#lblusername").html("")
 $("#lblrealname").html("");
 $("#sex").html("");
 $("#lbladdress").html("")
 $("#lblemall").html("");
}

(5)、调用

代码如下:

<a class="showuserinfo" onmouseover="ShowInfo('<%#Eval("Response_Person") %>')" onmouseout="HiddenInfo()">
jquery鼠标滑过显示提示框
</a>

希望本文所述对大家的jQuery程序设计有所帮助。

作者最新文章