2016-03-02 82 views
24

我有一個簡單的水平菜單更像標籤..我希望它像BBC應用程序標籤菜單一樣工作,所以當菜單有更多的項目時,它將允許在兩個方向上水平滾動。基於水平標籤的滾動標籤菜單

同我的代碼是在這裏http://codepen.io/anon/pen/GZRaee

enter image description here

我試過的東西不多,但似乎沒有任何工作就像我在固定寬度的div包裹的菜單,並試圖使其滾動能夠但這沒有工作,因爲它總是添加滾動條。我試圖讓它旋轉木馬也不適合我。

是否有任何類似的基於HTML的網站插件。 BBC應用程序使用的導航欄在應用程序中非常常見,我希望我可以爲基於HTML的移動版網頁提供類似的功能。

<div class="tab-nav-wrapper"> 
    <ul> 
    <li class="one"><a href="#">MenuONE</a></li> 
    <li class="two"><a href="#">MTWO</a></li> 
    <li class="three"><a href="#">THREE</a></li> 
    <li class="four"><a href="#">FOUR</a></li> 
    <li class="five"><a href="#">MenuFIVE</a></li> 
    <hr /> 
    </ul> 
</div> 
<div class="tab-content-wrapper"> 
    <div class="tab1-c"> 
    <p>This is ONE.</p> 
    </div> 
    <div class="tab2-c"> 
    <p>This is TWO</p> 
    </div> 
    <div class="tab3-c"> 
    <p>This is THREE</p> 
    </div> 
    <div class="tab4-c"> 
    <p>This is FOUR</p> 
    </div> 

    <div> 
+1

這是你在找什麼? http://codepen.io/anon/pen/EKjjQg,我只是添加了白色空間:nowrap; overflow-x:auto;到ul風格 – Dax

+0

這樣的事情,但沒有滾動條出現。 – Learning

+0

您是否在尋找類似的東西 - > http://jsfiddle.net/kdRJ7/64/ 只需用鼠標拖動menuitem或使用鍵盤的左或右箭頭 – RRR

回答

18

你可以用Owl Carousel 2做到這一點。正如你所看到的,你可以用鼠標拖動來水平滾動標籤,並且沒有水平滾動條。此外,我使用responsive選項來調整不同寬度的顯示標籤數量,但您可以修改它。這裏是a Fiddleanother Fiddle with arrows.

//OWL Carousel 
 
$('.tabs').owlCarousel({ 
 
    loop: true, 
 
    nav: false, 
 
    dots: false, 
 
    responsive: { 
 
     0: {items: 2}, 
 
     250: {items: 3}, 
 
     400: {items: 4}, 
 
     500: {items: 5} 
 
    } 
 
}); 
 

 
//Tabs 
 
