jQuery选项卡淡出淡出

时间:2015-12-15 16:55:47

标签: javascript jquery

我正在尝试淡入和淡出标签,这就是代码:

$(document).ready(function(){

    $('ul.tabs li').click(function(){
        var tab_id = $(this).attr('data-tab');

        $('ul.tabs li').removeClass('current');
        $('.tab-content').removeClass('current');

        $(this).addClass('current');
        $("#"+tab_id).addClass('current').fadeIn('slow').delay(5000).fadeOut('slow');

    })

})

但问题是,当我按下另一个标签时,内容会重复出现。这意味着文本在另一个选项卡和部分中重复。

HTML:

<div class="container">

    <ul class="tabs">
        <li class="tab-link current" data-tab="tab-1">Tab One</li>
        <li class="tab-link" data-tab="tab-2">Tab Two</li>
        <li class="tab-link" data-tab="tab-3">Tab Three</li>
        <li class="tab-link" data-tab="tab-4">Tab Four</li>
    </ul>

    <div id="tab-1" class="tab-content current">
    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
    </div>
    <div id="tab-2" class="tab-content">
     Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    </div>
    <div id="tab-3" class="tab-content">
    Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
    </div>
    <div id="tab-4" class="tab-content">
    Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
    </div>
</div>

1 个答案:

答案 0 :(得分:0)

我更喜欢使用id作为选择器并为每个li添加id。所以,你的代码将是:

$(document).ready(function(){

    $('#tab-link').click(function(){
        var tab_li_id = $(this).attr('id');
        
        var temp = tab_li_id.split('-');
      
        var div_active = 'tab-div-'+temp[2]; //temp[0] contaings tab, temp[1] contains li and tab[2] contains the variabale 1,2,3,4 depending upon which li was clicked.
      
      
        $('.tab-content').fadeOut(2000); //div
        $('.tab-content').removeClass('current'); //div
        $('.tab-link').removeClass('current'); //li
      
        $('#'+div_active).addClass('current'); //div
        $('#'+div_active).fadeIn(2000); //div 
       $('#'+tab_li_id).addClass('current'); //li


    })

})
    <ul class="tabs">
      
      <li class="tab-link current" data-tab="tab-1" id="tab-li-1">Tab One</li>
      <li class="tab-link" data-tab="tab-2" id="tab-li-2">Tab Two</li>
      <li class="tab-link" data-tab="tab-3" id="tab-li-3">Tab Three</li>
      <li class="tab-link" data-tab="tab-4" id="tab-li-4">Tab Four</li>

    </ul>

    <div id="tab-div-1" class="tab-content current">
      Lorem ipsum dolor sit amet....
    </div>
    
    <div id="tab-div-2" class="tab-content">
      Lorem ipsum dolor sit amet....
    </div>

    <div id="tab-div-3" class="tab-content">
      Lorem ipsum dolor sit amet....
    </div>
    
    <div id="tab-div-4" class="tab-content">
      Lorem ipsum dolor sit amet....
    </div>

P.S。确保包含jquery.js文件。