溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

jQuery如何實(shí)現(xiàn)Nav導(dǎo)航菜單效果

發(fā)布時(shí)間:2021-06-24 14:49:09 來源:億速云 閱讀:128 作者:小新 欄目:web開發(fā)

這篇文章給大家分享的是有關(guān)jQuery如何實(shí)現(xiàn)Nav導(dǎo)航菜單效果的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

自己寫的一個(gè)簡(jiǎn)單的導(dǎo)航菜單,先看效果:

jQuery如何實(shí)現(xiàn)Nav導(dǎo)航菜單效果

鼠標(biāo)懸浮時(shí)菜單項(xiàng)向上移動(dòng)成藍(lán)底白字,點(diǎn)擊之后底部會(huì)有藍(lán)條表示當(dāng)前選中項(xiàng)。

頁面代碼,菜單的每一項(xiàng)都是一個(gè) div ,其中包括一個(gè) ul 用來放置顯示文字等,另一個(gè) div 則是底部的藍(lán)條,需要給第一項(xiàng)和最后一項(xiàng)設(shè)置不同的 class ,樣式需要用到:

<div id="nav">
  <div class="navItem indexNavItem">
    <ul class="navUl">
      <li>首頁</li>
      <li class="hoverLi">首頁</li>
    </ul>
    <div class="highlighter selectedNav"></div>
  </div>
  <div class="navItem">
    <ul class="navUl">
      <li>A</li>
      <li class="hoverLi">A</li>
    </ul>
    <div class="highlighter"></div>
  </div>
  <div class="navItem lastNavItem">
    <ul class="navUl">
      <li>A</li>
      <li class="hoverLi">A</li>
    </ul>
    <div class="highlighter"></div>
  </div>
  <div id="logoutNavItem" class="navItem logoutNavItem lastNavItem">
    <ul class="navUl">
      <li>退出</li>
      <li class="hoverLi">退出</li>
    </ul>
    <div class="highlighter"></div>  
  </div>
</div>

樣式,主要就是每個(gè)菜單項(xiàng)的左右邊框的設(shè)置以及 ul 和 li 的位置設(shè)置:

*
{
  padding: 0;
  margin: 0;
}
body
{
  background-color: #fffff3;
  font: 12px/1.6em Helvetica, Arial, sans-serif;
}
ul,li{
  list-style: none;
}
#nav
{
  text-align: center;
  height: 50px;
  font-size: 10px;
  line-height: 30px;
  background-color: #F0E6DB;
  margin-bottom: 10px;
}
.navItem
{
  cursor: pointer;
  position: relative;
  float: left;
  width: 100px;
  height: 50px;
  font-size: 15px;
  border-right: 2px solid rgb(255,255,255);
  border-left: 2px solid rgb(255,255,255);
  overflow: hidden;
  font-weight:bold;
}
.indexNavItem
{
  border-left: 4px solid rgb(255,255,255);
  margin-left: 10px;
}
.lastNavItem
{
  border-right: 4px solid rgb(255,255,255);
}
.logoutNavItem
{
  float: right;
  width: 120px;
  margin-right: 10px;
  border-left: 4px solid rgb(255,255,255);
}
.navUl
{
  position: relative;
  height: 100px;
  width: 100%;
  border-bottom: 5px solid rgb(2,159,212);
}
.navUl li
{
  height: 50px;
  line-height: 50px;
}
.highlighter
{
  position: absolute;
  bottom: 0;
  height: 5px;
  width: 100%;
}
.selectedNav
{
  background-color: #029FD4;
}
.hoverLi
{
  background-color: #029FD4;
  color: #ffffff;
}

接下來就是給菜單編寫懸浮和單擊事件的 js 代碼了,懸浮時(shí)將 ul 上移 li 的高度,鼠標(biāo)移開后再恢復(fù),點(diǎn)擊之后就是給藍(lán)條的 div 添加樣式即可:

$(function() {
  $(".navItem").hover(function() {
    $(this).children("ul").animate({
      top: "-50px"
    }, 100);
  }, function() {
    $(this).children("ul").animate({
      top: "0px"
    }, 100);
  });
  $(".navItem").click(function(event) {
    $(this).siblings().children('.highlighter').removeClass('selectedNav');
    $(this).children('.highlighter').addClass('selectedNav');
  });
})

感謝各位的閱讀!關(guān)于“jQuery如何實(shí)現(xiàn)Nav導(dǎo)航菜單效果”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI