jQuery簡(jiǎn)單自定義圖片輪播插件及用法示例
本文實(shí)例講述了jQuery簡(jiǎn)單自定義圖片輪播插件及用法。分享給大家供大家參考,具體如下:
經(jīng)常使用別人的插件,現(xiàn)在自己寫(xiě)一個(gè),紀(jì)念一下。
jQuery.banner.js:
/*
* banner 0.1
* 使用banner 實(shí)現(xiàn)圖片定時(shí)切換 鼠標(biāo)經(jīng)過(guò)停止動(dòng)畫(huà)
* 鼠標(biāo)離開(kāi),繼續(xù)動(dòng)畫(huà)
*/
;(function($){
$.fn.banner =function(options){
//各種屬性和參數(shù)
var defaults ={
picWidth:"1000",
picHeight:"300",
speed:"1500"
};
var totalW = 0; //保存總的動(dòng)畫(huà)寬度
var timer = null; //保存定時(shí)器
var current = 0; //保存當(dāng)前動(dòng)畫(huà)到第N張圖,下次從這里開(kāi)始
var totalNum = 0; //保存總的圖數(shù)
var Dsqtime = 0; //定義定時(shí)器時(shí)間 【外傳參數(shù)】
var Dhtime = 0; //定義動(dòng)畫(huà)時(shí)間
var count = 0 ;
//合并多個(gè)對(duì)象為一個(gè),即有新參數(shù) 用新的,否則用默認(rèn)的
var options = $.extend(defaults, options);
this.each(function(){
//實(shí)現(xiàn)代碼
var __this = $(this);
Dsqtime = options.speed;
Dhtime = Dsqtime/3;
//初始化
init(__this);
//調(diào)用動(dòng)畫(huà)
show(__this, options.picWidth,current);
//鼠標(biāo)經(jīng)過(guò)時(shí)事件
__this.find('ul li').bind('mouseover',function(){
window.clearInterval(timer); //清除定時(shí)器
});
__this.find('ul li').bind('mouseout',function(){
show(__this, options.picWidth,current);
//接著上一次動(dòng)畫(huà)輪播
});
});
//初始化 設(shè)定父容器寬度
function init(obj){
obj.find('ul li').each(function(){
totalW += $(this).width();
totalNum++;
});
obj.find('ul').width(totalW);
}
//開(kāi)始動(dòng)畫(huà)顯示
function show(obj, width, current){
timer = setInterval(function(){
obj.find('ul').animate({'margin-left':'-'+count*width+'px'},
Dhtime);
current = count;
count++;
if(count == totalNum){
count =0;
}
}, Dsqtime);
}
};
})(jQuery);
html代碼:
<!doctype html>
<html>
<head>
<meta charset="utf8"/>
<script type="text/javascript" src="./js/jquery.min.js"></script>
<script type="text/javascript" src="./js/jquery.banner.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('.wrap').banner({
picWidth:"1000",
picHeight:"300",
speed:"6000"
});
});
</script>
<style type="text/css">
*{margin:0;padding:0;}
.wrap{width:1000px; height:300px; overflow:hidden; margin:0 auto;}
.wrap ul li{float:left; list-style:none;}
.wrap ul li img{width:1000px;height:300px;}
.clear{clear: both;}
</style>
</head>
<body>
<div>
<div class="wrap">
<ul>
<li><a href="#"><img src="./images/1.jpg"/></a></li>
<li><a href="#"><img src="./images/2.jpg"/></a></li>
<li><a href="#"><img src="./images/3.jpg"/></a></li>
<li><a href="#"><img src="./images/4.jpg"/></a></li>
<li><a href="#"><img src="./images/5.jpg"/></a></li>
</ul>
<div class="clear"></div>
</div>
</div>
</body>
</html>
效果圖:

更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《jQuery常用插件及用法總結(jié)》、《jQuery擴(kuò)展技巧總結(jié)》、《jQuery切換特效與技巧總結(jié)》、《jQuery遍歷算法與技巧總結(jié)》、《jQuery常見(jiàn)經(jīng)典特效匯總》、《jQuery動(dòng)畫(huà)與特效用法總結(jié)》及《jquery選擇器用法總結(jié)》
希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。
- 原生js和jquery實(shí)現(xiàn)圖片輪播淡入淡出效果
- 基于jQuery實(shí)現(xiàn)淡入淡出效果輪播圖
- 原生js和jQuery實(shí)現(xiàn)淡入淡出輪播效果
- jQuery實(shí)現(xiàn)圖片簡(jiǎn)單輪播功能示例
- jQuery實(shí)現(xiàn)的簡(jiǎn)單圖片輪播效果完整示例
- 使用JQuery實(shí)現(xiàn)圖片輪播效果的實(shí)例(推薦)
- 純javaScript、jQuery實(shí)現(xiàn)個(gè)性化圖片輪播【推薦】
- jQuery實(shí)現(xiàn)的圖片輪播效果完整示例
- jQuery的圖片輪播插件PgwSlideshow使用詳解
- jQuery實(shí)現(xiàn)圖片輪播效果代碼(基于jquery.pack.js插件)
- jQuery實(shí)現(xiàn)的淡入淡出圖片輪播效果示例
相關(guān)文章
jQuery實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊彈出漸變層的方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊彈出漸變層的方法,jQuery是當(dāng)下最具人氣的JavaScript庫(kù),需要的朋友可以參考下2015-07-07
簡(jiǎn)單分析javascript面向?qū)ο笈c原型
為了說(shuō)明 JavaScript 是一門(mén)徹底的面向?qū)ο蟮恼Z(yǔ)言,首先有必要從面向?qū)ο蟮母拍钪?, 探討一下面向?qū)ο笾械膸讉€(gè)概念:1.一切事物皆對(duì)象,2.對(duì)象具有封裝和繼承特性,3.對(duì)象與對(duì)象之間使用消息通信,各自存在信息隱藏2015-05-05
基于jQuery實(shí)現(xiàn)音樂(lè)播放試聽(tīng)列表
這篇文章主要介紹了基于jQuery實(shí)現(xiàn)音樂(lè)播放試聽(tīng)列表的相關(guān)資料,需要的朋友可以參考下2016-04-04
jQuery實(shí)現(xiàn)點(diǎn)擊水紋波動(dòng)動(dòng)畫(huà)
今天要為大家紹一款由jquery實(shí)現(xiàn)的鼠標(biāo)單擊出現(xiàn)水波特效。用鼠標(biāo)點(diǎn)擊頁(yè)面,你可以看到頁(yè)面不斷出面水波紋效果。然后水波紋漸漸消失。效果非常不錯(cuò)2016-04-04
jQuery 如何實(shí)現(xiàn)一個(gè)滑動(dòng)按鈕開(kāi)關(guān)
本文給大家分享一段jquery代碼實(shí)現(xiàn)滑動(dòng)按鈕開(kāi)關(guān)的效果,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的的朋友參考下2016-12-12

