2011-08-16 114 views
1

好吧,所以我已經使用相同的代碼來循環列表項作爲計時器上的圖像滑塊,它已經很好。現在我想添加'Next'和'Prev'按鈕來增加用戶體驗。我看了看周圍的堆棧,嘗試了一些東西,似乎無法想出解決方案。jQuery滑塊 - 添加下一步和上一步按鈕

首先這裏是HTML代碼:

<div class="container"> 
    <div class="tabs"> 
    <nav> 
     <ul id="tabs"> 
     <li><a href="javascript:;" class='current'>Tab1</a></li> 
     <li><a href="javascript:;">Tab1</a></li> 
     <li><a href="javascript:;">Tab2</a></li> 
     <li><a href="javascript:;">Tab3</a></li> 
     <li><a href="javascript:;">Tab4</a></li> 
     <li><a href="javascript:;">Tab5</a></li> 
     <li><a href="javascript:;">Tab6</a></li> 

     </ul> 
    </nav> 
    <!-- end .tabs --> 
    </div> 
    <div class="content"> 
    <div id="feature_list"> 
    <div id="prev" class="arrows prev"><a href="#">PREV</a></div> 
     <ul id="output"> 
      <li><a href="#" target="_parent" class="corresp">Output1</a></li> 
      <li><a href="#" target="_parent">Output2</a></li> 
      <li><a href="#" target="_parent">Output3</a></li> 
      <li><a href="#" target="_parent">Output4</a></li> 
      <li><a href="#" target="_parent">Output5</a></li> 
      <li><a href="#" target="_parent">Output6</a></li> 
      <li><a href="#" target="_parent">Output7</a></li> 
     </ul> 
     <div id="next" class="arrows next"><a href="#">NEXT</a></div> 
    </div> 
    <!-- end .content --> 
    </div> 

現在OK了jQuery是下一個。但首先我應該解釋一下,代碼循環遍歷列表中的id爲「tabs」,從第一個標籤開始,在每個錨標籤上添加和刪除類「current」。然後它在「輸出」列表中找到相應的項目並顯示該輸出。現在這裏是代碼:

<script src="http://code.jquery.com/jquery-1.6.2.min.js"></script> 

<script type="text/javascript"> 
     (function($) { 
      $.fn.featureList = function(options) { 
       var tabs = $(this); 
       var output = $(options.output); 
       new jQuery.featureList(tabs, output, options); 
       return this;  
      }; 
      //Loops through tab to next and displays cooresponding output 
      $.featureList = function(tabs, output, options) { 
       function slide(nextli) { 
        if (typeof nextli == "undefined") { 
         nextli = visible_item + 1; 
         nextli = nextli >= total_items ? 0 : nextli; 
         prevli = visible_item - 1; 
         prevli = prevli >= total_items ? 0 : prevli; 
        } 

        tabs.removeClass('current').filter(":eq(" + nextli + ")").addClass('current'); 

        output.stop(true, true).filter(":visible").removeClass('cooresp').css({zIndex:10}).fadeOut(); 
        output.filter(":eq(" + nextli + ")").addClass('cooresp').css({zIndex:15}).fadeIn(function() { 
         visible_item = nextli;  
        }); 
       } 

       var options   = options || {}; 
       var total_items  = tabs.length; 
       var visible_item = options.start_item || 0; 

       options.pause_on_hover  = options.pause_on_hover  || true; 
       options.transition_interval = options.transition_interval || 4000; 

       output.hide().eq(visible_item).show(); 
       tabs.eq(visible_item).addClass('current'); 

       tabs.click(function() { 
        if ($(this).hasClass('current')) { 
         return false;  
        } 

        slide(tabs.index(this)); 
       }); 

       if (options.transition_interval > 0) { 
        var timer = setInterval(function() { 
         slide(); 
        }, options.transition_interval); 

        if (options.pause_on_hover) { 
         tabs.mouseenter(function() { 
          clearInterval(timer); 

         }).mouseleave(function() { 
          clearInterval(timer); 
          timer = setInterval(function() { 
           slide(); 
          }, options.transition_interval); 
         }); 
        } 
       } 
      }; 
     })(jQuery); 
    </script> 
    <script language="javascript"> 
     $(document).ready(function() { 
      $.featureList(
       $("#tabs li a"), 
       $("#output li"), { 
        start_item : 0 
       } 
      ); 
      var next = $("#feature_list #next a"); 
      var prev = $("#feature_list #prev a"); 
      var tabs = $('#tabs li a'); 
      next.click(function(){ 
       var tabli = $('#tabs li a.current'); 
       //var current = $('.selectoption li:visible'); 
       var output = $("#output li.cooresp"); 

       var currentli = $('#tabs li a.current'); 
       output.stop(true, true).css({zIndex:10}).fadeOut(); 
       output.prev().css({zIndex:15}).fadeIn(function() { 
         tabsli.prev('a').addClass('current'); 
       }); 
      }); 

      prev.click(function(){ 
       var tabli = $('#tabs li a.current'); 
       //var current = $('.selectoption li:visible'); 
       var output = $("#output li.cooresp"); 

       var currentli = $('#tabs li a.current'); 
       output.stop(true, true).css({zIndex:10}).fadeOut(); 
       output.prev().css({zIndex:15}).fadeIn(function() { 
         tabli.stop(true, true).removeClass('current'); 
         $('#tabs li a').prev().addClass('current'); 
       }); 
      });  
     }); 
    </script> 

