简体   繁体   English

Bootstrap Carousel对于每个幻灯片都有不同的URL

[英]Bootstrap Carousel to have different URL for each slide

My bootstrap carousel is located here: testdomain.org/about.html and when the slide changes, the URL in the address bar remains the same. 我的引导轮播位于: testdomain.org/about.html并且当幻灯片更改时,地址栏中的URL保持不变。 I want the URL to auto change when the slides change to something like this: 当幻灯片更改为以下格式时,我希望URL自动更改:

https://testdomain.org/about.html#slide1
https://testdomain.org/about.html#slide2
https://testdomain.org/about.html#slide3

How can we make this happen?I created this JSFiddle Demo for your convenience. 我们如何做到这一点?为方便起见,我创建了这个JSFiddle演示 I don't know how to go about writing JS for this :( 我不知道该怎么写JS :(

It's the standard bootstrap HTML: 这是标准的引导HTML:

<div id="myCarousel" class="carousel slide" data-ride="carousel">
  <!-- Wrapper for slides -->
  <div class="carousel-inner" role="listbox">
    <div class="item active">
      <img src="img_chania.jpg" alt="Chania">
      <div class="">
        <h3>Chania</h3>
        <p>The atmosphere in Chania has a touch of Florence and Venice.</p>
      </div>
    </div>

    <div class="item">
      <img src="img_chania2.jpg" alt="Chania">
      <div class="">
        <h3>Chania</h3>
        <p>The atmosphere in Chania has a touch of Florence and Venice.</p>
      </div>
    </div>

    <div class="item">
      <img src="img_flower.jpg" alt="Flower">
      <div class="">
        <h3>Flowers</h3>
        <p>Beatiful flowers in Kolymbari, Crete.</p>
      </div>
    </div>

    <div class="item">
      <img src="img_flower2.jpg" alt="Flower">
      <div class="">
        <h3>Flowers</h3>
        <p>Beatiful flowers in Kolymbari, Crete.</p>
      </div>
    </div>
  </div>

  <!-- Left and right controls -->
  <a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
    <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
    <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

DEMO Screenshot of the URL in the Address bar should look like this DEMO地址栏中的URL屏幕截图应如下所示 在此处输入图片说明

Use the carousel Events to determine when a slide transition happens and which slide has been activated. 使用轮播事件来确定何时发生幻灯片过渡以及激活了哪个幻灯片。 Then update the address bar and browser history with the slide URL. 然后使用幻灯片URL更新地址栏和浏览器历史记录。 Instructions for both are at the links below: 两者的说明位于以下链接:

http://getbootstrap.com/javascript/#carousel-events http://getbootstrap.com/javascript/#carousel-events

Updating address bar with new URL without hash or reloading the page 使用新的URL更新地址栏,而不散列或重新加载页面

Try adding the following javascript, amending references where applicable. 尝试添加以下javascript,并在适用时修改参考。 You will also need to add an ID to <div class="carousel-inner" role="listbox"> . 您还需要将ID添加到<div class="carousel-inner" role="listbox"> I worked the below off the following - <div class="carousel-inner" id="listbox" role="listbox"> . 我根据以下内容进行了以下操作- <div class="carousel-inner" id="listbox" role="listbox">

childsList = Array.prototype.slice.call( document.getElementById('listbox').children );

$("#myCarousel").on('slide.bs.carousel', function (event) {
  window.location.hash = "slide" + (childsList.indexOf(event.relatedTarget) + 1);
}

This picks up the event of a slide changing, picks up the index of the slide and sets the '#' in the URL to '#slide[index of slide + 1]'. 这将获取幻灯片更改的事件,获取幻灯片的索引,并将URL中的“#”设置为“ #slide [幻灯片的索引+ 1]”。 I hope this helps. 我希望这有帮助。

Here is a fiddle doing the same thing but outputting to a rather than the URL. 这是一件小提琴,在做同样的事情,但输出到而不是URL。 https://jsfiddle.net/JokerDan/22botkvm/ https://jsfiddle.net/JokerDan/22botkvm/

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

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