原生javascript實(shí)現(xiàn)圖片按鈕切換
先給大家看下效果展示圖
以下為詳細(xì)代碼:
function LGY_picSwitch(option){
this.oWrap = this.getId(option.wrapID); //最外層元素
this.olistWrap = this.getNodeByClassname(this.oWrap,'gy_picSwitch_listWrap')[0];
this.oUl = this.olistWrap.getElementsByTagName('ul')[0];
this.oBtnPrev = this.getNodeByClassname(this.oWrap,'gy_picSwitch_prev')[0];
this.oBtnNext = this.getNodeByClassname(this.oWrap,'gy_picSwitch_next')[0];
this.nLen = this.oUl.getElementsByTagName('li').length; //圖片總數(shù)
this.nScollCount = option.scrollCount; //每次滾動(dòng)的數(shù)量
this.nScollLen = Math.ceil(this.nLen/option.scrollCount); // 切換判斷的最大值
this.nSwitchWidth = 0; //每次切換移動(dòng)的距離,在代碼里面動(dòng)態(tài)獲取值
this.nIndex = 0; //切換圖片的當(dāng)前索引
this.timer = null; //切換圖片的引值
this.int();
}
LGY_picSwitch.prototype = {
getId:function(id){
return document.getElementById(id);
},
getNodeByClassname:function(parent,classname){
var classElements = new Array();
var els = parent.getElementsByTagName('*');
var elsLen = els.length;
var pattern = new RegExp("(^|\\s)"+classname+"(\\s|$)");
for (i = 0, j = 0; i < elsLen; i++) {
if ( pattern.test(els[i].className) ) {
classElements[j] = els[i];
j++;
}
}
return classElements;
},
getCss:function(node,value)
{
return node.currentStyle?node.currentStyle[value]:getComputedStyle(node,null)[value];
},
setCss:function(node,val){
for(var v in val){
node.style.cssText += ';'+ v +':'+val[v];
}
},
moveFn:function(node,value,targetValue,callback){
var _that = this;
clearInterval(this.timer);
this.timer = setInterval(function()
{
var val = parseFloat(_that.getCss(node,value));
var speed = ( targetValue- val )/8;
speed = speed>0?Math.ceil(speed):Math.floor(speed);
if(speed ==0)
{
clearInterval(_that.timer);
callback&&callback();
}
else
{
node.style[value] = ( val + speed ) +'px';
}
},20);
},
picChange:function(){
this.moveFn(this.oUl,'marginLeft',-this.nIndex*this.nSwitchWidth);
},
cancelBubble:function(e){
e.stopPropagation?e.stopPropagation():e.cancelBubble = true;
},
btnIsShow:function(){
this.setCss(this.oBtnNext,{'display':'block'});
this.setCss(this.oBtnPrev,{'display':'block'});
if( this.nIndex == 0 ) this.setCss(this.oBtnPrev,{'display':'none'});
if( this.nIndex ==(this.nScollLen-1) ) this.setCss(this.oBtnNext,{'display':'none'});
},
btnPrev:function(){
var _that = this;
this.oBtnPrev.onclick = function(e){
var e = e || window.event;
_that.cancelBubble(e);
if(_that.nIndex != 0 ) {
_that.nIndex--;
_that.picChange();
_that.btnIsShow();
}
}
},
btnNext:function(){
var _that = this;
this.oBtnNext.onclick = function(e){
var e = e || window.event;
_that.cancelBubble(e);
if(_that.nIndex != (_that.nScollLen-1) ) {
_that.nIndex++;
_that.picChange();
_that.btnIsShow();
}
}
},
int:function(){
//動(dòng)態(tài)獲取移動(dòng)的寬度
var oLi = this.oUl.getElementsByTagName('li')[0],
oLi_w = oLi.offsetWidth + parseInt(this.getCss(oLi,'marginLeft')) + parseInt(this.getCss(oLi,'marginRight'));
this.nSwitchWidth = oLi_w*this.nScollCount;
//按鈕顯示初始化
this.btnIsShow();
//左右切換
this.btnPrev();
this.btnNext();
}
}
HTML代碼:
/*
* HTML結(jié)構(gòu)必需是以下:外層ID名,自己傳入 如下面的:id="gy_picSwitch02" ,ID名,自己隨便給
但,里面的結(jié)構(gòu)必需一樣,包括類名classname
<div id="gy_picSwitch02">
<span class="gy_picSwitch_prev"></span>
<span class="gy_picSwitch_next"></span>
<div class="gy_picSwitch_listWrap">
<ul>
<li><img src="images/pic01.jpg" alt=""></li>
<li><img src="images/pic02.jpg" alt=""></li>
<li><img src="images/pic03.jpg" alt=""></li>
<li><img src="images/pic04.jpg" alt=""></li>
<li><img src="images/pic05.jpg" alt=""></li>
<li><img src="images/pic06.jpg" alt=""></li>
<li><img src="images/pic07.jpg" alt=""></li>
<li><img src="images/pic08.jpg" alt=""></li>
</ul>
</div>
</div>
參數(shù):'wrapID':'xxxx',最外層的ID名
'scrollCount':5,滾動(dòng)的數(shù)量
*
*/
//實(shí)例化
new LGY_picSwitch({'wrapID':'gy_picSwitch','scrollCount':5});
是不是很方便的功能呢,使用也很簡(jiǎn)單,這里推薦給小伙伴,希望對(duì)大家能有所幫助
相關(guān)文章
javascript實(shí)現(xiàn)移動(dòng)端 HTML5 圖片上傳預(yù)覽和壓縮功能示例
這篇文章主要介紹了javascript實(shí)現(xiàn)移動(dòng)端 HTML5 圖片上傳預(yù)覽和壓縮功能,結(jié)合實(shí)例形式分析了javascript移動(dòng)端 HTML5 圖片上傳預(yù)覽和壓縮功能具體實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下2020-05-05javascript 模擬JQuery的Ready方法實(shí)現(xiàn)并出現(xiàn)的問(wèn)題
今天在閱讀網(wǎng)上一些模擬Jq的ready方法時(shí),發(fā)現(xiàn)一些小細(xì)節(jié),就是網(wǎng)上的ready事件大部分都是在onload事件執(zhí)行后加載,而jquery確能在onload加載前。2009-12-12JS運(yùn)動(dòng)特效之任意值添加運(yùn)動(dòng)的方法分析
這篇文章主要介紹了JS運(yùn)動(dòng)特效之任意值添加運(yùn)動(dòng)的方法,結(jié)合實(shí)例形式分析了javascript針對(duì)多物體運(yùn)動(dòng)通過(guò)參數(shù)控制不同運(yùn)動(dòng)效果的實(shí)現(xiàn)方法,需要的朋友可以參考下2018-01-01JavaScript判斷空值、NULL、undefined的方法對(duì)比
JavaScript五種原始類型(boolean、number、string、null、undefined)中的一種。在鑒別JavaScript原始類型的時(shí)候我們會(huì)用到typeof操作符。Typeof操作符可用于字符串、數(shù)字、布爾和未定義類型。2022-12-12BootStrap daterangepicker 雙日歷控件
這篇文章主要介紹了BootStrap daterangepicker 雙日歷控件,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-06-06JavaScript 冒泡排序和選擇排序的實(shí)現(xiàn)代碼
本文通過(guò)實(shí)例代碼給大家介紹了js冒泡排序和選擇排序的實(shí)現(xiàn)代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧2016-09-09