jquery實(shí)現(xiàn)鼠標(biāo)滑過(guò)顯示提示框的方法
本文實(shí)例講述了jquery實(shí)現(xiàn)鼠標(biāo)滑過(guò)顯示提示框的方法。分享給大家供大家參考。具體如下:
一、jquery鼠標(biāo)滑過(guò)顯示提示框?qū)嵗?/strong>
1、效果圖
2、實(shí)現(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://www.dbjr.com.cn">Web Designer Wall</a>
<em>A wall of design ideas, web trends, and tutorials</em>
</li>
<li>
<a href="http://www.dbjr.com.cn">Best Web Gallery</a>
<em>Featuring the best CSS and Flash web sites</em>
</li>
<li>
<a href="http://www.dbjr.com.cn">N.Design Studio</a>
<em>Blog and design portfolio of WDW designer, Nick La</em>
</li>
</ul>
</body>
</html>
二、jquery鼠標(biāo)滑過(guò)顯示提示框?qū)嵗?/strong>
鼠標(biāo)劃過(guò)用戶名時(shí),在鼠標(biāo)右下角顯示div展示用戶資料這個(gè)效果
1、效果圖
2、實(shí)現(xiàn)方式
無(wú)非就三大塊,一個(gè)是div的定位,這個(gè)是該效果的主要難點(diǎn);二個(gè)是通過(guò)ajax異步加載數(shù)據(jù);第三個(gè)就是要用到兩個(gè)js屬性onmouseover和onmouseout,即鼠標(biāo)經(jīng)過(guò)和鼠標(biāo)離開。
3、實(shí)現(xiàn)步驟
(1)、首先設(shè)計(jì)好要顯示用戶資料的div的樣式, 這里要注意的該方法不是給每個(gè)用戶名的旁邊都綁定一個(gè)div,當(dāng)鼠標(biāo)經(jīng)過(guò)時(shí)顯示,鼠標(biāo)離開時(shí)隱藏,網(wǎng)頁(yè)里就一個(gè)顯示信息的div,哪里需要顯示時(shí)就定位在哪里,這要就需要把該div的定位方式設(shè)置為絕對(duì)定位。
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>真實(shí)姓名:</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)用,所以先在頁(yè)面中放了兩個(gè)標(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)指針的位置,相對(duì)于文檔的左邊緣。
document.getElementById("pagey").value = e.pageY;//pageY() 屬性是鼠標(biāo)指針的位置,相對(duì)于文檔的上邊緣。
});
});
(4)、鼠標(biāo)經(jīng)過(guò)和離開事件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)滑過(guò)顯示提示框
</a>
希望本文所述對(duì)大家的jQuery程序設(shè)計(jì)有所幫助。
相關(guān)文章
基于jQuery實(shí)現(xiàn)仿百度首頁(yè)選項(xiàng)卡切換效果
這篇文章主要介紹了基于jQuery實(shí)現(xiàn)仿百度首頁(yè)選項(xiàng)卡切換效果的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-05-05解析ajaxFileUpload 異步上傳文件簡(jiǎn)單使用
本篇文章主要介紹了ajaxFileUpload 異步上傳文件簡(jiǎn)單使用,jQuery插件AjaxFileUpload可以實(shí)現(xiàn)ajax文件上傳。有興趣的可以了解一下,2016-12-12jquery實(shí)現(xiàn)div拖拽寬度示例代碼
本例為大家演示個(gè)比較簡(jiǎn)單的div拖動(dòng),另外可根據(jù)自己的需求,添加相應(yīng)的代碼,實(shí)現(xiàn)自己的想要的效果,具體如下,喜歡的請(qǐng)支持下2013-07-07AspNet中使用JQuery上傳插件Uploadify詳解
Uploadify是JQuery的一個(gè)上傳插件,實(shí)現(xiàn)的效果非常不錯(cuò),帶進(jìn)度顯示。不過(guò)官方提供的實(shí)例時(shí)php版本的,本文將詳細(xì)介紹Uploadify在Aspnet中的使用2015-05-05基于jQuery實(shí)現(xiàn)Ajax驗(yàn)證用戶名是否存在實(shí)例
這篇文章主要為大家詳細(xì)介紹了基于jQuery實(shí)現(xiàn)Ajax驗(yàn)證用戶名是否存在實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-03-03jQuery插件HighCharts繪制的基本折線圖效果示例【附demo源碼下載】
這篇文章主要介紹了jQuery插件HighCharts繪制的基本折線圖效果,結(jié)合實(shí)例形式分析了jQuery基于HighCharts插件繪制圖形的具體實(shí)現(xiàn)步驟與相關(guān)操作技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-03-03使用jQuery.Validate進(jìn)行客戶端驗(yàn)證(初級(jí)篇) 不使用微軟驗(yàn)證控件的理由
以前在做項(xiàng)目的時(shí)候就有個(gè)很大心病,就是微軟的驗(yàn)證控件,雖然微軟的驗(yàn)證控件可以幫我們完成大部分的驗(yàn)證,驗(yàn)證也很可靠上手也很容易,但是我就是覺(jué)得不爽.2010-06-06