基于jQuery的簡(jiǎn)單的列表導(dǎo)航菜單
更新時(shí)間:2011年03月02日 00:05:51 作者:
最近一段時(shí)間準(zhǔn)備開始學(xué)習(xí)JQUERY,感覺JQUERY的功能非常強(qiáng)大,而且代碼量也減少了許多....代碼也非常的清晰!
下面是我做的一個(gè)簡(jiǎn)單的導(dǎo)航菜單,因?yàn)槭切率?難免有什么不對(duì)的地方,希望大家拍磚!
好了 開始進(jìn)入正題吧:
1.首先定義CSS樣式表:
<style type="text/css">
body{font-size:13px}
ul,li{list-style-type:none;padding:0px;margin:0px}
.menu{width:190px;border:solid 1px #E5D1A1;background-color:#FFFDD2}
.optn{width:190px;line-height:28px;border-top:dashed 1px #ccc}
.content{padding-top:10px;clear:left}
a{text-decoration:none;color:#666;padding:10px}
.optnFocus{background-color:#fff;font-weight:bold}
div{padding:10px}
div img{float:left;padding-right:6px}
span{padding-top:3px;font-size:14px;font-weight:bold;float:left}
.tip{width:190px;border:solid 2px #ffa200;position:absolute;padding:10px;
background-color:#fff;display:none}
.tip li{line-height:23px;}
#sort{position:absolute;display:none}
</style>
2.引用JQUERY的框架,大家可以去官網(wǎng)上下載最新的JQUERY,好像JQUERY1.5的版本都發(fā)布了,鏈接地址:http://jquery.com/
3.定義HTML標(biāo)簽:
<ul>
<li class="menu">
<div>
<img alt="" src="Images/icon.gif" />
<span>電腦數(shù)碼類產(chǎn)品</span>
</div>
<ul class="content">
<li class="optn"><a href="#">筆記本</a></li>
<ul class="tip">
<li><a href="#">筆記本1</a></li>
<li><a href="#">筆記本2</a></li>
<li><a href="#">筆記本3</a></li>
<li><a href="#">筆記本4</a></li>
<li><a href="#">筆記本5</a></li>
</ul>
<li class="optn"><a href="#">移動(dòng)硬盤</a></li>
<ul class="tip">
<li><a href="#">移動(dòng)硬盤1</a></li>
<li><a href="#">移動(dòng)硬盤2</a></li>
<li><a href="#">移動(dòng)硬盤3</a></li>
<li><a href="#">移動(dòng)硬盤4</a></li>
<li><a href="#">移動(dòng)硬盤5</a></li>
</ul>
<li class="optn"><a href="#">電腦軟件</a></li>
<ul class="tip">
<li><a href="#">電腦軟件1</a></li>
<li><a href="#">電腦軟件2</a></li>
<li><a href="#">電腦軟件3</a></li>
<li><a href="#">電腦軟件4</a></li>
<li><a href="#">電腦軟件5</a></li>
</ul>
<li class="optn"><a href="#">數(shù)碼產(chǎn)品</a></li>
<ul class="tip">
<li><a href="#">數(shù)碼產(chǎn)品1</a></li>
<li><a href="#">數(shù)碼產(chǎn)品2</a></li>
<li><a href="#">數(shù)碼產(chǎn)品3</a></li>
<li><a href="#">數(shù)碼產(chǎn)品4</a></li>
<li><a href="#">數(shù)碼產(chǎn)品5</a></li>
</ul>
</ul>
<img id="sort" src="Images/sort.gif" alt=""/>
</li>
</ul>
4.接下來是定義最重要的JQUERY代碼了:
<script type="text/javascript">
$(function() {
var curY; //獲取所選項(xiàng)的Top值
var curH; //獲取所選項(xiàng)的Height值
var curW; //獲取所選項(xiàng)的Width值
var srtY; //設(shè)置提示箭頭的Top值
var srtX; //設(shè)置提示箭頭的Left值
var objL; //獲取當(dāng)前對(duì)象
/*
*設(shè)置當(dāng)前位置數(shù)值
*參數(shù)obj為當(dāng)前對(duì)象名稱
*/
function setInitValue(obj) {
curY = obj.offset().top
curH = obj.height();
curW = obj.width();
srtY = curY + (curH / 2) + "px"; //設(shè)置提示箭頭的Top值
srtX = curW - 5 + "px"; //設(shè)置提示箭頭的Left值
}
$(".optn").mouseover(function() {//設(shè)置當(dāng)前所選項(xiàng)的鼠標(biāo)滑過事件
objL = $(this); //獲取當(dāng)前對(duì)象
setInitValue(objL); //設(shè)置當(dāng)前位置
var allY = curY - curH + "px"; //設(shè)置提示框的Top值
objL.addClass("optnFocus"); //增加獲取焦點(diǎn)時(shí)的樣式
objL.next("ul").show().css({ "top": allY, "left": curW }) //顯示并設(shè)置提示框的坐標(biāo)
$("#sort").show().css({ "top": srtY, "left": srtX }); //顯示并設(shè)置提示箭頭的坐標(biāo)
})
.mouseout(function() {//設(shè)置當(dāng)前所選項(xiàng)的鼠標(biāo)移出事件
$(this).removeClass("optnFocus"); //刪除獲取焦點(diǎn)時(shí)的樣式
$(this).next("ul").hide(); //隱藏提示框
$("#sort").hide(); //隱藏提示箭頭
})
$(".tip").mousemove(function() {
$(this).show(); //顯示提示框
objL = $(this).prev("li"); //獲取當(dāng)前的上級(jí)li對(duì)象
setInitValue(objL); //設(shè)置當(dāng)前位置
objL.addClass("optnFocus"); //增加上級(jí)li對(duì)象獲取焦點(diǎn)時(shí)的樣式
$("#sort").show().css({ "top": srtY, "left": srtX }); //顯示并設(shè)置提示箭頭的坐標(biāo)
})
.mouseout(function() {
$(this).hide(); //隱藏提示框
$(this).prev("li").removeClass("optnFocus"); //刪除獲取焦點(diǎn)時(shí)的樣式
$("#sort").hide(); //隱藏提示箭頭
})
})
</script>
5.好了,大功告成了......
運(yùn)行的效果如下:
好了 開始進(jìn)入正題吧:
1.首先定義CSS樣式表:
復(fù)制代碼 代碼如下:
<style type="text/css">
body{font-size:13px}
ul,li{list-style-type:none;padding:0px;margin:0px}
.menu{width:190px;border:solid 1px #E5D1A1;background-color:#FFFDD2}
.optn{width:190px;line-height:28px;border-top:dashed 1px #ccc}
.content{padding-top:10px;clear:left}
a{text-decoration:none;color:#666;padding:10px}
.optnFocus{background-color:#fff;font-weight:bold}
div{padding:10px}
div img{float:left;padding-right:6px}
span{padding-top:3px;font-size:14px;font-weight:bold;float:left}
.tip{width:190px;border:solid 2px #ffa200;position:absolute;padding:10px;
background-color:#fff;display:none}
.tip li{line-height:23px;}
#sort{position:absolute;display:none}
</style>
2.引用JQUERY的框架,大家可以去官網(wǎng)上下載最新的JQUERY,好像JQUERY1.5的版本都發(fā)布了,鏈接地址:http://jquery.com/
3.定義HTML標(biāo)簽:
復(fù)制代碼 代碼如下:
<ul>
<li class="menu">
<div>
<img alt="" src="Images/icon.gif" />
<span>電腦數(shù)碼類產(chǎn)品</span>
</div>
<ul class="content">
<li class="optn"><a href="#">筆記本</a></li>
<ul class="tip">
<li><a href="#">筆記本1</a></li>
<li><a href="#">筆記本2</a></li>
<li><a href="#">筆記本3</a></li>
<li><a href="#">筆記本4</a></li>
<li><a href="#">筆記本5</a></li>
</ul>
<li class="optn"><a href="#">移動(dòng)硬盤</a></li>
<ul class="tip">
<li><a href="#">移動(dòng)硬盤1</a></li>
<li><a href="#">移動(dòng)硬盤2</a></li>
<li><a href="#">移動(dòng)硬盤3</a></li>
<li><a href="#">移動(dòng)硬盤4</a></li>
<li><a href="#">移動(dòng)硬盤5</a></li>
</ul>
<li class="optn"><a href="#">電腦軟件</a></li>
<ul class="tip">
<li><a href="#">電腦軟件1</a></li>
<li><a href="#">電腦軟件2</a></li>
<li><a href="#">電腦軟件3</a></li>
<li><a href="#">電腦軟件4</a></li>
<li><a href="#">電腦軟件5</a></li>
</ul>
<li class="optn"><a href="#">數(shù)碼產(chǎn)品</a></li>
<ul class="tip">
<li><a href="#">數(shù)碼產(chǎn)品1</a></li>
<li><a href="#">數(shù)碼產(chǎn)品2</a></li>
<li><a href="#">數(shù)碼產(chǎn)品3</a></li>
<li><a href="#">數(shù)碼產(chǎn)品4</a></li>
<li><a href="#">數(shù)碼產(chǎn)品5</a></li>
</ul>
</ul>
<img id="sort" src="Images/sort.gif" alt=""/>
</li>
</ul>
4.接下來是定義最重要的JQUERY代碼了:
復(fù)制代碼 代碼如下:
<script type="text/javascript">
$(function() {
var curY; //獲取所選項(xiàng)的Top值
var curH; //獲取所選項(xiàng)的Height值
var curW; //獲取所選項(xiàng)的Width值
var srtY; //設(shè)置提示箭頭的Top值
var srtX; //設(shè)置提示箭頭的Left值
var objL; //獲取當(dāng)前對(duì)象
/*
*設(shè)置當(dāng)前位置數(shù)值
*參數(shù)obj為當(dāng)前對(duì)象名稱
*/
function setInitValue(obj) {
curY = obj.offset().top
curH = obj.height();
curW = obj.width();
srtY = curY + (curH / 2) + "px"; //設(shè)置提示箭頭的Top值
srtX = curW - 5 + "px"; //設(shè)置提示箭頭的Left值
}
$(".optn").mouseover(function() {//設(shè)置當(dāng)前所選項(xiàng)的鼠標(biāo)滑過事件
objL = $(this); //獲取當(dāng)前對(duì)象
setInitValue(objL); //設(shè)置當(dāng)前位置
var allY = curY - curH + "px"; //設(shè)置提示框的Top值
objL.addClass("optnFocus"); //增加獲取焦點(diǎn)時(shí)的樣式
objL.next("ul").show().css({ "top": allY, "left": curW }) //顯示并設(shè)置提示框的坐標(biāo)
$("#sort").show().css({ "top": srtY, "left": srtX }); //顯示并設(shè)置提示箭頭的坐標(biāo)
})
.mouseout(function() {//設(shè)置當(dāng)前所選項(xiàng)的鼠標(biāo)移出事件
$(this).removeClass("optnFocus"); //刪除獲取焦點(diǎn)時(shí)的樣式
$(this).next("ul").hide(); //隱藏提示框
$("#sort").hide(); //隱藏提示箭頭
})
$(".tip").mousemove(function() {
$(this).show(); //顯示提示框
objL = $(this).prev("li"); //獲取當(dāng)前的上級(jí)li對(duì)象
setInitValue(objL); //設(shè)置當(dāng)前位置
objL.addClass("optnFocus"); //增加上級(jí)li對(duì)象獲取焦點(diǎn)時(shí)的樣式
$("#sort").show().css({ "top": srtY, "left": srtX }); //顯示并設(shè)置提示箭頭的坐標(biāo)
})
.mouseout(function() {
$(this).hide(); //隱藏提示框
$(this).prev("li").removeClass("optnFocus"); //刪除獲取焦點(diǎn)時(shí)的樣式
$("#sort").hide(); //隱藏提示箭頭
})
})
</script>
5.好了,大功告成了......
運(yùn)行的效果如下:
您可能感興趣的文章:
- JQuery 寫的個(gè)性導(dǎo)航菜單
- 基于jquery實(shí)現(xiàn)導(dǎo)航菜單高亮顯示(兩種方法)
- jQuery彈性滑動(dòng)導(dǎo)航菜單實(shí)現(xiàn)思路及代碼
- Jquery實(shí)現(xiàn)帶動(dòng)畫效果的經(jīng)典二級(jí)導(dǎo)航菜單
- jQuery實(shí)現(xiàn)的動(dòng)態(tài)伸縮導(dǎo)航菜單實(shí)例
- 純CSS打造的導(dǎo)航菜單(附j(luò)query版)
- jQuery三級(jí)下拉列表導(dǎo)航菜單代碼分享
- Jquery+CSS 創(chuàng)建流動(dòng)導(dǎo)航菜單 Fluid Navigation
- jQuery實(shí)現(xiàn)簡(jiǎn)潔的導(dǎo)航菜單效果
- jQuery漸變發(fā)光導(dǎo)航菜單的實(shí)例代碼
- 分享14個(gè)很酷的jQuery導(dǎo)航菜單插件
- jQuery實(shí)現(xiàn)的背景動(dòng)態(tài)變化導(dǎo)航菜單效果
- 制作jquery遮罩層效果導(dǎo)航菜單代碼分享
- jQuery實(shí)現(xiàn)簡(jiǎn)單漂亮的Nav導(dǎo)航菜單效果
相關(guān)文章
解決jquery submit()提交表單提示:f[s] is not a function
jquery submit()無法提交表單 報(bào)錯(cuò):f[s] is not a function,很是疑惑搜集整理了一些解決方法,感興趣的朋友可以了解下啊,希望本文對(duì)你有所幫助2013-01-01jQuery的animate函數(shù)實(shí)現(xiàn)圖文切換動(dòng)畫效果
animate()在jquery中作為一個(gè)方法,可用于創(chuàng)建動(dòng)畫效果,jquery中的animate()方法讓那個(gè)頁面增加了很好的視覺效果2015-05-05jQuery選擇器選中最后一個(gè)元素,倒數(shù)第二個(gè)元素操作示例
這篇文章主要介紹了jQuery選擇器選中最后一個(gè)元素,倒數(shù)第二個(gè)元素操作,涉及jQuery選擇器eq()方法與last()方法相關(guān)操作技巧,需要的朋友可以參考下2018-12-12jQuery自定義數(shù)值抽獎(jiǎng)活動(dòng)代碼
這篇文章主要為大家詳細(xì)介紹了jQuery自定義數(shù)值抽獎(jiǎng)活動(dòng)的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的朋友可以參考一下2016-06-06jquery幻燈片插件bxslider樣式改進(jìn)實(shí)例
這篇文章主要介紹了jquery幻燈片插件bxslider樣式改進(jìn),對(duì)比官方樣式以實(shí)例形式講述了改進(jìn)的技巧,非常實(shí)用,需要的朋友可以參考下2014-10-10