效果圖:
代碼中存在的錯誤歡迎大家指正
代碼如下:
/**
* @author leepood
* @title 圖片自動輪換效果
* @version v2.0
* @E-Mail leepood@gmail.com
* @notice:要顯示圖片的數目可以在setting裡調整,但需要在imagesArray中加入相應圖片的參數
*/
function changeImages()
{
var setting={
'width':'330px',
'height':'200px',
'images_count':'4',
'time':'1800', //圖片切換的速度
'imageschange_border_color':'#fcf0a1'
};
var imagesArray=[{'src':'images/1.bmp','href':'http://www.baidu.com/','title':'秋天,一片金色','target':'_blank'},
{'src':'images/2.bmp','href':'http://www.163.com','title':'春天,給人一片生機的感覺','target':'_blank'},
{'src':'images/3.bmp','href':'http://iagyje.blog.163.com','title':'龍眼最好吃了,我每次吃好多的','target':'_blank'},
{'src':'images/4.bmp','href':'http://www.sina.com/','title':'火紅的楓葉啊,有機會看看去','target':'_blank'}];
//添加元素,定義變量
var $div_imageschange=$("#imageschange");
$div_imageschange.children().css("margin","0px").css("padding","0px");
$div_imageschange.append("<div id='images_button'></div>");
$div_imageschange.append("<div id='images_title'></div>");
var $div_images_title=$("#images_title");
var $div_images_button=$("#images_button");
var count=setting.images_count;
for(var a=0;a<count;a++)
{
var b=a+1;
$div_images_button.append("<a id='"+b+"'>"+b+"</a>");
}
var $link_buttons=$("#imageschange a");
//設置元素的初始屬性
//最外層容器,容納所有元素
$div_imageschange.css("width",setting.width)
.css("position","relative")
.css("height",setting.height)
.css("border","solid 1px "+setting.imageschange_border_color);
//容納按鈕的元素
$div_images_button.css("position","absolute")
.css("height","20px")
.css("right","0px")
.css("bottom","21px")
.css("opacity","1")
.css("float","right");
//容納文字說明的元素
$div_images_title.css("position","absolute")
.css("height","20px")
.css("width",setting.width)
.css("bottom","0px")
.css("right","0px")
.css("background-color","black")
.css("opacity","0.6")
.css("font-size","12px")
.css("color","white");
//按鈕組合
$link_buttons.css("width","15px")
.css("height","15px")
.css("border","solid 1px #fcf0a1")
.css("display","block")
.css("margin","0 5px 5px 5px")
.css("font-size","12px")
.css("text-align","center")
.css("float","left")
.css("color","white")
.css("text-decoration","none");
//初始化設定
$div_imageschange.css("background-image","url('images/1.bmp')");
$div_images_title.html(imagesArray[0].title);
$("#images_button a:first").css("background","#fcf0a1");
function change(index){
$div_imageschange.css("background-image", "none");
$div_imageschange.css("background-image", "url('" + imagesArray[index].src + "')");
$div_images_title.html("");
$div_images_title.html(imagesArray[index].title);
$($link_buttons[index]).attr("href", imagesArray[index].href).attr("target", imagesArray[index].target);
$link_buttons.css("background","");
$($link_buttons[index]).css("background","#fcf0a1");
};
//自動切換代碼
function autochange(){
var i=0;
setInterval(function(){
change(i);
if(i==setting.images_count-1)
{
i=-1;
}
i++;
},setting.time);
}
autochange();
//手動切換代碼
$link_buttons.each(function(i){
$(this).hover(function(){
change(i);
});
});
};
$(document).ready(function(){
changeImages();
});
要顯示圖片的數目可以在setting裡調整,但需要在imagesArray中加入相應圖片的參數