DIV CSS 佈局教程網

用 jQuery 打造圖片輪播切換的完整實作
編輯:JQuery特效代碼     

一個網站的首頁如果靜態得毫無生氣,往往很難抓住訪客的目光。早些年我曾用 Flash AS3 做過圖片切換,效果確實亮眼,但維護和修改起來實在繁瑣。心裡一直有個念頭,想親手做一套輪播效果,總覺得工程浩大,直到真正動手實踐才發現,其實沒有想像中那麼難。

過程中倒是碰到一個小插曲:在 IE8 相容模式下,為帶透明效果的 div 添加樣式時會失效,好在 Chrome 與 IE8 相容測試都正常通過。由於這段程式碼主要是寫給自己留作記錄,JS 沒有與頁面分離,也沒有刻意封裝成外掛,畢竟一個網站用不到幾處這樣的效果,先把功能跑通再說。最終呈現出來的成果,視覺上還算相當有質感。

以下附上頁面與 JS 的完整程式碼:

程式碼如下:
<script type="text/javascript">
        var picCurrent = 1;
        var picTotal = 8;
        var interval; //自動運行
        function picChange(current) {
             //停止當前動畫
             if ($("#divImg").is(":animated")) { $("#divImg").stop(); }
             picCurrent = current;
             //為當前選擇的設置樣式
             $("#divLink").find("a").removeClass("picselect")
             $("#divLink").find("a[title='" + picCurrent + "']").addClass("picselect");
             //設置下面的圖片說明
             var remark = "<a href=\"images/pic" + picCurrent + ".jpg\">";
             switch (picCurrent) {
                 case 1: remark += " 菊花〔拉丁學名:Dendranthema morifolium(Ramat. )Tzvel.〕,常用chrysanthemum。菊花是菊科,菊屬多年生草本... "; break;
                 default: remark += picCurrent + "測試說明"; break;
             }
             remark += "</a>";
             $("#picremark").html(remark);
             //運行動畫
             $("#divImg").animate({ left: -((picCurrent - 1) * 1000) + "px" }, "1000");
             return false;
         }
         //暫不需使用
         function PicPer() {
             if (picCurrent > 1) {
                picCurrent--;
             }
             else {
                picCurrent = picTotal;
             }
             picChange(picCurrent);
         }
         //下一張
         function PicNext() {
             if (picCurrent == picTotal) {
                picCurrent = 1
             }
             else {
                picCurrent++;
             }
             picChange(picCurrent);
         }
         //自動切換圖片
         function PicRun(functionName) {
             picChange(1);
             interval = setInterval(PicNext, "3000");
         }
         $(document).ready(function () {
             PicRun();
         });
    </script>

 
程式碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
    <title>圖片切換</title>
    <script src="jquery-1.8.0.js" type="text/javascript"></script>
    <link href="picchange.css" rel="stylesheet" type="text/css" />
    </head>
<body>
    <div class="picMain">
        <div class="picimg" id="divImg">
            <img src="images/pic1.jpg" class="pic" />
            <img src="images/pic2.jpg" class="pic" />
            <img src="images/pic3.jpg" class="pic" />
            <img src="images/pic4.jpg" class="pic" />
            <img src="images/pic5.jpg" class="pic" />
            <img src="images/pic6.jpg" class="pic" />
            <img src="images/pic7.jpg" class="pic" />
            <img src="images/pic8.jpg" class="pic" />
        </div>
        <div class="picaction" id="divLink">
            <a href="images/pic8.jpg" title="8" onclick=" return picChange(8)" class="">8</a> <a href="images/pic7.jpg" title="7" onclick=" return picChange(7)">7</a> <a href="images/pic6.jpg" title="6"
                    onclick=" return picChange(6)">6</a> <a href="images/pic5.jpg" title="5" onclick=" return picChange(5)">
                        5</a> <a href="images/pic4.jpg" title="4" onclick=" return picChange(4)">4</a>
            <a href="images/pic3.jpg" title="3" onclick=" return picChange(3)">3</a> <a href="images/pic2.jpg"
                title="2" onclick=" return picChange(2)">2</a> <a href="images/pic1.jpg" title="1"
                    onclick=" return picChange(1)" class="">1</a>
        </div>
        <div id="picremark" class="picRemark">
            測試介紹文件了啊</div>
    </div>
</body>
</html>

接著是 CSS 的實作:

程式碼如下:
.picMain
{
    margin: auto;
    overflow: hidden;
    width: 1000px;
    height: 400px;
    position: relative;
}
.picimg
{
    width: 10000px;
    height: 400px;
    background-color: #000000;
    position: absolute;
    top: 0px;
}
.picRemark
{
    position: absolute;
    width: 500px;
    height: 50px;
    bottom: 0px;
    left: 0px;
    color: #FFFFFF;
    text-indent: 2em;
}
.picRemark a
{
    color: #FFFFFF;
    text-decoration: none;
}
.picRemark a:hover
{
    text-decoration: underline;
}
.picaction
{
    position: absolute;
    width: 1000px;
    height: 50px;
    background-color: #000000;
    filter: alpha(opacity=50);
    -moz-opacity: 0.5;
    opacity: 0.5;
    overflow: auto;
    bottom: 0px;
    left: 0px;
    text-align: right;
}
.picaction a
{
    border: 1px solid #C0C0C0;
    width: 30px;
    height: 30px;
    float: right;
    line-height: 30px;
    text-decoration: none;
    text-align: center;
    color: #FFFFFF;
    font-weight: bold;
    margin-top: 10px;
    display: block;
    margin-right: 10px;
}
.pic
{
    width: 1000px;
    height: 400px;
    float: left;
}
.picselect
{
    background-color: #919191;
}

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved