首先是HTML代碼,很簡單,ul+li實現菜單
導航欄一 Android C++ IOS Java Ruby 首頁
基本效果:
接下來設置CSS屬性,這里要注意標簽a是行級元素,所以需要用display轉成塊級元素,這個很常用,還有就是line-height的常見用法
*{ 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;}
實現的效果:
最后就是JS動態添加定位效果了
js里面這樣考慮,頁面跳轉就會有鏈接,根據鏈接的后綴來匹配屬性,匹配則更改樣式即可達到想要的效果
需要注意的就是如何獲取URL,如何從URL里面查找出href信息
$(function(){ //當前鏈接以/分割后最后一個元素索引 var index = window.location.href.split("/").length-1; //最后一個元素前四個字母,防止后面帶參數 var href = window.location.href.split("/")[index].substr(0,4); if(href.length>0){ //如果匹配開頭成功則更改樣式 $(".list li a[href^='"+href+"']").addClass("on"); //[attribute^=value]:匹配給定的屬性是以某些值開始的元素。 }else { //默認主頁高亮 $(".list li a[href^='index']").addClass("on"); }});
效果圖
在1的基礎上,修改一下HTMLa標簽內容,然后通過CSS設置動畫效果
CSS實現動畫效果,首先把b和i標簽都設置為塊級元素,這樣的話就可以垂直分布,再給a設置一個transition,所謂的動畫,就是劃入后改變把a上移,再給a加個邊框好觀察,看下圖
最后想實現效果,就給包裹菜單的div設置一個溢出隱藏屬性即可
*{ 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來實現,代碼如下
$(function () { $(".list a").hover(function () { //stop是當執行其他動畫的時候停止當前的 $(this).stop().animate({ "margin-top": -40 }, 300); }, function () { $(this).stop().animate({ "margin-top": 0 }, 300); });});
首先子菜單使用div包裹,里面都是a標簽,如下
接下來設置樣式,既然是子菜單,就要脫離文檔頁面,所以使用absolute屬性,使用這個屬性那么父容器就要是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;}
如下效果:
接下來使用JQ和easing插件來控制動畫
find方法一般用來查找操作元素的后代元素的
$(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"}); });});
效果,圖片錄制不太好,實際上都是彈性動畫的
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com