我試圖做的是使它有這樣,當有人點擊'下一步'按鈕,顯示前一個圖像。這有效(儘管它可能是一種循環的方式)。嘗試使用.prev和.next()函數時遇到的主要問題我無法讓當前類也回退或前進。我希望兩個列表在其各自的列表中始終保持相同的編號。如果「當前」類在第一個或最後一個,我希望它像在定時器上一樣循環。

如果有人可以幫助我將不勝感激。如果您需要更多信息,請直接詢問。提前致謝!

編輯

好了,所以我試着取出next.click並把它放回主代碼區域略低於tabs.click:

   next.click(function() { 
        currentli = $('a.current'); 
        nextli = currentli.parent().siblings().next('li').find('a'); 
        console.log(currentli.parent().siblings().next('li').find('a')); 
        slide(tabs.index(nextli)); 
       }); 

這種近乎作品!唯一的問題是變量'nextli'是靜態的,它的值僅在DOM加載時才被確定。不是每次點擊下一個按鈕。如何獲得每次點擊評估的'nextli'?

回答

0

好男人想通了,我自己的答案。 JSLint並不完美,但它幾乎完全符合我的要求。 這裏是我發現那些你感興趣的答案:

(function($) { 
    $.fn.featureList = function(options) { 
     var tabs = $(this); 
     var output = $(options.output); 

     new jQuery.featureList(tabs, output, options); 
     return this; 
    }; 
    //Loops through tab to next and displays cooresponding output 
    $.featureList = function(tabs, output, options) { 
     function slide(nextli) { 
      if (typeof nextli == "undefined") { 
       nextli = visible_item + 1; 
       nextli = nextli >= total_items ? 0 : nextli; 
      } 
      var futureli = nextli + 1; 
      futureli = futureli >= total_items ? 0 : futureli; 
      var pastli = nextli - 1; 
      pastli = pastli >= total_items ? 0 : pastli; 

      tabs.removeClass('current').filter(":eq(" + nextli + ")").addClass('current'); 
      tabs.removeClass('previous').filter(":eq(" + pastli + ")").addClass('previous'); 
      tabs.removeClass('nextup').filter(":eq(" + futureli + ")").addClass('nextup'); 

      output.stop(true, true).filter(":visible").removeClass('cooresp').css({ 
       zIndex: 10 
      }).fadeOut(); 
      output.filter(":eq(" + nextli + ")").addClass('cooresp').css({ 
       zIndex: 15 
      }).fadeIn(function() { 
       visible_item = nextli; 
       next_item = futureli; 
       prev_item = pastli; 
      }); 
     } 

     function prevslide(nextli) { 
      if (typeof nextli == "undefined") { 
       nextli = visible_item - 1; 
       nextli = nextli >= total_items ? (tabs.length -1) : nextli; 
      } 
      if ((nextli < 0)) { 
       nextli = (tabs.length -1); 
      } 
      var futureli = nextli + 1; 
      futureli = futureli >= total_items ? (tabs.length -1) : futureli; 
      var pastli = nextli - 1; 
      pastli = pastli >= total_items ? (tabs.length -1) : pastli; 


      tabs.removeClass('current').filter(":eq(" + nextli + ")").addClass('current'); 
      tabs.removeClass('previous').filter(":eq(" + pastli + ")").addClass('previous'); 
      tabs.removeClass('nextup').filter(":eq(" + futureli + ")").addClass('nextup'); 

      output.stop(true, true).filter(":visible").removeClass('cooresp').css({ 
       zIndex: 10 
      }).fadeOut(); 
      output.filter(":eq(" + nextli + ")").addClass('cooresp').css({ 
       zIndex: 15 
      }).fadeIn(function() { 
       visible_item = nextli; 
       next_item = futureli; 
       prev_item = pastli; 
      }); 
     } 

     var options = options || {}; 
     var total_items = tabs.length; 
     var visible_item = options.start_item || 0; 
     var next_item = (visible_item + 1) || 1; 
     var prev_item = (visible_item - 1) || (tabs.length -1); 
     var next = $('#feature_list #next a'); 
     var prev = $('#feature_list #prev a'); 
     var mainIMG = $("#output li"); 

     options.pause_on_hover = options.pause_on_hover || true; 
     options.transition_interval = options.transition_interval || 6000; 
     prev.pause_on_hover = prev.pause_on_hover || true; 
     next.pause_on_hover = next.pause_on_hover || true; 
     mainIMG.pause_on_hover = mainIMG.pause_on_hover || true; 

     output.hide().eq(visible_item).show(); 
     tabs.eq(visible_item).addClass('current'); 

     tabs.click(function() { 
      if ($(this).hasClass('current')) { 
       return false; 
      } 
      slide(tabs.index(this)); 
     }); 
     findCurrentli = function(currentTag) { 
      var currentli = currentTag; 
      var nextli = currentli.parent().siblings().next('li').find('a'); 
      console.log(currentli.parent().siblings().next('li').find('a')); 
      slide(tabs.index(nextli)); 
     }; 
     prev.click(function(e) { 
      e.preventDefault(); 
      var prevli = $('#tabs li a.previous'); 
      var currentli = $('#tabs li a.current'); 
      var nextli = $('#tabs li a.nextup'); 
      console.log(prevli); 
      currentli.addClass('nextup'); 
      currentli.parent().siblings().prev('li a').addClass('previous'); 
      prevslide(tabs.index(prevli)); 
     }); 
     next.click(function(e) { 
      e.preventDefault(); 
      var prevli = $('#tabs li a.previous'); 
      var currentli = $('#tabs li a.current'); 
      var nextli = $('#tabs li a.nextup'); 
      currentli.addClass('previous'); 
      nextli.parent().siblings().next('li a').addClass('nextup'); 
      slide(tabs.index(nextli)); 
     }); 


     if (options.transition_interval > 0) { 
      var timer = setInterval(function() { 
       slide(); 
      }, options.transition_interval); 

      if (options.pause_on_hover || prev.pause_on_hover || next.pause_on_hover || mainIMG.pause_on_hover) { 
       tabs.mouseenter(function() { 
        clearInterval(timer); 

       }).mouseleave(function() { 
        clearInterval(timer); 
        timer = setInterval(function() { 
         slide(); 
        }, options.transition_interval); 
       }); 
      } 
     } 
    }; 
})(jQuery); 

基本上「下一步」按鈕上稍微改變幻燈片()函數捎帶。對於'Prev'按鈕,我製作了slide()的副本,並將其命名爲prevslide()。新函數所做的不是找到下一個列表項,而是找到前一個列表項,並將「當前」類添加到錨標記。我還必須添加'previous'和'nextup'類,因爲在Firefox中,單擊其中一個選項卡之後,'current'類的CSS在計時器經過之後纔會消失。如果你能告訴我如何讓pause_on_hover在'Next'&'PREV'按鈕上工作,請評論。

相關問題