DIV CSS 佈局教程網

用 jQuery 做方向感圖片漸變切換動畫
編輯:JQuery特效代碼     

這個效果用到透明度變化與 CSS 濾鏡。先提醒一個坑:濾鏡在 Firefox 下不被支援,所以陰影那部分在 Firefox 看不到效果。

HTML 結構

整體由一個 #nav 容器包住縮圖清單與數個文字、遮罩層:

<div id="nav">
  <ul>
    <li><img src="images/02.jpg" text="美景如畫的海景|天藍海藍" pic="2">
      <div>一個海島</div></li>
    <li><img src="images/01.jpg" text="Handy Code|春華秋實" pic="1">
      <div>一片麥穗</div></li>
    <li><img src="images/03.jpg" text="郁郁蔥蔥的生命|生生不息的生命" pic="3">
      <div>一樹</div></li>
    <li><img src="images/04.jpg" text="孤獨的一棵老樹|等誰呢?" pic="4">
      <div>一棵大樹</div></li>
    <li><img src="images/05.jpg" text="明媚的向日葵花|生生不息的生命" pic="5">
      <div>一地葵花</div></li>
  </ul>
  <div id="frontTextBack"></div>
  <div id="frontText"></div>
  <div id="frontTextSub"></div>
  <div id="BG"></div>
  <div id="mask"></div>
  <div id="back"><img height="240" src="images/2.jpg" width="760"></div>
</div>

其中 #frontTextBack 是靠 CSS 濾鏡做出來的陰影文字,位置比 #frontText 各偏移 2px,兩者疊在一起就有立體感。

CSS

body { padding:0; moz-user-select: none; margin:0; }
#nav { background: url("loadsmall.gif") no-repeat scroll 330px 100px #000;
       height:240px; overflow:hidden; position:relative; width:760px; display:block; }
#nav ul { bottom:0px; left:3px; position:absolute; text-align:left; z-index:999; }
#nav ul li { list-style:none; display:block; float:left; height:50px; width:140px;
             font-size:12px; position:relative; }
#nav li img{ border:1px solid white; cursor:pointer; float:left; height:35px;
             left:10px; top:5px; width:52px; margin-top:10px; }
#nav li div{ FILTER: alpha(opacity=60); margin-left:70px; margin-top:5px;
             padding-left:10px; color:white; }
#frontTextBack{ color:#000; font-family:Verdana; font-size:30px; left:22px;
                position:absolute; width:100%; top:22px; }
#frontText { color:#fff; font-size:30px; font-weight:900; left:20px;
             position:absolute; top:20px; width:100%; z-index:999; }
#frontTextSub{ color:#fff; font-size:13px; left:25px; position:absolute; top:60px; width:100%; }
#BG { background:none repeat scroll 0 0 #000; border-top:1px solid #999;
      bottom:0; height:50px; position:absolute; text-align:right; width:100%; }
#mask { background:repeat scroll 0 0 transparent; height:100%; position:absolute;
        width:100%; z-index:990; }
#back { text-align:center; }
.gray { FILTER: gray(); }   /* 濾鏡:轉灰階 */

JavaScript 邏輯

點擊縮圖後依序做四件事:

$(function(){
  $("li img").click(function(){
    if (this.className.indexOf("active") != -1) { return; }  // 已選中就不重複播放
    var i = $(this).attr("pic");                 // 大圖編號
    var t = $(this).attr("text").split("|");     // 主標 / 副標以 | 分隔

    $("#frontText").fadeOut();
    $("#frontTextBack").fadeOut();
    $("#frontTextSub").fadeOut();

    // 上一張 active 縮圖復位
    $("li img.active").animate({top:5,width:52,left:10},300)
                       .removeClass("active").fadeTo(200,0.6);

    // 當前縮圖放大並提高不透明度
    $(this).animate({top:-5,width:40,height:70,left:1},300)
           .addClass("active").fadeTo(200,1);

    // 大圖先轉灰再淡入,完成後換圖並淡入文字
    $("#back").children().addClass("gray").end()
      .fadeIn(500,0.1,function(){
        $(this).children("img").attr("src","imgaes/"+i+".jpg").removeClass("gray");
        $(this).fadeTo(500,1,function(){
          $("#frontText").html(t[0]).fadeIn(200);
          $("#frontTextBack").html(t[0]).fadeIn(200);
          $("#frontTextSub").html(t[1]).fadeIn(200);
        });
      });
  });
});

切換時先把大圖套上 gray 濾鏡再淡入,视觉上就有「先蒙一層灰、再逐漸鮮明」的過場感。最後的 show() 用 eq(i).click() 觸發第一張圖,達到初始化的目的。

var i = 0;
show();
function show(){
  if (i == $("li img").size()) i = 0;
  $("li img").eq(i).click();
  i++;
}
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved