简体   繁体   English

结合侧边栏菜单和轮播

[英]Combine sidebar menu and carousel

I have a sidebar menu and a bootstrap carousel and I managed to connect the carousel to the sidebar menu such that according to what item is shown in the carousel, the corresponding option in the menu is highlighted. 我有一个侧边栏菜单和一个自举轮播,并且设法将转盘连接到侧边栏菜单,以便根据轮播中显示的项目,突出显示菜单中的相应选项。

How can I do this the other way around such that when an option is selected from the menu, the appropriate item in the carousel is shown. 我该如何反过来做呢?当从菜单中选择一个选项时,轮播中的相应项目就会显示出来。

Here is my code: 这是我的代码:

 $('.carousel').on('slid.bs.carousel', function() { var carouselData = $(this).data('bs.carousel'); var currentIndex = carouselData.getItemIndex(carouselData.$element.find('.item.active')); $('.result p') .removeClass('active-p') .eq(currentIndex) .addClass('active-p'); }); 
 .active-p { background-color: #eee; } 
 <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.7/js/bootstrap.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <div id="sideMenu_div" class="result"> <p class="active-p">Option 1</p> <p> Option 2</p> <p> Option 3</p> </div> <div id="myCarousel" class="carousel slide" data-ride="carousel"> <div class="carousel-inner"> <div class="item active"> <div>Item 1</div> </div> <div class="item"> <div>Item 2</div> </div> <div class="item"> <div>Item 3</div> </div> </div> <a class="left carousel-control" href="#myCarousel" data-slide="prev"> <span class="glyphicon glyphicon-chevron-left"></span> <span class="sr-only">Previous</span> </a> <a class="right carousel-control" href="#myCarousel" data-slide="next"> <span class="glyphicon glyphicon-chevron-right"></span> <span class="sr-only">Next</span> </a> </div> 

Find the index with jQuery and tell the Bootstrap carousel to move to that index. 使用jQuery查找索引,并告诉Bootstrap轮播移动到该索引。

 $('.carousel').on('slid.bs.carousel', function() { var carouselData = $(this).data('bs.carousel'); var currentIndex = carouselData.getItemIndex(carouselData.$element.find('.item.active')); $('.result p') .removeClass('active-p') .eq(currentIndex) .addClass('active-p'); }); $('#sideMenu_div>p').on('click', function(e) { var currentIndex = $(this).index(); $('#myCarousel').carousel(currentIndex); }) 
 .active-p { background-color: #eee; } 
 <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.7/js/bootstrap.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <div id="sideMenu_div" class="result"> <p class="active-p">Option 1</p> <p> Option 2</p> <p> Option 3</p> </div> <div id="myCarousel" class="carousel slide" data-ride="carousel"> <div class="carousel-inner"> <div class="item active"> <div>Item 1</div> </div> <div class="item"> <div>Item 2</div> </div> <div class="item"> <div>Item 3</div> </div> </div> <a class="left carousel-control" href="#myCarousel" data-slide="prev"> <span class="glyphicon glyphicon-chevron-left"></span> <span class="sr-only">Previous</span> </a> <a class="right carousel-control" href="#myCarousel" data-slide="next"> <span class="glyphicon glyphicon-chevron-right"></span> <span class="sr-only">Next</span> </a> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM