2017-05-27 13 views
0

隱藏滑動控件所以我有這個滑塊,如果它只有一個元素,我想隱藏它的控件。如果滑塊長度<2

標記:

<div class="grid carousel-controls"> 
    <span class="control prev"></span> 
    <span class="control next"></span> 
</div> 

<div class="carousel"> 
    <img src="//placecage.com/440/660"/> 
</div> 

的jQuery:

$(function(){ 
    var count = $('.carousel').children().length; 

    if (count < 2) { 
     $('.carousel-controls').hide(); 
    } 
}); 

贊成嗎? ❤️

編輯:值得添加的是它在一個tab nav中,其中另一個carousel有> 2個孩子,在這種情況下我顯然希望控件可見。

+0

也許是它的一個片段或小提琴,所以我們可以想象它 –

+0

你的意思是你有兩個或更多的選項卡,每個選項卡有它自己的旋轉木馬? –

+0

@Dinesh是的,先生。 –

回答

0

您正在尋找$.each()

$(function(){ 
$('.carousel').each(function(i, c){ 
    var count = $(c).children().length; 

    // if the .carousel-controls is a children of .carousel 
    if (count < 3) 
     $(c).find('.carousel-controls').hide(); 

    // if the .carousel-controls is a siblings of .carousel 
    if (count < 2) 
     $(c).siblings('.carousel-controls').hide(); 

    // comment out the inappropriate syntax above. 


}); 
}); 

find()相對於.carousel,並取決於其中.carousel-controls放置。如果.carousel-controls裏面.carousel,這也將是考慮兒童.carousel所以你需要數小於3

+0

似乎沒有工作。 :\ –

+0

'.carousel-controls'是'.carousel'的兄弟。在這種情況下,代碼會有任何改變嗎? –

+0

@AronHøyer是所有的旋轉木馬內容(離開控制)圖像文件?因爲上面的代碼也在計算'.carousel-controls'。如果全部都是圖像,'var count = $(c).find('img').length'否則如果除了特殊div以外的圖像之外還有很多其他的東西,你可以嘗試'if <.carousel-controls'也是'.carousel'的一個孩子。# –

0

我可以給你出個主意。

假設:只有一個標籤將被激活一次只能活動標籤的

發現旋轉木馬控制。然後你可以根據邏輯控制隱藏它們。

$('a[data-toggle="tab"]').on('shown', function (e) { 

    // access carousel here then you can find children here using $(this); 
    // your logic goes here. 
}); 

這段代碼僅供參考。