首先是HTML代碼,很簡(jiǎn)單,ul+li實(shí)現(xiàn)菜單
導(dǎo)航欄一 Android C++ IOS Java Ruby 首頁(yè)
基本效果:
接下來(lái)設(shè)置CSS屬性,這里要注意標(biāo)簽a是行級(jí)元素,所以需要用display轉(zhuǎn)成塊級(jí)元素,這個(gè)很常用,還有就是line-height的常見(jiàn)用法
*{ margin:0; padding: 0;}a{ text-decoration: none;}.nva{ width: 100%; height: 40px; margin-top: 70px; background-color: #222;}.list{ width: 80%; height: 40px; margin: 0 auto; list-style-type: none;}.list li{ float: left;}.list li a{ padding: 0 30px; color: #aaa; line-height: 40px; display: block;}.list li a:hover{ background:#333; color:#fff;}.list li a.on{ background:#333; color:#fff;}h1{ margin: 20px auto; text-align: center;}
實(shí)現(xiàn)的效果:
最后就是JS動(dòng)態(tài)添加定位效果了
js里面這樣考慮,頁(yè)面跳轉(zhuǎn)就會(huì)有鏈接,根據(jù)鏈接的后綴來(lái)匹配屬性,匹配則更改樣式即可達(dá)到想要的效果
需要注意的就是如何獲取URL,如何從URL里面查找出href信息
$(function(){ //當(dāng)前鏈接以/分割后最后一個(gè)元素索引 var index = window.location.href.split("/").length-1; //最后一個(gè)元素前四個(gè)字母,防止后面帶參數(shù) var href = window.location.href.split("/")[index].substr(0,4); if(href.length>0){ //如果匹配開(kāi)頭成功則更改樣式 $(".list li a[href^='"+href+"']").addClass("on"); //[attribute^=value]:匹配給定的屬性是以某些值開(kāi)始的元素。 }else { //默認(rèn)主頁(yè)高亮 $(".list li a[href^='index']").addClass("on"); }});
效果圖
在1的基礎(chǔ)上,修改一下HTMLa標(biāo)簽內(nèi)容,然后通過(guò)CSS設(shè)置動(dòng)畫效果
CSS實(shí)現(xiàn)動(dòng)畫效果,首先把b和i標(biāo)簽都設(shè)置為塊級(jí)元素,這樣的話就可以垂直分布,再給a設(shè)置一個(gè)transition,所謂的動(dòng)畫,就是劃入后改變把a(bǔ)上移,再給a加個(gè)邊框好觀察,看下圖
最后想實(shí)現(xiàn)效果,就給包裹菜單的div設(shè)置一個(gè)溢出隱藏屬性即可
*{ margin:0; padding: 0;}a{ text-decoration: none;}.nva{ width: 100%; height: 40px; margin-top: 70px; background-color: #222; overflow: hidden;}.list{ width: 80%; height: 40px; margin: 0 auto; list-style-type: none;}.list li{ float: left;}.list li a{ padding: 0 30px; color: #aaa; line-height: 40px; display: block; transition: 0.3s;} .list b,.list i{ display: block; }.list li a:hover{ margin-top: -40px; background:#333; color:#fff;}.list li a.on{ background:#333; color:#fff;}h1{ margin: 20px auto; text-align: center;}
也可以采用JQ來(lái)實(shí)現(xiàn),代碼如下
$(function () { $(".list a").hover(function () { //stop是當(dāng)執(zhí)行其他動(dòng)畫的時(shí)候停止當(dāng)前的 $(this).stop().animate({ "margin-top": -40 }, 300); }, function () { $(this).stop().animate({ "margin-top": 0 }, 300); });});
首先子菜單使用div包裹,里面都是a標(biāo)簽,如下
接下來(lái)設(shè)置樣式,既然是子菜單,就要脫離文檔頁(yè)面,所以使用absolute屬性,使用這個(gè)屬性那么父容器就要是relative
*{ margin:0; padding: 0;}a{ text-decoration: none;}.nva{ width: 100%; height: 40px; margin-top: 70px; background-color: #222; position: relative;}.list{ width: 80%; height: 40px; margin: 0 auto; list-style-type: none;}.list li{ float: left;}.list li a{ padding: 0 30px; color: #aaa; line-height: 40px; display: block; transition: 0.3s;}.list b{ display: block;}.list li a:hover{ background:#333; color:#fff;}.list li a.on{ background:#333; color:#fff;}.list .down{ position: absolute; top: 40px; background-color: #222; /*display: none;*/}.list .down a{ color: #aaa; padding-left: 30px; display: block;}h1{ margin: 20px auto; text-align: center;}
如下效果:
接下來(lái)使用JQ和easing插件來(lái)控制動(dòng)畫
find方法一般用來(lái)查找操作元素的后代元素的
$(function () { $(".list li").hover(function () { //這里使用了easing插件 $(this).find(".down").stop().slideDown({duration:1000,easing:"easeOutBounce"}); }, function () { $(this).find(".down").stop().slideUp({duration:1000,easing:"easeOutBounce"}); });});
效果,圖片錄制不太好,實(shí)際上都是彈性動(dòng)畫的
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com