设为首页收藏本站关注微信<<< 抵制 IE6 人人有责 >>>
搜索
热搜: 活动 交友 discuz
查看: 1986|回复: 1

[Jquery] jquery实现鼠标滑过显示提示框

[复制链接]
发表于 2017-2-8 10:07:48 | 显示全部楼层 |阅读模式
这篇文章主要介绍了jquery实现鼠标滑过显示提示框的方法,以两个不同实例形式分析了jQuery鼠标滑过显示提示框的实现技巧与功能代码,非常具有实用价值,需要的朋友可以参考下

本文实例讲述了jquery实现鼠标滑过显示提示框的方法。分享给大家供大家参考。具体如下:

一、jquery鼠标滑过显示提示框实例

1、效果图


2、实现代码 ( 需要自行添加  jquery.js、按钮图片、提示框图片  )

HTML 代码

[HTML] 纯文本查看 复制代码
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<script src="http://www.kevin-ying.com/jq/jquery-2.1.4.min.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="http://www.kevin-ying.com">Web Designer Wall</a> 
  <em>A wall of design ideas, web trends, and tutorials</em>
 </li>
 <li>
  <a href="http://www.kevin-ying.com">Best Web Gallery</a>
  <em>Featuring the best CSS and Flash web sites</em>
 </li>
 <li>
  <a href="http://www.kevin-ying.com">N.Design Studio</a>
  <em>Blog and design portfolio of WDW designer, Nick La</em>
 </li>
</ul>
</body>
</html>

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?免费注册

x
回复

使用道具 举报

 楼主| 发表于 2017-2-8 10:13:37 | 显示全部楼层

二、jquery鼠标滑过显示提示框实例二

鼠标划过用户名时,在鼠标右下角显示div展示用户资料这个效果

1、效果图


2、实现方式

无非就三大块,一个是div的定位,这个是该效果的主要难点;二个是通过ajax异步加载数据;第三个就是要用到两个js属性onmouseover和onmouseout,即鼠标经过和鼠标离开。

3、实现步骤

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

HTML代码:

代码如下:
[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代码
代码如下:

[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)、定位,为了能够精确的定位并且能够方便的调用,所以先在页面中放了两个标签,分别用于保存当前鼠标的坐标
代码如下:

[HTML] 纯文本查看 复制代码
<input type="hidden" id="pagex" />
<input type="hidden" id="pagey" />



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

[JavaScript] 纯文本查看 复制代码
jQuery(document).ready(function ($) {
$(document).mousemove(function (e) {
 document.getElementById("pagex").value = e.pageX;//pageX() 属性是鼠标指针的位置,相对于文档的左边缘。
 document.getElementById("pagey").value = e.pageY;//pageY() 属性是鼠标指针的位置,相对于文档的上边缘。
    });
});



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


[JavaScript] 纯文本查看 复制代码
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)、调用
代码如下:

[HTML] 纯文本查看 复制代码
<a class="showuserinfo"Response_Person") %>')">
jquery鼠标滑过显示提示框
</a>




all code:
[HTML] 纯文本查看 复制代码
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
#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;}
</style>
<script src="http://www.kevin-ying.com/jq/jquery-2.1.4.min.js"></script>
<script>
jQuery(document).ready(function ($) {
$(document).mousemove(function (e) {
 document.getElementById("pagex").value = e.pageX;//pageX() 属性是鼠标指针的位置,相对于文档的左边缘。
 document.getElementById("pagey").value = e.pageY;//pageY() 属性是鼠标指针的位置,相对于文档的上边缘。
    });
});

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("");
}
</script>

</head>

<body>
<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>

<input type="hidden" id="pagex" />
<input type="hidden" id="pagey" />
<a class="showuserinfo">
jquery鼠标滑过显示提示框
</a>
</body>
</html>


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?免费注册

x
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

QQ|手机版|Archiver|源于生活(个人生活娱乐工作的笔记)css3,html5,学习笔记    

GMT+8, 2024-3-28 21:07 , Processed in 0.109200 second(s), 30 queries .

Powered by Mr.Kevin-ying

© 2004-2015

快速回复 返回顶部 返回列表