jquery實現(xiàn)鼠標(biāo)滑過顯示提示框的方法
本文實例講述了jquery實現(xiàn)鼠標(biāo)滑過顯示提示框的方法。分享給大家供大家參考。具體如下:
一、jquery鼠標(biāo)滑過顯示提示框?qū)嵗?/strong>
1、效果圖
2、實現(xiàn)代碼 ( 需要自行添加 jquery.js、按鈕圖片、提示框圖片 )
HTML 代碼
<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="http://chabaoo.cn">Web Designer Wall</a>
<em>A wall of design ideas, web trends, and tutorials</em>
</li>
<li>
<a href="http://chabaoo.cn">Best Web Gallery</a>
<em>Featuring the best CSS and Flash web sites</em>
</li>
<li>
<a href="http://chabaoo.cn">N.Design Studio</a>
<em>Blog and design portfolio of WDW designer, Nick La</em>
</li>
</ul>
</body>
</html>
二、jquery鼠標(biāo)滑過顯示提示框?qū)嵗?/strong>
鼠標(biāo)劃過用戶名時,在鼠標(biāo)右下角顯示div展示用戶資料這個效果
1、效果圖
2、實現(xiàn)方式
無非就三大塊,一個是div的定位,這個是該效果的主要難點;二個是通過ajax異步加載數(shù)據(jù);第三個就是要用到兩個js屬性onmouseover和onmouseout,即鼠標(biāo)經(jīng)過和鼠標(biāo)離開。
3、實現(xiàn)步驟
(1)、首先設(shè)計好要顯示用戶資料的div的樣式, 這里要注意的該方法不是給每個用戶名的旁邊都綁定一個div,當(dāng)鼠標(biāo)經(jīng)過時顯示,鼠標(biāo)離開時隱藏,網(wǎng)頁里就一個顯示信息的div,哪里需要顯示時就定位在哪里,這要就需要把該div的定位方式設(shè)置為絕對定位。
HTML代碼:
<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>所屬地區(qū):</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)、相應(yīng)css代碼
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)、定位,為了能夠精確的定位并且能夠方便的調(diào)用,所以先在頁面中放了兩個標(biāo)簽,分別用于保存當(dāng)前鼠標(biāo)的坐標(biāo)
<input type="hidden" id="pagey" />
然后用js獲取當(dāng)前坐標(biāo)并保存到標(biāo)簽中:
$(document).mousemove(function (e) {
document.getElementById("pagex").value = e.pageX;//pageX() 屬性是鼠標(biāo)指針的位置,相對于文檔的左邊緣。
document.getElementById("pagey").value = e.pageY;//pageY() 屬性是鼠標(biāo)指針的位置,相對于文檔的上邊緣。
});
});
(4)、鼠標(biāo)經(jīng)過和離開事件js代碼
$("#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("未加載到數(shù)據(jù)!");
});
}
function HiddenInfo() {
$("#blockdiv").css({
"display": "none",
});
$("#lblusername").html("")
$("#lblrealname").html("");
$("#sex").html("");
$("#lbladdress").html("")
$("#lblemall").html("");
}
(5)、調(diào)用
jquery鼠標(biāo)滑過顯示提示框
</a>
希望本文所述對大家的jQuery程序設(shè)計有所幫助。
相關(guān)文章
AspNet中使用JQuery上傳插件Uploadify詳解
Uploadify是JQuery的一個上傳插件,實現(xiàn)的效果非常不錯,帶進度顯示。不過官方提供的實例時php版本的,本文將詳細(xì)介紹Uploadify在Aspnet中的使用2015-05-05基于jQuery實現(xiàn)Ajax驗證用戶名是否存在實例
這篇文章主要為大家詳細(xì)介紹了基于jQuery實現(xiàn)Ajax驗證用戶名是否存在實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-03-03jQuery插件HighCharts繪制的基本折線圖效果示例【附demo源碼下載】
這篇文章主要介紹了jQuery插件HighCharts繪制的基本折線圖效果,結(jié)合實例形式分析了jQuery基于HighCharts插件繪制圖形的具體實現(xiàn)步驟與相關(guān)操作技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-03-03使用jQuery.Validate進行客戶端驗證(初級篇) 不使用微軟驗證控件的理由
以前在做項目的時候就有個很大心病,就是微軟的驗證控件,雖然微軟的驗證控件可以幫我們完成大部分的驗證,驗證也很可靠上手也很容易,但是我就是覺得不爽.2010-06-06