我有一个来自Flickity的旋转木马,我想隐藏其中的左右箭头和分页。
jQuery代码
$('.carousel').each(function() {
if ($(this).find('div.carousel-cell').length === 1)
$(this).find('button.flickity-prev-next-button previous, button.flickity-prev-next-button next, button.flickity-page-dots').hide();
});
轮播代码
<div class="carousel mt-1x mb-1x flickity-enabled" data-navdots="true" data-auto="false" data-buttons="true" data-wrap="true" data-adaptiveheight="false" tabindex="0">
<div class="flickity-viewport" style="height: 385.188px; touch-action: pan-y;">
<div class="flickity-slider" style="left: 0px; transform: translateX(0%);">
<div class="carousel-item is-selected" aria-selected="true" style="position: absolute; left: 0%;">
<img src="/img/asset/bWFpbi9oZWFsdGhjYXJlL1BSSU1BUlkvQlVSR09QQUstQkxJU1RFUi1QUklNQVJZLTEuanBn?w=2880&h=1600&s=c58a408a48a295ed53498fd03655878f" alt="" title="" class="mb-1x">
</div>
</div>
</div>
<button class="flickity-button flickity-prev-next-button previous" type="button" disabled="" aria-label="Previous">
<svg class="flickity-button-icon" viewBox="0 0 100 100">
<path d="M 10,50 L 60,100 L 70,90 L 30,50 L 70,10 L 60,0 Z" class="arrow"></path>
</svg>
</button>
<button class="flickity-button flickity-prev-next-button next" type="button" disabled="" aria-label="Next">
<svg class="flickity-button-icon" viewBox="0 0 100 100">
<path d="M 10,50 L 60,100 L 70,90 L 30,50 L 70,10 L 60,0 Z" class="arrow" transform="translate(100, 100) rotate(180) "></path>
</svg>
</button>
<ol class="flickity-page-dots">
<li class="dot is-selected" aria-label="Page dot 1" aria-current="step"></li>
</ol>
答案 0 :(得分:0)
我认为发生这种情况的原因是,您在快速创建所有必要元素之前运行了代码。因此,如果您将代码包含在函数中,然后稍后通过setTimeout对其进行调用,则一切正常。但是首先,您需要更正选择器。正确的选择器看起来像button.flickity-prev-next-button.previous, button.flickity-prev-next-button.next, ol.flickity-page-dots
,而不是您的`button.flickity-prev-next-button上一个,button.flickity-prev-next-button下一个,button.flickity-page-dots'。最后看起来可能是
function x() {
$('.carousel').each(function() {
if ($(this).find('div.carousel-cell').length === 1) {
$(this).find('button.flickity-prev-next-button.previous, button.flickity-prev-next-button.next, ol.flickity-page-dots').hide();
}
});
};
setTimeout(x,1000);