首頁  >  文章  >  web前端  >  原生javascript實作圖片按鈕切換_javascript技巧

原生javascript實作圖片按鈕切換_javascript技巧

WBOY
WBOY原創
2016-05-16 16:20:591415瀏覽

先給大家看下效果展示圖

以下為詳細程式碼:

複製程式碼 程式碼如下:

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; //圖片總數
    this.nScollCount = option.scrollCount; //每次捲動的數量
    this.nScollLen = Math.ceil(this.nLen/option.scrollCount); // 切換判斷的最大值
    this.nSwitchWidth = 0; //每次切換移動的距離,在程式碼裡面動態取得值
    this.nIndex = 0; //切換圖片的目前索引
    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                 if ( pattern.test(els[i].className) ) {
                        類項[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:函數(){
        this.moveFn(this.oUl,'marginLeft',-this.nIndex*this.nSwitchWidth);
    },
    cancelBubble:函數(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(){
        //動態取得移動的寬度
        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結構必須是以下:外層ID名,自己傳入 如下面的:id="gy_picSwitch02" ,ID名,自己隨便給
但,裡面的結構必需一樣,包含類別名稱classname

   
   
   

       

               

  •            

  •            

  •            

  •            

  •            

  •            

  •            

  •        

   


參數:'wrapID':'xxxx',最外層的ID名稱
      'scrollCount':5,滾動的數量  

複製程式碼 程式碼如下:
  
*
*/
//實例化
 new LGY_picSwitch({'wrapID':'gy_picSwitch','scrollCount':5});

是不是很方便的功能呢,使用也很簡單,這裡推薦給小夥伴,希望對大家能有所幫助

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn