jQuery 一個(gè)圖片切換的插件
更新時(shí)間:2011年10月09日 23:10:25 作者:
B/S開發(fā)的朋友,首頁(yè)常常需要一些新聞圖片切換的特效,鑒于jQuery良好的插件開發(fā)機(jī)制,我也常常自己寫一些實(shí)用的小插件,這里分享一個(gè)新聞圖片切換插件
以下是參數(shù)說明:
插件代碼及調(diào)用
- 插件名稱:ImageScroll
(function($){
$.fn.ImageScroll = function(options) {
var defaults = {
delay: 2000,
maskOpacity: 0.6,
numOpacity: 0.6,
maskBgColor: "#000",
textColor: "#fff",
numColor: "#fff",
numBgColor: "#000",
alterColor: "#fff",
alterBgColor: "#999"
};
options = $.extend(defaults, options);
var _this = $(this).css("display", "none");
var _links = [], _texts = [], _urls = [];
var _list = _this.find("a");
var _timer;
var _index = 0;
_list.each(function(index){
var temp = $(this).find("img:eq(0)");
_links.push($(this).attr("href"));
_texts.push(temp.attr("alt"));
_urls.push(temp.attr("src"));
});
if(_list.length <= 0) {
return;
}
else {
_this.html("");
}
var _width = _this.width();
var _height = _this.height();
var _numCount = _list.length;
var _numColumn = Math.ceil(_numCount / 2);
var _img = $("<a/>").css({"display":"block", "position":"absolute","top":"0px","left":"0px", "z-index":"2", "width":_width+"px", "height":_height+"px", "background":"url("+_urls[0]+")"}).appendTo(_this);
var _mask = $("<div/>").attr("style","opacity:"+options.maskOpacity)
.css({"position":"absolute", "left":"0px", "bottom":"0px", "z-index":"3", "width":_width+"px", "height":"46px", "opacity":options.maskOpacity, "background-color":options.maskBgColor}).appendTo(_this);
var _num = $("<div/>").attr("style","opacity:"+options.numOpacity)
.css({"position":"absolute", "right":"0px", "bottom":"0px", "z-index":"5", "width":_numColumn*22, "opacity":options.numOpacity, "height":"44px"}).appendTo(_this);
var _text = $("<div/>").css({"position":"absolute", "left":"0px", "bottom":"0px", "z-index":"4", "padding-left":"10px", "height":"44px", "line-height":"44px", "color":options.textColor}).html(_texts[0]).appendTo(_this);
for(var i = 0; i < _numCount; i++)
{
$("<a/>").html(i+1)
.css({"float":"left", "width":"20px", "height":"20px", "text-align":"center", "background-color":options.numBgColor, "margin":"0px 2px 2px 0px", "cursor":"pointer", "line-height":"20px", "color":options.numColor})
.mouseover(function() {
if(_timer) {
clearInterval(_timer);
}
}).mouseout(function() {
_timer = setInterval(alter, options.delay);
}).click(function(){
numClick($(this));
}).appendTo(_num);
}
var _tempList = _num.find("a");
function alter() {
if(_index > _numCount-1) {
_index = 0;
}
_tempList.eq(_index).click();
}
function numClick(obj) {
var i = _tempList.index(obj);
_tempList.css({"background-color":options.numBgColor, "color":options.numColor});
obj.css({"background-color":options.alterBgColor, "color":options.alterColor});
_img.attr({"href":_links[i], "target":"_blank"})
.css({"opacity":"0", "background":"url("+_urls[i]+")"})
.animate({"opacity":"1"}, 500);
_text.html(_texts[i]);
_index = i + 1;
}
setTimeout(alter, 10);
_timer = setInterval(alter, options.delay);
_this.css("display", "block");
};
})(jQuery);
- 調(diào)用代碼
<script src="js/jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="js/jquery.ImageScroll.js" type="text/javascript"></script>
<style type="text/css">
<!--
body {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
}
#scroll { position:relative; width:450px; height:300px; }
-->
</style>
<div id="scroll">
<a ><img src="images/1.jpg" alt="漂亮的風(fēng)景圖片一" /></a>
<a href="http://www.dbjr.com.cn"><img src="images/2.jpg" alt="漂亮的風(fēng)景圖片二" /></a>
<a ><img src="images/3.jpg" alt="漂亮的風(fēng)景圖片三" /></a>
<a ><img src="images/4.jpg" alt="漂亮的風(fēng)景圖片四" /></a>
<a ><img src="images/5.jpg" alt="漂亮的風(fēng)景圖片五" /></a>
<a ><img src="images/3.jpg" alt="漂亮的風(fēng)景圖片六" /></a>
</div>
<script>
$("#scroll").ImageScroll();
</script>
- 運(yùn)行結(jié)果
<script>
$("#scroll").ImageScroll({delay:500, maskBgColor:"#ff0000"});
</script>
- 運(yùn)行結(jié)果

