2016-05-17 115 views
11

當屏幕尺寸變小時,引導選項卡像圖像一樣重疊。 enter image description here當屏幕尺寸變小時,引導選項卡重疊

我用col-sm和我的角HTML是這樣的(但任何非角HTML溶液是細太):

<div class="col-sm-6"> <tabset> <tab> 
    <tab-heading active="true"> 
     Tab1 
     <toggle-switch ></toggle-switch> 
    </tab-heading> 
... 
</tab> 
<tab> 
    <tab-heading>Tab2 
     <toggle-switch ></toggle-switch></tab-heading> 
    ... 
</tab> 
<tab> 
    <tab-heading>Tab3 
     <toggle-switch ></toggle-switch></tab-heading> 
    ... 
</tab></tabset></div> 

我想對於其他標籤中的背景的,而不是堆疊前景。

desired screenshot

請原諒我糟糕瘸子技能,但激活的標籤應該總是衝到前面,因此,如果接頭3選擇,那麼其他行應該轉移到後面。

desired active

它不必是這樣的特意安排,但問題是,當前標籤不應該有它和它的內容窗格之間的任何東西,所以這樣的事情也未嘗不可:

enter image description here

+0

好了,所以......他們包...你想讓他們做的,而不是什麼呢? –

+0

嗨@KevinB我添加了一個可能的解決方案截圖 –

+0

toggle-switch ......你在使用bootstrap-switch.js嗎? – 2016-07-12 19:58:59

回答

1

嘗試這種解決方案: https://jsfiddle.net/DTcHh/22583/

CSS