$('.tabs li a').click(function() { 
 
    var activeLink = $(this).data('target'); 
 
    var targetTab = $('.tab.'+activeLink); 
 
    
 
    targetTab.siblings().removeClass('active'); 
 
    targetTab.addClass('active'); 
 
});
body { 
 
    background: white; 
 
} 
 

 
a { 
 
    color: white; 
 
    text-decoration: none; 
 
    font-size: 12px; 
 
    text-transform: uppercase; 
 
} 
 

 
ul { 
 
    list-style-type: none; 
 
    max-width: 500px; 
 
    margin: 2px auto; 
 
    background: #353434; 
 
    padding: 0; 
 
} 
 

 
.tab-content { 
 
    max-width: 500px; 
 
    border: 1px solid black; 
 
    margin: 0 auto; 
 
} 
 

 
.owl-controls { 
 
    display: none; 
 
} 
 

 
li { 
 
    display: inline-block; 
 
    padding: 10px 20px; 
 
    white-space: nowrap; 
 
    transition: all 0.3s ease-in; 
 
    border-bottom: 4px solid transparent; 
 
} 
 

 
li:hover { 
 
    border-bottom: 4px solid white; 
 
    opacity: 0.7; 
 
    cursor: pointer; 
 
} 
 

 
.tab-content > div { 
 
    display: none; 
 
} 
 

 
.tab-content > div.active { 
 
    display: block; 
 
} 
 

 
.info { 
 
    text-align: center;
<link href="http://owlcarousel.owlgraphic.com/assets/owlcarousel/assets/owl.carousel.min.css" rel="stylesheet"/> 
 
<link href="http://owlcarousel.owlgraphic.com/assets/owlcarousel/assets/owl.theme.default.min.css" rel="stylesheet"/> 
 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> 
 
<script src="http://owlcarousel.owlgraphic.com/assets/owlcarousel/owl.carousel.js"></script> 
 

 
<p class="info">Grab and drag tabs for scroll</p> 
 

 
<ul class="tabs"> 
 
    <li class="item"><a data-target="tab-one">Tab One</a></li> 
 
    <li class="item"><a data-target="tab-two">Tab Two</a></li> 
 
    <li class="item"><a data-target="tab-three">Tab Three</a></li> 
 
    <li class="item"><a data-target="tab-four">Tab Four</a></li> 
 
    <li class="item"><a data-target="tab-five">Tab Five</a></li> 
 
    <li class="item"><a data-target="tab-six">Tab Six</a></li> 
 
    <li class="item"><a data-target="tab-seven">Tab Seven</a></li> 
 
    <li class="item"><a data-target="tab-eight">Tab Eight</a></li> 
 
</ul> 
 

 
<div class="tab-content"> 
 
    <div class="tab tab-one active">One <br> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Omnis, iusto!</div> 
 
    <div class="tab tab-two">Two <br> Lorem ipsum dolor sit amet, consectetur</div> 
 
    <div class="tab tab-three">Three</div> 
 
    <div class="tab tab-four">Four</div> 
 
    <div class="tab tab-five">Five</div> 
 
    <div class="tab tab-six">Six</div> 
 
    <div class="tab tab-seven">Seven</div> 
 
    <div class="tab tab-eight">Eight</div> 
 
</div>

+0

我試過貓頭鷹Carousal也可能會在一段時間後搞砸了,你的解決方案很乾淨。我會微調它爲不同的設備。謝謝。 – Learning

8

我改變了你用下面的代碼,通過迫使包裝高度和隱藏其溢出基本上隱藏滾動條codepen。

.tab-nav-wrapper{ 
    /* forced the wrapper height and set overflow to hidden to hide the ul scrollbar */ 
    height: 47px; 
    overflow: hidden; 
} 

.tab-nav-wrapper > ul{ 
    /* padding: 0 !important; */ 
    overflow-x: auto; 
    white-space: nowrap; 
    /* this padding will place the scrollbar inside the hidden overflow */ 
    padding: 0 0 20px; 
} 

如果你不介意強制菜單高度,這應該做到這一點。

http://codepen.io/anon/pen/wGaKrB

+0

我現在不能滾動。我在選項卡中添加了另一個元素,它不允許向右滾動以查看它。 – Learning

+0

奇怪,我測試了我的codepen在firefox,chrome和chrome mobile上的效果,我甚至更新了它,使用7個標籤沒有問題。你是否用我的codepen或本地代碼來解決這個問題? – Dax

+0

我在FF上打開了相同的鏈接,我無法滾動。讓我試試另一個瀏覽器。 – Learning

3

要在元素內容滾動的,首先我們需要添加overflow: scrolloverflow: auto的元素。 (見差異here。)

.tab-nav-wrapper { 
    width: 360px; 
    max-width: 100%; 
    margin: 0 auto; 
    overflow: scroll; /* add */ 
} 

然後,我們需要讓爲它想要的內容佔據儘可能多的空間。刪除width: 100%以允許。此外,請添加white-space: nowrap以防止內容分裂成多行。

.tab-nav-wrapper > ul { 
    padding: 0 !important; 
    margin: 0 !important; 
    /* width: 100%; */ /* remove */ 
    white-space: nowrap; /* add */ 
    display: inline-block; 
    z-index: 100; 
    background-color: #ccc; 
} 

最後,如果你不想滾動條顯示(在.tab-nav-wrapper元素上),你可以將其隱藏這樣。

.tab-nav-wrapper::-webkit-scrollbar { 
    display: none; 
} 

最後,將背景從tab-nav-wrapper > ultab-nav-wrapper。這是因爲ul沒有覆蓋它的所有內容,但是包裝器卻是。

.tab-nav-wrapper > ul { 
    padding: 0 !important; 
    margin: 0 !important; 
    white-space: nowrap; 
    z-index: 100; 
    /* background-color: #ccc; */ /* move */ 
} 

.tab-nav-wrapper { 
    width: 360px; 
    max-width: 100%; 
    margin: 0 auto; 
    overflow: scroll; 
    background-color: #ccc; /* move to here */ 
} 

小提琴:http://codepen.io/anon/pen/VaeLje

NB:有與此codepen例如滾動的問題。鼠標滾輪不起作用,但如果您在設備上查看鼠標滾輪,或者在瀏覽器中以開發模式+設備模式進行拖動,則可以通過拖動進行滾動。

3

一個簡單的辦法:

.tab-nav-wrapper一個固定的高度應該是鋰項目的高度(導航項)和隱藏要素溢出(在這裏我們想隱藏滾動條):

.tab-nav-wrapper { 
    overflow: hidden; // magic is here 
    height: 48px; // magic is here 
} 

然後使ul滾動(僅x方向),並防止li元素從破到一個新的行:

.tab-nav-wrapper > ul { 
    padding: 0 !important; 
    margin: 0 !important; 
    width: 100%; 
    z-index: 100; 
    background-color: #ccc; 
    white-space: nowrap; // magic is here 
    overflow-x: scroll; // magic is here 
} 

這就是全部:)。無需JavaScript即可使其工作:http://codepen.io/anon/pen/RarPxp

+0

好,簡單的答案。我建議使用'.tab-nav-wrapper> ul :: - webkit-scrollbar {display:none;}來隱藏滾動條。 }而不是調整包裝的大小來隱藏它。 – ArneHugo

+0

@ArneHugo謝謝!這是一個偉大的建議:)。但是,我認爲它不適用於所有瀏覽器,是嗎? – sjkm

+0

我無法找到瀏覽器對滾動條支持的更新概述,但是我確實發現了這個http://stackoverflow.com/a/14150577/2054731所以你是對的,它不能保證適用於所有瀏覽器。 – ArneHugo