DIV CSS 佈局教程網

仿迅雷焦點廣告輪播效果(jQuery 版)
編輯:JQuery特效代碼     

一、JavaScript 部分

使用前請先引入 jQuery。

$(document).ready(function(){
    var imgUrl      = new Array();
    var title       = new Array();
    var description = new Array();
    var imgLink     = new Array();
    var local;
    var count;

    local = 1;    // 指標位置
    count = 7;    // 圖片數量

    imgUrl[1] = "Images/ceshi.jpg";
    imgUrl[2] = "Images/ceshi1.jpg";
    imgUrl[3] = "Images/ceshi.jpg";
    imgUrl[4] = "Images/ceshi1.jpg";
    imgUrl[5] = "Images/ceshi.jpg";
    imgUrl[6] = "Images/ceshi1.jpg";
    imgUrl[7] = "Images/ceshi.jpg";

    title[1] = "測試一下1";
    title[2] = "測試一下2";
    title[3] = "測試一下3";
    title[4] = "測試一下4";
    title[5] = "測試一下5";
    title[6] = "測試一下6";
    title[7] = "測試一下7";

    description[1] = "描述一下1";
    description[2] = "描述一下2";
    description[3] = "描述一下3";
    description[4] = "描述一下4";
    description[5] = "描述一下5";
    description[6] = "描述一下6";
    description[7] = "描述一下7";

    imgLink[1] = "ceshi1.asp";
    imgLink[2] = "ceshi2.asp";
    imgLink[3] = "ceshi3.asp";
    imgLink[4] = "ceshi4.asp";
    imgLink[5] = "ceshi5.asp";
    imgLink[6] = "ceshi6.asp";
    imgLink[7] = "ceshi7.asp";

    function showImage(){
        $("#Buttons div").each(function(){
            if($(this).attr("id") == "B"+local){
                $(this).attr("class","Menu_On");
                $(this).addClass("active");
                $("#AdImage").attr("src", imgUrl[local]);
                $("#AdTitle a").html(title[local]);
                $("#AdDescription a").html(description[local]);
                $("#Images a").attr("href", imgLink[local]);
            }else{
                $(this).removeClass("active");
                $(this).attr("class","Menu_Off");
            }
        });

        local++;
        if(local > count){
            local = 1;
        }
        theTimer = setTimeout(function(){ showImage() }, 3000);
    }

    // 點擊數字按鈕直接跳到該張
    $("#Buttons div").click(function(){
        local = $(this).attr("id").replace("B","");
        var cid = $(this).attr("id");
        $("#Buttons div").each(function(){
            if($(this).attr("id") == cid){
                $(this).addClass("active");
                $("#AdImage").attr("src", imgUrl[$(this).attr("id").replace("B","")]);
                $("#AdTitle a").html(title[$(this).attr("id").replace("B","")]);
                $("#AdDescription a").html(description[$(this).attr("id").replace("B","")]);
                $("#Images a").attr("href", imgLink[$(this).attr("id").replace("B","")]);
            }else{
                $(this).removeClass("active");
                $(this).attr("class","Menu_Off");
            }
        });
        window.clearInterval(theTimer);
        showImage();
    });

    // 滑鼠移入移出時切換按鈕樣式
    $("#Buttons div").mouseover(function(){
        if($(this).attr("id") != "Top"){
            $(this).css("cursor","pointer");
        }
        if($(this).attr("class") == "Menu_Off"){
            $(this).attr("class","Menu_On");
        }
    });

    $("#Buttons div").mouseout(function(){
        if($(this).attr("class") == "Menu_On"){
            $(this).attr("class","Menu_Off");
        }
    });

    showImage();
});

二、CSS 部分

body{
    margin:0px;
    padding:0px;
    background-color:#FFFFFF;
}
table,th,tr,td,div,span,p{
    font-size:9pt;
}
a:link{
    color:#FFFFFF;
    text-decoration:none;
}
a:visited{
    color:#FFFFFF;
    text-decoration:none;
}
a:active{
    color:#FFFFFF;
    text-decoration:none;
}
a:hover{
    color:#FF0000;
    text-decoration:none;
}
#Images{
    position:relative;
    width:304px;
    height:270px;
    border:1px #b6cae3 solid;
    background-color:#FFFFFF;
}
#Titles{
    width:100%;
    height:73px;
    right:0px;
    bottom:0px;
    background-color:#000000;
    filter:alpha(opacity=40);
}
.Menu_On{
    float:right;
    width:15px;
    height:18px;
    background-color:#000000;
    margin-left:2px;
    line-height:20px;
    color:#FF0000;
}
.Menu_Off{
    float:right;
    width:15px;
    height:18px;
    background-color:#000000;
    margin-left:2px;
    line-height:20px;
    color:#FFFFFF;
}
#Top{
    float:right;
    width:47px;
    height:18px;
    background-color:#000000;
    filter:alpha(opacity=40,finishopacity=100,style=1,startx=0px,starty=18px,finishx=47px,finishy=18px);
}
#AdTitle{
    padding-top:10px;
    padding-bottom:6px;
    font-weight:bold;
    color:#FFFFFF;
    font-size:18px;
    padding-left:10px;
}
#AdDescription{
    color:#FFFFFF;
    padding-left:10px;
}

三、HTML 部分

<div id="Images">
<a href="ceshi1.asp" target="_blank"><img src="Images/ceshi.jpg" border="0" id="AdImage" /></a>
<div style="position:absolute; right:0px; bottom:0px; height:73px; width:100%;">
<div id="Titles">
<div style="position:absolute; width:100%; height:73px;">
<div id="AdTitle"><a href="ceshi1.asp" target="_blank">測試一下1</a></div>
<div id="AdDescription"><a href="ceshi1.asp" target="_blank">描述一下1</a></div>
<div id="Buttons" style="position:absolute; right:0px; bottom:0px; height:18px; width:100%;" align="right">
<div id="B7" align="center" class="Menu_Off">7</div>
<div id="B6" align="center" class="Menu_Off">6</div>
<div id="B5" align="center" class="Menu_Off">5</div>
<div id="B4" align="center" class="Menu_Off">4</div>
<div id="B3" align="center" class="Menu_Off">3</div>
<div id="B2" align="center" class="Menu_Off">2</div>
<div id="B1" align="center" class="Menu_On active">1</div>
<div id="Top"></div>
</div>
</div>
</div>
</div>
</div>

結構解析

整個效果由四個陣列驅動:imgUrl、title、description、imgLink,用同一個數字索引串起「圖片、標題、描述、連結」四項資料。showImage() 每次執行時把 local 指標對應的資料寫進頁面,再把指標往前推一格,到尾就回到頭,形成無縫循環。

底部的數字按鈕 id 命名為 B1~B7,程式碼用 replace("B","") 把 id 轉回索引數字,就能直接跳到指定畫面。

幾個可以改進的地方

  • setTimeout 與 clearInterval 混用:自動輪播用 setTimeout 遞迴呼叫,但點擊時卻用 clearInterval 清除,兩者並不相容。建議統一改用 setInterval 搭配 clearInterval,或改用 clearTimeout。
  • 用 attr("class") 切換樣式:$(this).attr("class","Menu_On") 會直接覆寫整個 class,若元素上有多個 class 就會遺失。建議改用 addClass() / removeClass() / toggleClass()。
  • filter:alpha(opacity=40) 是 IE 專屬寫法,其他瀏覽器請補上 opacity:0.4 才有半透明效果。
  • 沒有滑入暫停:使用者正在看某張圖時不該被強制切換。加上 hover 暫停、移出恢復,體驗會好很多。
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved