Web設計師值得收藏的10個jQuery特效(1)

來源:互聯網
上載者:User

jQuery已經不是什麼新鮮的事兒,以前總把它認為是非常難的東西,也就沒有認真去瞭解他了。直到學完CSS的大部分內容,才開始接觸這種"write less, do more" 的Javascrīpt架構。這篇文章的最重要內容是來自Web Designer Wall的一篇教程,一篇包含了10個jQuery特效的教程。這裡不打算全文翻譯,想以自己的語言來表達,或許這樣更方便大家理解/自己以後學習,也可能更準確地描述。

先試試看?特效執行個體:

View jQuery Demos:http://www.webdesignerwall.com/demo/jquery/

jQuery是如何工作的?

首先,你需要下載一個jQuery版本,並將它插入到<head>標籤內。然後,你將需要寫函數來告訴jQuery做些什麼,下面的這個圖表將告訴你jQuery是怎樣工作的請點擊圖片,查看大圖):

如何擷取元素Get the element)?

書寫jQuery函數是一個非常簡單的事。關鍵是你要學習如何擷取你想要實現的效果的確切元素。

  1. ("#header") = 擷取 id="header" 的元素   
  2. ("h3") = 擷取所有<h3>   
  3. ("div#content .photo") = 擷取<div id="content">裡  
  4.   所有用class="photo"定義的元素   
  5. ("ul li") = 擷取所以 <ul> 中 <li> 的元素   
  6. ("ul li:first") = 只擷取<ul>中第一個<li>  

1. 簡單的下拉麵板

讓我們來開始這個簡單的下拉麵板特效吧,或許你已經見過很多次,現在,自己試試吧:

當包含class="btn-slide"的元素被點擊,它會下拉/上提<div id="panel">裡的元素。然後切換到CSS中的class="active"到<a class="btn-slide">元素。.active 將會以CSS的方式開啟/關閉出面板。

  1. $(document).ready(function(){  
  2.  
  3.  $(".btn-slide").click(function(){     
  4.  $("#panel").slideToggle("slow");     
  5.  $(this).toggleClass("active");   
  6.  });  
  7.  
  8. }); 

view demo:http://www.webdesignerwall.com/demo/jquery/simple-slide-panel.html

2. 簡單的隱藏效果

,當右上方的標被點擊時,內容被隱藏。

當被定義為 <img class="delete"> 的圖片被點擊,它會手找到父級元素 <div class="pane"> 並啟用它的能力,慢慢消失,隱藏起來。

  1. $(document).ready(function(){  
  2.  
  3.  $(".pane .delete").click(function(){     
  4.  $(this).parents(".pane").animate({ opacity: "hide" }, "slow");   
  5.  });  
  6.  
  7. }); 

view demo:http://www.webdesignerwall.com/demo/jquery/simple-disappear.html

3 連續過渡效果

讓我們來看看jQuery連貫性的威力吧。只需要幾行代碼,我能讓這個方塊漸層+縮放比例地飛來飛去。

Line 1: 當 <a class="run"> 被點擊

Line 2: 啟用 <div id="box"> 的不透明度opacity)=0.1,直到值達到400px,速度達到1200px/ms

Line 3: 當opacity=0.4, top=160px,height=20,width=20,以"slow"顯示

Line 4: 當opacity=1, left=0, height=100, width=100,也以"slow"顯示

Line 5: 當opacity=1, left=0, height=100, width=100, 也以"slow"顯示

Line 6: 當top=0, 以"fast"顯示

Line 7: 然後,以常速上滑 (default speed = "normal")

Line 8: 然後以"slow"下滑

Line 9:返回失效會阻止瀏覽器跳向連結錨點

  1. $(document).ready(function(){  
  2.  
  3.  $(".run").click(function(){  
  4.  
  5.    $("#box").animate({opacity: "0.1", left: "+=400"}, 1200)     
  6.    .animate({opacity: "0.4", top: "+=160", height: "20", width: "20"}, "slow")     
  7.    .animate({opacity: "1", left: "0", height: "100", width: "100"}, "slow")     
  8.    .animate({top: "0"}, "fast")     
  9.    .slideUp()     
  10.    .slideDown("slow")     
  11.    return false;  
  12.  
  13.  });  
  14.  
  15. }); 

