1. 程式人生 > >前端之路——jQuery第四課(結合所學知識,製作一個具有jq特效的導航欄)

前端之路——jQuery第四課(結合所學知識,製作一個具有jq特效的導航欄)

新課內容:

1.與&&、或||、非!(邏輯判斷->和布林值有關係),這與或非,,,

2.與或非的運算級

3.何為+=?何為-=

4.駝峰寫法!!(例子:MaiZhanHao)

5.callback(回撥函式)

6.alter彈窗

7.jq特效:顯示/隱藏/彈出/彈入/滑動,語法:

div_1.click(function(){
               div_2.slideDown();
          });
             div_2.click(function(){
               div_2.slideUp('slow', function() {
               
               });

8.動畫(重點)animate,上圖,上程式碼生氣(滑鼠移入div,div會向左滑動後返回原位)


;$(function(){
           var ul = $('.ul'),
               a  = $('.a'),
               b  = $('.b'),
               c  = $('.c'),
               d  = $('.d'),
               e  = $('.e'),
               f  = $('.f'),
               g  = $('.g');
           // ul.click(function() {
           // window.location.href="https://www.baidu.com";
           // });
           a.mouseenter(function(){
             a.animate({right: 28},320)
              .animate({left: 5},320)
           });
           b.mouseenter(function(){
             b.animate({right: 120},300)
              .animate({left: 5},300)
           });
           c.mouseenter(function(){
             c.animate({right: 200},280)
              .animate({left: 5},280)
           });
           d.mouseenter(function(){
             d.animate({right: 300},260)
              .animate({left: 5},260)
           });
           e.mouseenter(function(){
             e.animate({right: 400},240)
              .animate({left: 5},240)
           });
           f.mouseenter(function(){
             f.animate({right: 480},220)
              .animate({left: 5},220)
           });
           g.mouseenter(function(){
             g.animate({right: 580},200)
              .animate({left: 5},200)
           });
});

你看到後,你會覺得我真是個前端小白,,,,,,

問題在於很多能優化的語句都被我複雜化了。驚訝,老師一眼就看出來了。。。。。。。這麼多行程式碼,,,,

優化版:

// 文件就緒事件
;$(function(){
           var ul = $('.ul'),
               a  = $('.a'),
               b  = $('.b'),
               c  = $('.c'),
               d  = $('.d'),
               e  = $('.e'),
               f  = $('.f'),
               g  = $('.g');
           function showDiv (dom,value){
             dom.mouseenter(function(){
             dom.animate({right: value})
              .animate({right: 0})
           });
           }
           showDiv(a,30);
           showDiv(b,120);
           showDiv(c,200);
           showDiv(d,300);
           showDiv(e,400);
           showDiv(f,480);
           showDiv(g,580); 

});

今天還有個重要的知識電:鏈式寫法,語法:

div_1.click(function(){
    div_1.animate({top: 400},2000).animate({right: 400}, 2000).click(function() {alert(1);});
    });

還有!!!!!福利到!!!!羨慕

http://www.jq22.com/

你開啟後會感覺到jq的強大,,,,,