小結(jié)
只是個(gè)小插件,可定制性可能不是很好,大家隨便看看和修改好了,貌似IE8好像還有個(gè)小bug,一會(huì)修正了再上傳,大家有什么bug發(fā)現(xiàn),請(qǐng)回復(fù)告訴我,方便我及時(shí)修正,有代碼上的優(yōu)化意見,也請(qǐng)告訴我,幫助我這個(gè)新人學(xué)習(xí),=.=
參數(shù)名稱 | 描述 |
delay | 圖片切換速度,單位毫秒 |
maskOpacity | 遮罩層透明度,1為不透明,0為全透明 |
numOpacity | 數(shù)字按鈕透明度,1為不透明,0為全透明 |
maskBgColor | 遮罩層背景色 |
textColor | 標(biāo)題字體顏色 |
numColor | 數(shù)字按鈕字體顏色 |
numBgColor | 數(shù)字按鈕背景色 |
alterColor | 數(shù)字按鈕選中項(xiàng)字體顏色 |
alterBgColor | 數(shù)字按鈕選中項(xiàng)背景顏色 |
- 插件名稱:ImageScroll
復(fù)制代碼 代碼如下:
(function($){
$.fn.ImageScroll = function(options) {
var defaults = {
delay: 2000,
maskOpacity: 0.6,
numOpacity: 0.6,
maskBgColor: "#000",
textColor: "#fff",
numColor: "#fff",
numBgColor: "#000",
alterColor: "#fff",
alterBgColor: "#999"
};
options = $.extend(defaults, options);
var _this = $(this).css("display", "none");
var _links = [], _texts = [], _urls = [];
var _list = _this.find("a");
var _timer;
var _index = 0;
_list.each(function(index){
var temp = $(this).find("img:eq(0)");
_links.push($(this).attr("href"));
_texts.push(temp.attr("alt"));
_urls.push(temp.attr("src"));
});
if(_list.length <= 0) {
return;
}
else {
_this.html("");
}
var _width = _this.width();
var _height = _this.height();
var _numCount = _list.length;
var _numColumn = Math.ceil(_numCount / 2);
var _img = $("<a/>").css({"display":"block", "position":"absolute","top":"0px","left":"0px", "z-index":"2", "width":_width+"px", "height":_height+"px", "background":"url("+_urls[0]+")"}).appendTo(_this);
var _mask = $("<div/>").attr("style","opacity:"+options.maskOpacity)
.css({"position":"absolute", "left":"0px", "bottom":"0px", "z-index":"3", "width":_width+"px", "height":"46px", "opacity":options.maskOpacity, "background-color":options.maskBgColor}).appendTo(_this);
var _num = $("<div/>").attr("style","opacity:"+options.numOpacity)
.css({"position":"absolute", "right":"0px", "bottom":"0px", "z-index":"5", "width":_numColumn*22, "opacity":options.numOpacity, "height":"44px"}).appendTo(_this);
var _text = $("<div/>").css({"position":"absolute", "left":"0px", "bottom":"0px", "z-index":"4", "padding-left":"10px", "height":"44px", "line-height":"44px", "color":options.textColor}).html(_texts[0]).appendTo(_this);
for(var i = 0; i < _numCount; i++)
{
$("<a/>").html(i+1)
.css({"float":"left", "width":"20px", "height":"20px", "text-align":"center", "background-color":options.numBgColor, "margin":"0px 2px 2px 0px", "cursor":"pointer", "line-height":"20px", "color":options.numColor})
.mouseover(function() {
if(_timer) {
clearInterval(_timer);
}
}).mouseout(function() {
_timer = setInterval(alter, options.delay);
}).click(function(){
numClick($(this));
}).appendTo(_num);
}
var _tempList = _num.find("a");
function alter() {
if(_index > _numCount-1) {
_index = 0;
}
_tempList.eq(_index).click();
}
function numClick(obj) {
var i = _tempList.index(obj);
_tempList.css({"background-color":options.numBgColor, "color":options.numColor});
obj.css({"background-color":options.alterBgColor, "color":options.alterColor});
_img.attr({"href":_links[i], "target":"_blank"})
.css({"opacity":"0", "background":"url("+_urls[i]+")"})
.animate({"opacity":"1"}, 500);
_text.html(_texts[i]);
_index = i + 1;
}
setTimeout(alter, 10);
_timer = setInterval(alter, options.delay);
_this.css("display", "block");
};
})(jQuery);
- 調(diào)用代碼
復(fù)制代碼 代碼如下:
<script src="js/jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="js/jquery.ImageScroll.js" type="text/javascript"></script>
<style type="text/css">
<!--
body {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
}
#scroll { position:relative; width:450px; height:300px; }
-->
</style>
<div id="scroll">
<a ><img src="images/1.jpg" alt="漂亮的風(fēng)景圖片一" /></a>
<a href="http://www.dbjr.com.cn"><img src="images/2.jpg" alt="漂亮的風(fēng)景圖片二" /></a>
<a ><img src="images/3.jpg" alt="漂亮的風(fēng)景圖片三" /></a>
<a ><img src="images/4.jpg" alt="漂亮的風(fēng)景圖片四" /></a>
<a ><img src="images/5.jpg" alt="漂亮的風(fēng)景圖片五" /></a>
<a ><img src="images/3.jpg" alt="漂亮的風(fēng)景圖片六" /></a>
</div>
<script>
$("#scroll").ImageScroll();
</script>
- 運(yùn)行結(jié)果
- 帶參數(shù)調(diào)用
復(fù)制代碼 代碼如下:
<script>
$("#scroll").ImageScroll({delay:500, maskBgColor:"#ff0000"});
</script>
- 運(yùn)行結(jié)果