.nav-tabs { border-bottom: 2px solid #DDD; } 
.nav-tabs > li.active > a, .nav-tabs > li.active > a:focus, .nav-tabs > li.active > a:hover { border-width: 0; } 
.nav-tabs > li > a { border: none; color: #666; } 
.nav-tabs > li.active > a, .nav-tabs > li > a:hover { border: none; color: #4285F4 !important; background: transparent; } 
.nav-tabs > li > a::after { content: ""; background: #4285F4; height: 2px; position: absolute; width: 100%; left: 0px; bottom: -1px; transition: all 250ms ease 0s; transform: scale(0); } 
.nav-tabs > li.active > a::after, .nav-tabs > li:hover > a::after { transform: scale(1); } 
.tab-nav > li > a::after { background: #21527d none repeat scroll 0% 0%; color: #fff; } 
.tab-pane { padding: 15px 0; } 
.tab-content{padding:20px} 

UPDATE: 我做了一些改進,增加了一些jQuery的。現在看小提琴。

var active = $('.nav-tabs > li.active').html(), 
    last = $('.nav-tabs > li:last-child').html(); 
    $('.nav-tabs > li.active').removeClass('active').html(last); 
    $('.nav-tabs > li:last-child').addClass('active').html(active); 
    last = active; 
    $('.nav-tabs li').on('click', function(){ 
     active = $(this).html(); 
     last = $('.nav-tabs > li:last-child').html(); 
     $(this).removeClass('active').html(last); 
     $('.nav-tabs > li:last-child').addClass('active').html(active);  
     last = active; 
    }); 
+0

這是一個很好的解決方案,但是我認爲當選項卡進行到最後時,最終用戶會覺得有點困惑。有沒有一種方法可以使這個解決方案在開始時顯示出來呢?我意識到你可能想發佈另一個答案,因爲它可能會有很大的不同,因爲這個答案取決於現有的堆棧 - 而不是將非活動標籤移到後面。 –

4

有一個解決方案結合了Css和Javascript。

但是,您需要知道標籤的高度和寬度!

因爲每個標籤在圖片中都有相同的寬度和高度,所以這似乎不成問題。

// Tab width 
 
var tabWidth = 140; 
 
// Tab height 
 
var tabHeight = 42; 
 

 
var updateAllTabs = function(){ 
 
\t $('li.active a[data-toggle="tab"]').each(function(){ 
 
\t \t updateTabs({target : this}); 
 
\t }); 
 
}; 
 

 
var updateTabs = function(e){ 
 
\t // Get elements 
 
\t var activeItem = e.target.parentNode; 
 
\t var menu = activeItem.parentNode; 
 
\t var items = menu.getElementsByTagName("li"); 
 
\t 
 
\t // Reset menu 
 
\t menu.style.paddingLeft = 0; 
 
\t menu.style.paddingBottom = 0; 
 
\t 
 
\t var menuWidth = jQuery(menu).width(); 
 
\t 
 
\t var otherItems = []; 
 
\t 
 
\t // Clear old changes - make new list 
 
\t for(var i=0; i < items.length; i++){ 
 
\t \t items[i].style.marginLeft = "0px"; 
 
\t \t items[i].style.marginTop = "0px"; 
 
\t \t 
 
\t \t if(items[i] != activeItem) 
 
\t \t \t otherItems.push(items[i]); 
 
\t } 
 
\t 
 
\t // If one line return (or only one item) 
 
\t if(menuWidth >= items.length * tabWidth || items.length <= 1) 
 
\t \t return; 
 
\t 
 
\t // Make some calculations 
 
\t var perLine = Math.floor(menuWidth/tabWidth); 
 
\t var lines = Math.ceil(items.length/perLine); 
 
\t 
 
\t // 1 tab per line 
 
\t if(perLine == 1){ 
 
\t \t menu.style.paddingBottom = jQuery(activeItem).height() + "px"; 
 
\t \t return; 
 
\t } else if(perLine + 1 == items.length){ 
 
\t \t menu.style.paddingBottom = jQuery(activeItem).height() + "px"; 
 
\t } 
 
\t 
 
\t var pad = jQuery(activeItem).width(); 
 
\t 
 
\t // More than one per tab 
 
\t menu.style.paddingLeft = pad + "px"; 
 
\t 
 
\t // For every line exept the last 
 
\t for(var i=0; i < lines-1; i++){ 
 
\t \t // Move first of the line 
 
\t \t otherItems[i*perLine].style.marginLeft = ((i+1)*pad*(-1)) + "px"; 
 
\t \t otherItems[i*perLine].style.marginTop = (i*tabHeight) + "px"; 
 
\t } 
 
\t 
 
\t return; 
 
}; 
 

 
$(window).resize(updateAllTabs); 
 
$('a[data-toggle="tab"]').on('shown.bs.tab', updateTabs); 
 
updateAllTabs();
.nav-tabs > li{ 
 
    width: 140px; 
 
    height: 42px; 
 
} 
 

 
@media only screen and (max-width: 840px) { 
 
    .nav-tabs{ 
 
    position: relative; 
 
    } 
 
    .nav-tabs > li.active{ 
 
    position: absolute; 
 
    bottom: -1px; 
 
    left: 0px; 
 
    } 
 
}
<link rel="stylesheet prefetch" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> 
 

 
<div> 
 

 
    <!-- Nav tabs --> 
 
    <ul class="nav nav-tabs" role="tablist"> 
 
    <li role="presentation" class="active"><a href="#tab1" aria-controls="home" role="tab" data-toggle="tab">Tab 1</a></li> 
 
    <li role="presentation"><a href="#tab2" aria-controls="profile" role="tab" data-toggle="tab">Tab 2</a></li> 
 
    <li role="presentation"><a href="#tab3" aria-controls="messages" role="tab" data-toggle="tab">Tab 3</a></li> 
 
    <li role="presentation"><a href="#tab4" aria-controls="settings" role="tab" data-toggle="tab">Tab 4</a></li> 
 
    <li role="presentation"><a href="#tab5" aria-controls="settings" role="tab" data-toggle="tab">Tab 5</a></li> 
 
    <li role="presentation"><a href="#tab6" aria-controls="settings" role="tab" data-toggle="tab">Tab 6</a></li> 
 
    </ul> 
 

 
    <!-- Tab panes --> 
 
    <div class="tab-content"> 
 
    <div role="tabpanel" class="tab-pane active" id="tab1">This is the tab 1</div> 
 
    <div role="tabpanel" class="tab-pane" id="tab2">This is the tab 2</div> 
 
    <div role="tabpanel" class="tab-pane" id="tab3">This is the tab 3</div> 
 
    <div role="tabpanel" class="tab-pane" id="tab4">This is the tab 4</div> 
 
    <div role="tabpanel" class="tab-pane" id="tab5">This is the tab 5</div> 
 
    <div role="tabpanel" class="tab-pane" id="tab6">This is the tab 6</div> 
 
    </div> 
 

 
</div> 
 

 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

那麼,如何工作的呢?

的基本思想是,每一個新的標籤被激活時, 我們設置活動選項卡作爲position: absolute; left: 0px; bottom: -1px; 然後再加上的標籤菜單填充等於翼片的寬度。 最後,我們在每行的第一個選項卡上添加一個邊距減去選項卡的寬度。

一些更多的信息:

代碼工作,甚至有超過2行選項卡。

可以通過在otherItems陣列上添加假項目並在previus項目上添加margin-right來輕鬆轉換它以匹配最後一張圖像的佈局。 (明白了嗎?或者我必須爲你編碼? :P)

+0

這可能是正確的路徑,但是在保持標籤與其內容同步時存在一個錯誤 - 點擊標籤6顯示標籤5的內容,點擊4顯示3等。 –

+0

問題已修復。錯誤的HTML屬性:) – GramThanos

0

我對你實際想要達到的目標有些困惑。但是我做了這個解決方案來隱藏非活動選項卡的一部分(並使它們部分重疊/堆疊)。我使用了默認的引導程序示例。解釋在代碼中。使用這種技術,您基本上可以縮小標籤,使其適合較小的屏幕。請注意,您可以將所有想要消失的內容都包裹在一個範圍內。

這仍然不會做你想要的極限量的標籤。在這種情況下,您將不得不使用一些JavaScript來使某些選項卡消失/重新出現在屏幕的左側/右側。

Example

HTML

<ul class="nav nav-tabs"> 
    <li role="presentation" class="active"><a href="#">Home <span>I m whatever was missing</span></a></li> 
    <li role="presentation"><a href="#">P<span>rofile</span></a></li> 
    <li role="presentation"><a href="#">M<span>essages</span></a></li> 
</ul> 
<div id="stuff"></div> 

CSS

/*hide all the items after the part you want to show*/ 
.nav-tabs li span{ 
    display: none; 
} 
/*show the items on the parts with the bootstrap .active class*/ 
li.active span{ 
    display: inline-block; 
} 
#stuff{ 
    background-color: yellow; 
    height: 200px; 
} 
.active a{ 
    background-color: yellow !important; 
} 
0

不要使用float屬性。我認爲沒有使用浮動屬性的解決方案。您可以通過使用
style =「Position:Fixed;」來實現此目的。
我寫了一些東西。希望它能幫助你。

CSS



    ul { 
     list-style-type: none; 
     margin: 0; 
     padding: 0; 
     background-color: #333; 
     width:800px; 
     height:45px; width:100%; 
    } 
    li a { 
     position:fixed; 
     color: white; 
     text-align: center; 
     padding: 14px 16px; 
     text-decoration: none; 

    } 

    li a:hover:not(.active) { 
     background-color: #111; 
    } 

    .active { 
     background-color: #4CAF50; 
    } 


HTML

<html> 
    <body> 
    <ul > 
     <li><a class="active" href="#home">Tab1</a></li> 
     <li><a href="#n2" style="left:80px;" >Tab2</a></li> 
     <li><a href="#n3" style="left:160px;">Tab3</a></li> 
     <li><a href="#n4" style="left:240px;">Tab4</a></li> 
     <li><a href="#n5" style="left:320px;">Tab5</a></li> 
     <li><a href="#n6" style="left:400px;">Tab6</a></li> 
     <li><a href="#n7" style="left:480px;">Tab7</a></li> 
     <li><a href="#n8" style="left:560px;">Tab8</a></li> 
     <li><a href="#n9" style="left:640px;">Tab9</a></li> 
     <li><a href="#n10" style="left:720px;" >Tab10</a></li> 
    </ul> 
    </body></html> 
-1

一長串標籤將不裝配到固定的小屏幕,以便唯一方法是顯示在第二行中的選項卡與是默認行爲。

另一個將在下拉菜單中顯示額外的選項卡。類似here

下面是在bootply中創建的一個很好的示例。 click for code

+0

請你能澄清爲什麼這個解決方案不是一個好的解決方案 – sawbeanraz

+0

你推定*「唯一的辦法就是在第二行顯示標籤」*可能是真的,但問題是如何顯示第二行在後臺而不是前臺。 –

+0

謝謝@Jeff的解釋 – sawbeanraz

1

我也遇到了同樣的問題,我爲此解決了一個問題。我認爲這會對你有幫助。

注意:在處理引導標籤中,將不會對移動設備中的敏感標籤,所以它具有如下所述將與可摺疊網發起...

這是以前我經歷過。

這是我的代碼鏈接。

codepen.io/nehemc/pen/RRQKZB 

這裏是代碼,你需要在javascript中添加。

<script> 
    var fakewaffle = (function($, fakewaffle) { 
      'use strict'; 

      fakewaffle.responsiveTabs = function(collapseDisplayed) { 

      fakewaffle.currentPosition = 'tabs'; 

      var tabGroups = $('.nav-tabs.responsive'); 
      var hidden = ''; 
      var visible = ''; 
      var activeTab = ''; 

      if (collapseDisplayed === undefined) { 
       collapseDisplayed = ['xs', 'sm']; 
      } 

      $.each(collapseDisplayed, function() { 
       hidden += ' hidden-' + this; 
       visible += ' visible-' + this; 
      }); 

      $.each(tabGroups, function(index) { 
       var collapseDiv; 
       var $tabGroup = $(this); 
       var tabs = $tabGroup.find('li a'); 

       if ($tabGroup.attr('id') === undefined) { 
       $tabGroup.attr('id', 'tabs-' + index); 
       } 

       collapseDiv = $('<div></div>', { 
       'class': 'panel-group responsive' + visible, 
       'id': 'collapse-' + $tabGroup.attr('id') 
       }); 

       $.each(tabs, function() { 
       var $this = $(this); 
       var oldLinkClass = $this.attr('class') === undefined ? '' : $this.attr('class'); 
       var newLinkClass = 'accordion-toggle'; 
       var oldParentClass = $this.parent().attr('class') === undefined ? '' : $this.parent().attr('class'); 
       var newParentClass = 'panel panel-default'; 
       var newHash = $this.get(0).hash.replace('#', 'collapse-'); 

       if (oldLinkClass.length > 0) { 
        newLinkClass += ' ' + oldLinkClass; 
       } 

       if (oldParentClass.length > 0) { 
        oldParentClass = oldParentClass.replace(/\bactive\b/g, ''); 
        newParentClass += ' ' + oldParentClass; 
        newParentClass = newParentClass.replace(/\s{2,}/g, ' '); 
        newParentClass = newParentClass.replace(/^\s+|\s+$/g, ''); 
       } 

       if ($this.parent().hasClass('active')) { 
        activeTab = '#' + newHash; 
       } 

       collapseDiv.append(
        $('<div>').attr('class', newParentClass).html(
        $('<div>').attr('class', 'panel-heading').html(
         $('<h4>').attr('class', 'panel-title').html(
         $('<a>', { 
          'class': newLinkClass, 
          'data-toggle': 'collapse', 
          'data-parent': '#collapse-' + $tabGroup.attr('id'), 
          'href': '#' + newHash, 
          'html': $this.html() 
         }) 
        ) 
        ) 
       ).append(
        $('<div>', { 
         'id': newHash, 
         'class': 'panel-collapse collapse' 
        }) 
       ) 
       ); 
       }); 

       $tabGroup.next().after(collapseDiv); 
       $tabGroup.addClass(hidden); 
       $('.tab-content.responsive').addClass(hidden); 

       if (activeTab) { 
       $(activeTab).collapse('show'); 
       } 
      }); 

      fakewaffle.checkResize(); 
      fakewaffle.bindTabToCollapse(); 
      }; 

      fakewaffle.checkResize = function() { 

      if ($('.panel-group.responsive').is(':visible') === true && fakewaffle.currentPosition === 'tabs') { 
       fakewaffle.tabToPanel(); 
       fakewaffle.currentPosition = 'panel'; 
      } else if ($('.panel-group.responsive').is(':visible') === false && fakewaffle.currentPosition === 'panel') { 
       fakewaffle.panelToTab(); 
       fakewaffle.currentPosition = 'tabs'; 
      } 

      }; 

      fakewaffle.tabToPanel = function() { 

      var tabGroups = $('.nav-tabs.responsive'); 

      $.each(tabGroups, function(index, tabGroup) { 

       // Find the tab 
       var tabContents = $(tabGroup).next('.tab-content').find('.tab-pane'); 

       $.each(tabContents, function(index, tabContent) { 
       // Find the id to move the element to 
       var destinationId = $(tabContent).attr('id').replace(/^/, '#collapse-'); 

       // Convert tab to panel and move to destination 
       $(tabContent) 
        .removeClass('tab-pane') 
        .addClass('panel-body fw-previous-tab-pane') 
        .appendTo($(destinationId)); 

       }); 

      }); 

      }; 

      fakewaffle.panelToTab = function() { 

      var panelGroups = $('.panel-group.responsive'); 

      $.each(panelGroups, function(index, panelGroup) { 

       var destinationId = $(panelGroup).attr('id').replace('collapse-', '#'); 
       var destination = $(destinationId).next('.tab-content')[0]; 

       // Find the panel contents 
       var panelContents = $(panelGroup).find('.panel-body.fw-previous-tab-pane'); 

       // Convert to tab and move to destination 
       panelContents 
       .removeClass('panel-body fw-previous-tab-pane') 
       .addClass('tab-pane') 
       .appendTo($(destination)); 

      }); 

      }; 

      fakewaffle.bindTabToCollapse = function() { 

      var tabs = $('.nav-tabs.responsive').find('li a'); 
      var collapse = $('.panel-group.responsive').find('.panel-collapse'); 

      // Toggle the panels when the associated tab is toggled 
      tabs.on('shown.bs.tab', function(e) { 

       if (fakewaffle.currentPosition === 'tabs') { 
       var $current = $(e.currentTarget.hash.replace(/#/, '#collapse-')); 
       $current.collapse('show'); 

       if (e.relatedTarget) { 
        var $previous = $(e.relatedTarget.hash.replace(/#/, '#collapse-')); 
        $previous.collapse('hide'); 
       } 
       } 

      }); 

      // Toggle the tab when the associated panel is toggled 
      collapse.on('shown.bs.collapse', function(e) { 

       if (fakewaffle.currentPosition === 'panel') { 
       // Activate current tabs 
       var current = $(e.target).context.id.replace(/collapse-/g, '#'); 
       $('a[href="' + current + '"]').tab('show'); 

       // Update the content with active 
       var panelGroup = $(e.currentTarget).closest('.panel-group.responsive'); 
       $(panelGroup).find('.panel-body').removeClass('active'); 
       $(e.currentTarget).find('.panel-body').addClass('active'); 
       } 

      }); 
      }; 

      $(window).resize(function() { 
      fakewaffle.checkResize(); 
      }); 

      return fakewaffle; 
     }(window.jQuery, fakewaffle || {})); 

     (function($) { 
      fakewaffle.responsiveTabs(['xs', 'sm']); 
     })(jQuery); 
     $(document).ready(function() { 
      $('.tabcontainer .accordion-toggle').click(function() { 
      var divTarget = $(this).attr('href'), 
       divTargetElt = $(this); 

      setTimeout(function() { 
       console.log(divTarget); 
       $('html,body').animate({ 
        scrollTop: divTargetElt.offset().top 
       }, 
       'slow'); 
      }, 500); 
      }); 
     }); 
    </script> 
+1

請檢查它。 http://codepen.io/nehemc/pen/RRQKZB – Nehemiah

0

計算行索引add添加到li。在初始化或活動選項卡更改時,它會對li位置進行排序,直到它位於底行。

希望你正在努力達到的目標。

演示:http://codepen.io/anon/pen/GqQdyb

HTML

<div class="col-md-12"> 

     <ul id="tabs" class="nav nav-tabs" role="tablist"> 
     <li role="presentation" class="active"><a href="#home" aria-controls="home" role="tab" data-toggle="tab">Tab 1</a></li> 
     <li role="presentation"><a href="#profile" aria-controls="profile" role="tab" data-toggle="tab">Tab 2</a></li> 
     <li role="presentation"><a href="#messages" aria-controls="messages" role="tab" data-toggle="tab">Tab 3</a></li> 
     <li role="presentation"><a href="#settings" aria-controls="settings" role="tab" data-toggle="tab">Tab 4</a></li> 
     <li role="presentation"><a href="#settings" aria-controls="settings" role="tab" data-toggle="tab">Tab 5</a></li> 
     <li role="presentation"><a href="#settings" aria-controls="settings" role="tab" data-toggle="tab">Tab 6</a></li> 
     <li role="presentation"><a href="#settings" aria-controls="settings" role="tab" data-toggle="tab">Tab 7</a></li> 
     <li role="presentation"><a href="#settings" aria-controls="settings" role="tab" data-toggle="tab">Tab 8</a></li> 
     <li role="presentation"><a href="#settings" aria-controls="settings" role="tab" data-toggle="tab">Tab 9</a></li> 
     <li role="presentation"><a href="#settings" aria-controls="settings" role="tab" data-toggle="tab">Tab 10</a></li> 
     <li role="presentation"><a href="#settings" aria-controls="settings" role="tab" data-toggle="tab">Profile</a></li> 
     <li role="presentation"><a href="#settings" aria-controls="settings" role="tab" data-toggle="tab">Message</a></li> 
     <li role="presentation"><a href="#settings" aria-controls="settings" role="tab" data-toggle="tab">Very long tab name</a></li> 
     <li role="presentation"><a href="#settings" aria-controls="settings" role="tab" data-toggle="tab">Programming</a></li> 
     </ul> 

     <!-- Tab panes --> 
     <div class="tab-content"> 
     <div role="tabpanel" class="tab-pane active" id="home">...</div> 
     <div role="tabpanel" class="tab-pane" id="profile">...</div> 
     <div role="tabpanel" class="tab-pane" id="messages">...</div> 
     <div role="tabpanel" class="tab-pane" id="settings">...</div> 
     </div>  
    </div>  
    </div> 
</div> 

JS

$(function() { 
    var _max = 0; 

    var _calc = function() { 
     var _w = $('#tabs').width(); 
     var _h = $('#tabs').height(); 
     var _a = _w; 

     var _index = 0; 
     $('#tabs li').each(function(i, v) {    
     var _t = $(v).outerWidth();  
     if(_a < _t) { 
      _a = _w; 
      _index++; 
     } 
     $(v).data('rowindex', _index);  
     _a -= _t;  
     }); 

     _max = _index; 
     console.log(_max); 
    }; 



    var _sortTab = function() {  
     var _active = $('li.active').data('rowindex');  
     var _tabs = $('#tabs'); 
     _tabs.find('li').sort(function(a, b) {   
     var _a = $(a).data('rowindex'); 
     var _b = $(b).data('rowindex');   
     return (_a == _active && _a !== _b); 
     }).appendTo(_tabs);  
     _calc(); 

     if(_active !== _max) { 
     _sortTab(); 
     }  
    }; 


    $('#tabs li').on('shown.bs.tab', function (e) { 
    _sortTab(); 
    }); 
    _calc(); 
    _sortTab(); 
});