【发布时间】:2017-01-12 05:01:28
【问题描述】:
我的引导轮播位于此处:testdomain.org/about.html,当幻灯片更改时,地址栏中的 URL 保持不变。我希望在幻灯片更改为以下内容时自动更改 URL:
https://testdomain.org/about.html#slide1
https://testdomain.org/about.html#slide2
https://testdomain.org/about.html#slide3
我们怎样才能做到这一点?为了您的方便,我创建了这个JSFiddle Demo。我不知道如何为此编写 JS :(
这是标准的引导 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>
【问题讨论】:
-
您需要为每张幻灯片提供一个 ID,并在加载时检查
location.hash和hashchange事件。 -
这里已经有人问过了:stackoverflow.com/questions/18339638/…。将每个图像包装在一个链接中。
-
@gcampbell 你能做一个工作演示吗?
-
@NathanielFlick:这不是我想要的。我希望在幻灯片更改时更改浏览器地址栏中的 URL。不向幻灯片添加链接。
-
@NathanielFlick 我认为这略有不同。
标签: javascript jquery css twitter-bootstrap