view demo:http://www.webdesignerwall.com/demo/jquery/chainable-effects.html

4a. 可摺疊的模式 #1

這是第一個可摺疊的樣式。

第一行將向<div class="accordion"> 內的第一個<H3> 添加一個CSS class為"active"的值。 第二行剛是隱藏<div class="accordion">內非第一個< p >的內容。當 <h3> 被點擊時,當前<p>下拉,而原先下拉的<p> 上提。

  1. $(document).ready(function(){  
  2.  
  3.  $(".accordion h3:first").addClass("active");   
  4.  $(".accordion p:not(:first)").hide();  
  5.  $(".accordion h3").click(function(){  
  6.  $(this).next("p").slideToggle("slow")     
  7.  .siblings("p:visible").slideUp("slow");     
  8.  $(this).toggleClass("active");     
  9.  $(this).siblings("h3").removeClass("active");  
  10.  
  11.  });  
  12.  
  13. }); 

view demo:http://www.webdesignerwall.com/demo/jquery/accordion1.html

4b. 可摺疊模式 #2

這個執行個體與#1非常類似,不過,它會讓指定的面板像預設面板一樣開啟。

在CSS樣式表中,設定.accordion p 為 display:none。現在,如果你像預設開啟的樣式一樣,開啟第三個面板,你可以寫$(".accordion2 p").eq(2).show(); (eq = equal)來實現它,需要注意的是起始點是"0",而不是"1",所以,第三個相應的是"2",而不是"3"。

  1. $(document).ready(function(){  
  2.  
  3.  $(".accordion2 h3").eq(2).addClass("active");   
  4.  $(".accordion2 p").eq(2).show();  
  5.  $(".accordion2 h3").click(function(){     
  6.  $(this).next("p").slideToggle("slow")     
  7.  .siblings("p:visible").slideUp("slow");     
  8.  $(this).toggleClass("active");     
  9.  $(this).siblings("h3").removeClass("active"); });  
  10.  
  11. }); 

view demo:http://www.webdesignerwall.com/demo/jquery/accordion2.html

5a. 滑鼠經過啟用效果 #1

這個將會實現一個非常漂亮的,當滑鼠經過時出現漸層出現的效果。當滑鼠經過菜單時,它會尋找緊接著的<em>,並在上方啟用它的不透明度。

  1. $(document).ready(function(){  
  2.  
  3.  $(".menu a").hover(function() {     
  4.  $(this).next("em").animate({opacity: "show", top: "-75"}, "slow");   
  5.  }, function()   
  6.  {     
  7.  $(this).next("em").animate({opacity: "hide", top: "-85"}, "fast");   
  8.  });  
  9.  
  10. }); 

view demo:http://www.webdesignerwall.com/demo/jquery/animated-hover1.html

5b. 滑鼠經過啟用 #2

這個執行個體會顯示菜單中連結的title 屬性attribute,讓其以變數方式存在,並添加<em>標籤。第一行會添加一個空的<em>到菜單的<a>元素。當滑鼠經過菜單連結時,它會顯示title的屬性,讓它以"hoverText隱藏)"的形式顯示,並使<em>中的文字顯示隱藏文本的值。

  1. $(document).ready(function(){  
  2.  
  3.  $(".menu2 a").append("<em></em>");  
  4.  
  5.  $(".menu2 a").hover(function() {     
  6.  $(this).find("em").animate({opacity: "show", top: "-75"}, "slow");     
  7.  var hoverText = $(this).attr("title");     
  8.  $(this).find("em").text(hoverText);   
  9.  }, function() {     
  10.  $(this).find("em").animate({opacity: "hide", top: "-85"}, "fast");   
  11.  });  
  12.  
  13. }); 

view demo:http://www.webdesignerwall.com/demo/jquery/animated-hover2.html


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.