jQuery圖片滾動圖片的效果(另類實現)

來源:互聯網
上載者:User

需求 : 圖片切換的時候下一屏不允許出現空白的項,換句話說就是 :

  1、當移動的最後一屏移動的個數小於要展示的個數的時候 ,只移動(展示個數-最後一屏的個數的)差值。 舉個例子: 每一屏都要展示7個,但總個數才10個,滾動到下一屏時候使用者看到的還是7個,這個時候需要移動的是三個

這個效果是基於jQuery寫的,只是想紀念下自己的學習 話不多說了,貼代碼

複製代碼 代碼如下:
(function( $ ){
     var slider = function( elem , args ){
         this.config = $.extend({
             effect   : 'x', //效果  水平 - x
             speed    : 600 , //動畫速度
             trigger  : 'mouseenter', //觸發事件
             callback : null , // 回呼函數
             view     : 7
         }, args || {}  );

         this.history = [];//記錄移動的記錄
         this.index = 0;
         this.el = elem;
         this.length = this.el.find('li').length;//記錄總長度
         this.width = this.el.find('li').eq(0).outerWidth();//記錄每一個單項的寬度
         this.init();
     }
     slider.prototype = {
         constructor : slider,
         init : function(){
             this.bindEvents();
         },
         bindEvents : function(){
             this.prev();
             this.next();
         },
         prev :  function(){
             this.el.find('[data-type="left"]').click( $.proxy(function(){

                 if( !this.history.length ) return;//如果記錄為空白,證明沒有進行移動過,所以直接return

                 this.index--;
                 var step = this.history.pop();//取最後的移動步驟
                 var move =  step * this.width;//算出移動寬度
                 this.el.find("ul").animate( { "left" : "+="+move+"px" } , this.config.speed )

             } , this));
         },
         next : function(){
             this.el.find('[data-type="right"]').click( $.proxy(function(){
                 //如果是當前的最後一頁,直接return
                 if(this.index == parseInt( this.length / this.config.view , 10 ) ){
                     return;
                 }
                 this.index++;
                 //這個計算很重要
                 //計算 ( 下一頁 * view ) 展示個數是否大於總長度 : 好比當前在第一頁 (1+1) *7 > 12(總長度)
                 //則this.step 賦值為取餘 ,也就是剩下要移動的個數
                 if( this.config.view * (this.index+1) > this.length ){
                     this.step =  this.length%this.config.view;
                 }else{
                     //否則移動展示的個數
                     this.step = this.config.view;
                 }
                 //記錄移動的記錄
                 this.history.push(this.step);
                 var move = -1 * this.step * this.width;
                 this.el.find("ul").animate( { "left" : "+="+move+"px" } , this.config.speed )

             } , this));
         }
     }

     $.fn.slider = function( args ){
         return this.each(function(){
             var el = this;
             var plugins = new slider( $( el ) , args );
             $(el).data("slider" , plugins );
         });
     }
 })(jQuery)

開始對這個實現沒有好的想法,本來想利用一個變數記錄當前的移動個數的,但是後面突然想到用數組來做這樣子的處理,頓時感覺清晰了。

這個的實現重點是一個記錄移動步驟的數組。向左移動的時候往數組裡面push移動的步驟,向右移動的時候,從數組裡面取最後一項 [].pop()。

這樣子很好的實現了需求,做的比較粗糙,麻煩各位大神提點意見

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.