輪播圖組件js代碼
本文實(shí)例為大家分享了JavaScript輪播圖組件代碼,供大家參考,具體內(nèi)容如下
//輪播圖組件
function Rolling(o) {
this.index = ++o.index || 1; //當(dāng)前滾動(dòng)的位置,當(dāng)index大于可輪播的次數(shù)listLength或者等于0,為不可滾動(dòng)狀態(tài)
this.num = o.num || 1; //默認(rèn)滾動(dòng)一個(gè)列表
this.obj = o.obj || null; //要輪播的對(duì)象ul
this.perObj = o.perObj || null; //向上翻頁(yè)的按鈕對(duì)象
this.nextObj = o.nextObj || null; //向下翻頁(yè)的按鈕對(duì)象
this.focusPoint = o.focusPoint || null; //焦點(diǎn)對(duì)象,默認(rèn)為null。意思不開(kāi)啟焦點(diǎn)對(duì)象,如要開(kāi)啟可傳入焦點(diǎn)對(duì)象可自動(dòng)開(kāi)啟
this.focusClass = o.focusClass || 'mien-active'; //當(dāng)前焦點(diǎn)位置類(lèi)名
this.replaceBtn = o.replaceBtn || false;//是否在輪播到第一頁(yè)或最后一頁(yè)時(shí)替換翻頁(yè)按鈕圖片。默認(rèn)值為true,并替換按鈕圖片為re+圖片名。如:per.png替換成re-per.ping
console.log(o.replaceBtn)
this.listLength = Math.ceil(o.obj.find('li').length / this.num); //可輪播的次數(shù)
this.listObj = o.obj.children('li');
this.listWidth =this.listObj.width() + parseInt(this.listObj.css('margin-left')) + parseInt(this.listObj.css('margin-right')); //列表寬度
this.init(); //初始化
};
Rolling.prototype.init = function() {
var thisObj = this;
this.initLeft();
this.replaceFun();
if(this.focusPoint !== null) {
this.createFocusPoint();
}
this.perObj.unbind('click').on('click', function() {
thisObj.rollPrev();
});
this.nextObj.unbind('click').on('click', function() {
thisObj.rollNext();
});
}
//創(chuàng)建焦點(diǎn),并給當(dāng)前位置的焦點(diǎn)添加類(lèi)mien-active
Rolling.prototype.createFocusPoint = function() {
var str = '',
thisObj = this;
for(var i = 0; i < this.listLength; i++) {
if(i == this.index - 1) {
str += '<li class="' + this.focusClass + '"></li>';
} else {
str += '<li></li>';
}
}
this.focusPoint.append(str);
this.focusPoint.children().click(function() {
var oldIndex = $('.' + thisObj.focusClass).index() + 1; //之前的焦點(diǎn)位置
var index = $(this).index() + 1; //當(dāng)前點(diǎn)擊的焦點(diǎn)
var sum = index - oldIndex;
var perObject = thisObj.perObj.find('img');
var nextObject = thisObj.nextObj.find('img');
if (index == 1 && !thisObj.replaceBtn){
perObject.attr('src',perObject.attr('data-src'));
nextObject.attr('src',nextObject.attr('data-src'));
}else if (index == thisObj.listLength && !thisObj.replaceBtn){
perObject.attr('src',perObject.attr('re-src'));
nextObject.attr('src',nextObject.attr('re-src'));
}else if (!thisObj.replaceBtn){
perObject.attr('src',perObject.attr('re-src'));
nextObject.attr('src',nextObject.attr('data-src'));
}
thisObj.index += sum;
if(sum > 0) {
thisObj.obj.animate({
marginLeft: '-=' + Math.abs(sum) * thisObj.num * thisObj.listWidth + 'px'
});
}
if(sum < 0) {
thisObj.obj.animate({
marginLeft: '+=' + Math.abs(sum) * thisObj.num * thisObj.listWidth + 'px'
});
}
$(this).addClass(thisObj.focusClass).siblings().removeClass(thisObj.focusClass);
});
}
Rolling.prototype.initLeft = function() {
if(this.index == 1) {
return;
}
this.obj.css('margin-left', -(this.index - 1) * this.listWidth); //初始化全屏圖片顯示的位置
}
Rolling.prototype.rollPrev = function() {
--this.index;
//當(dāng)點(diǎn)擊到第一頁(yè)就return
if (this.index <= 1 && !this.replaceBtn){
this.perObj.find('img').attr('src',this.perObj.find('img').attr('data-src'));
}
if(!this.thisIndex()) {
++this.index;
return;
}
if (!this.replaceBtn){
this.nextObj.find('img').attr('src',this.nextObj.find('img').attr('data-src'));
}
this.obj.animate({
marginLeft: '+=' + this.num * this.listWidth + 'px'
});
if(this.focusPoint !== null) {
$('.' + this.focusClass).removeClass(this.focusClass).prev().addClass(this.focusClass);
}
}
Rolling.prototype.rollNext = function() {
++this.index;
if (this.index == this.listLength && !this.replaceBtn){
this.nextObj.find('img').attr('src',this.nextObj.find('img').attr('re-src'));
}
//當(dāng)點(diǎn)擊到最后一頁(yè)就return
if(!this.thisIndex()) {
--this.index;
return;
}
if (!this.replaceBtn){
this.perObj.find('img').attr('src',this.perObj.find('img').attr('re-src'));
}
this.obj.animate({
marginLeft: '-=' + this.num * this.listWidth + 'px'
});
if(this.focusPoint !== null) {
$('.' + this.focusClass).removeClass(this.focusClass).next().addClass(this.focusClass);
}
}
Rolling.prototype.replaceFun = function(){
var perObject = this.perObj.find('img'),
nextObject = this.nextObj.find('img');
var perSrc = perObject.attr('src'),
nextSrc = nextObject.attr('src');
perObject.attr({'data-src':perSrc,'re-src':this.splitSrc(perSrc)});
nextObject.attr({'data-src':nextSrc,'re-src':this.splitSrc(nextSrc)});
}
Rolling.prototype.splitSrc = function(str){
var list = str.split('/');
var data = list[list.length-1];
var sub = data.substr(0,data.indexOf('.'));
return str.replace(sub,'re-' + sub);
}
Rolling.prototype.thisIndex = function() {
if(this.index > this.listLength | this.index <= 0) {
return false;
}
return true;
}
function createRolling(o) {
return new Rolling(o);
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- jQuery自適應(yīng)輪播圖插件Swiper用法示例
- jQuery插件slides實(shí)現(xiàn)無(wú)縫輪播圖特效
- jquery制作多功能輪播圖插件
- 原生js實(shí)現(xiàn)網(wǎng)易輪播圖效果
- javascript經(jīng)典特效分享 手風(fēng)琴、輪播圖、圖片滑動(dòng)
- 利用AngularJs實(shí)現(xiàn)京東首頁(yè)輪播圖效果
- js 基礎(chǔ)篇必看(點(diǎn)擊事件輪播圖的簡(jiǎn)單實(shí)現(xiàn))
- 簡(jiǎn)單的JS輪播圖代碼
- js實(shí)現(xiàn)支持手機(jī)滑動(dòng)切換的輪播圖片效果實(shí)例
- 原生JS輪播圖插件
相關(guān)文章
詳解JS去重及字符串奇數(shù)位小寫(xiě)轉(zhuǎn)大寫(xiě)
本篇文章主要介紹了詳解JS去重及字符串奇數(shù)位小寫(xiě)轉(zhuǎn)大寫(xiě) ,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2016-12-12
JavaScript實(shí)現(xiàn)防止網(wǎng)頁(yè)被嵌入Frame框架的代碼分享
這篇文章主要介紹了JavaScript實(shí)現(xiàn)防止網(wǎng)頁(yè)被嵌入Frame框架的代碼分享,本文給出了2種防嵌入方法,需要的朋友可以參考下2014-12-12
使用D3.js構(gòu)建實(shí)時(shí)圖形的示例代碼
這篇文章主要介紹了使用D3.js構(gòu)建實(shí)時(shí)圖形的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
淺析JS刷新框架中的其他頁(yè)面 && JS刷新窗口方法匯總
本篇文章是對(duì)JS刷新框架中的其他頁(yè)面以及JS刷新窗口的方法進(jìn)行了匯總介紹,需要的朋友可以參考下2013-07-07
JS中把函數(shù)作為另一函數(shù)的參數(shù)傳遞方法(總結(jié))
下面小編就為大家?guī)?lái)一篇JS中把函數(shù)作為另一函數(shù)的參數(shù)傳遞方法(總結(jié))。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06
JSONP獲取Twitter和Facebook文章數(shù)的具體步驟
這篇文章主要介紹了JSONP獲取Twitter和Facebook文章數(shù)的方法,需要的朋友可以參考下2014-02-02
js百度地圖鼠標(biāo)滾輪縮放導(dǎo)致地圖中心點(diǎn)偏移問(wèn)題
本文主要介紹了js百度地圖鼠標(biāo)滾輪縮放導(dǎo)致地圖中心點(diǎn)偏移問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
javascript檢測(cè)flash插件是否被禁用的方法
這篇文章主要介紹了javascript檢測(cè)flash插件是否被禁用的方法,涉及JavaScript調(diào)用ActiveXObject組件操作flash插件的相關(guān)技巧,需要的朋友可以參考下2016-01-01
JavaScript組件焦點(diǎn)與頁(yè)內(nèi)錨點(diǎn)間傳值的方法
這篇文章主要介紹了JavaScript組件焦點(diǎn)與頁(yè)內(nèi)錨點(diǎn)間傳值的方法,涉及輸入框與錨點(diǎn)的操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-02-02

