使用水平导航显示内容(JavaScript)

时间:2017-10-04 20:00:10

标签: javascript jquery html css flickity

我有一个使用CSS库Flickity的水平滚动导航栏。我修改了轮播模板,以便在选择轮播单元格时,它会捕捉到导航栏的中心。我在旋转木马中有5个单元格,名为“幻灯片1”,“幻灯片2”,依此类推。我正在试图弄清楚如何在选择旋转木马单元格时显示相应的内容。我需要隐藏内容。

 <nav>
 <div class="carousel" data-flickity>

            <div class="carousel-cell">
                <a href="#slide1">
                slide 1
                </a>
            </div>

            <div class="carousel-cell">
                <a href="#slide2">
                slide 2
                </a>
            </div>

            <div class="carousel-cell">
                <a href="#slide3">
                slide 3
                </a>
            </div>        

            <div class="carousel-cell">
                <a href="#slide4">
                slide 4
                </a>
                </div>

            <div class="carousel-cell">
                <a href="#slide5">
                slide 5
                </a>
            </div>    

 </div>
 </nav>    
 <!--Content-->
 <main>
 <div class="container">
          <div id="slide1">
            SLIDE 1
          </div>
          <div id="slide2">
           SLIDE 2
          </div>
          <div id="slide3">
           SLIDE 3
          </div>
          <div id="slide4">
           SLIDE 4
          </div>
          <div id="slide5">
           SLIDE 5
          </div>           
 </div>
 </main>

这是我的Codepen: https://codepen.io/loudenw/pen/jGaxzG

1 个答案:

答案 0 :(得分:2)

  

Flickity事件文档https://flickity.metafizzy.co/events.html#select

     

jQuery event.namespace https://api.jquery.com/event.namespace/

我依赖于select命名空间中的flickity事件,并显示了具有id = slide + currentSelectedSlide + 1的元素(因为脚本从0开始获得幻灯片编号,但是您的ID从1)开始

$(document).ready(function() {

  $('.carousel').on('select.flickity', function() {
    var selector = '#slide' + ($('.carousel-cell.is-selected').index()+ 1);
    $('.container').find('div').hide();
    $(selector).fadeIn();
  })

});
main .container {
  width: 100%;
  height: 100px;
  margin-top: 20px;
  background-color: red;
  text-align: center;
}

nav {
  width: 100%;
  height: 30px;
  background-color: #ffffff;
  text-align: center;
}

.hidden {
  display: none;
}

.carousel {
  background-color: #fff;
  width: 60%;
  margin: 0 20% 0 20%;
}

.carousel-cell {
  width: 50%;
  height: 30px;
  margin-right: 10px;
  background: #fff;
  border-radius: 5px;
  counter-increment: gallery-cell;
}

* {
  box-sizing: border-box;
}

.flickity-prev-next-button {
  width: 30px;
  height: 30px;
  background: transparent;
}

.flickity-prev-next-button:hover {
  background: transparent;
}


/* arrow color */

.flickity-prev-next-button .arrow {
  fill: #000;
}

.flickity-prev-next-button.no-svg {
  color: #000;
}

.carousel-cell {
  opacity: 0;
  transition: opacity .4s;
}

.carousel-cell.is-selected {
  opacity: 1;
}

.container>div {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://unpkg.com/flickity@2/dist/flickity.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet" />



<nav>
  <div class="carousel" data-flickity='{"initialIndex": 4, "pageDots": false } '>

    <div class="carousel-cell">
      <a href="#slide1">
                    slide 1
                    </a>
    </div>

    <div class="carousel-cell">
      <a href="#slide2">
                    slide 2
                    </a>
    </div>

    <div class="carousel-cell">
      <a href="#slide3">
                    slide 3
                    </a>
    </div>

    <div class="carousel-cell">
      <a href="#slide4">
                    slide 4
                    </a>
    </div>

    <div class="carousel-cell">
      <a href="#slide5">
                    slide 5
                    </a>
    </div>

  </div>
</nav>
<!--Content-->
<main>
  <div class="container">
    <div id="slide1" class="slide-related-content">
      SLIDE 1 RELATED CONTENT
    </div>
    <div id="slide2" class="slide-related-content">
      SLIDE 2 RELATED CONTENT
    </div>
    <div id="slide3" class="slide-related-content">
      SLIDE 3 RELATED CONTENT
    </div>
    <div id="slide4" class="slide-related-content">
      SLIDE 4 RELATED CONTENT
    </div>
    <div id="slide5" class="slide-related-content">
      SLIDE 5 RELATED CONTENT
    </div>
  </div>
</main>
<script src="https://unpkg.com/flickity@2/dist/flickity.pkgd.js"></script>

根据文档$flickity.data('flickity').selectedIndex应该使用jquery index()方法 - 但对我来说它不起作用