小結(jié)
只是個(gè)小插件,可定制性可能不是很好,大家隨便看看和修改好了,貌似IE8好像還有個(gè)小bug,一會(huì)修正了再上傳,大家有什么bug發(fā)現(xiàn),請(qǐng)回復(fù)告訴我,方便我及時(shí)修正,有代碼上的優(yōu)化意見,也請(qǐng)告訴我,幫助我這個(gè)新人學(xué)習(xí),=.=
您可能感興趣的文章:
- 基于Jquery的簡(jiǎn)單圖片切換效果
- 基于jquery實(shí)現(xiàn)左右按鈕點(diǎn)擊的圖片切換效果
- jQuery插件Slider Revolution實(shí)現(xiàn)響應(yīng)動(dòng)畫滑動(dòng)圖片切換效果
- 一個(gè)基于jquery的圖片切換效果
- jQuery實(shí)現(xiàn)的Tab滑動(dòng)選項(xiàng)卡及圖片切換(多種效果)小結(jié)
- jQuery簡(jiǎn)單實(shí)現(xiàn)banner圖片切換
- jQuery插件slick實(shí)現(xiàn)響應(yīng)式移動(dòng)端幻燈片圖片切換特效
- JQuery頁(yè)面圖片切換和新聞列表滾動(dòng)效果的具體實(shí)現(xiàn)
- jQuery圖片切換插件jquery.cycle.js使用示例
- jQuery實(shí)現(xiàn)圖片切換效果
相關(guān)文章
jquery根據(jù)td給相同tr下其他td賦值的實(shí)現(xiàn)方法
下面就為大家?guī)?lái)一篇jquery根據(jù)td給相同tr下其他td賦值的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2016-10-10JQuery Ajax跨域調(diào)用和非跨域調(diào)用問題實(shí)例分析
這篇文章主要介紹了JQuery Ajax跨域調(diào)用和非跨域調(diào)用問題,結(jié)合具體實(shí)例形式分析了jQuery基于ajax的非跨域請(qǐng)求及跨域請(qǐng)求相關(guān)實(shí)現(xiàn)技巧與操作注意事項(xiàng),需要的朋友可以參考下2019-04-04jquery實(shí)現(xiàn)浮動(dòng)的側(cè)欄實(shí)例
這篇文章主要介紹了jquery實(shí)現(xiàn)浮動(dòng)的側(cè)欄,實(shí)例分析了基于jQuery的stickySidebar插件實(shí)現(xiàn)浮動(dòng)層的相關(guān)技巧,需要的朋友可以參考下2015-06-06- 這篇文章主要介紹了jQuery Select下拉框操作小結(jié)(推薦)的相關(guān)資料,非常實(shí)用,在前端開發(fā)經(jīng)??梢杂玫剑枰呐笥芽梢詤⒖枷?/div> 2016-07-07
jQuery $.each遍歷對(duì)象、數(shù)組用法實(shí)例
這篇文章主要介紹了jQuery $.each遍歷對(duì)象、數(shù)組用法實(shí)例,本文講解了在有參數(shù)和無(wú)參數(shù)的情況下遍歷對(duì)象及遍歷數(shù)組的例子及each方法的幾種常用用法,需要的朋友可以參考下2015-04-04最新評(píng)論