[英]twitter bootstrap carousel link directly to a specific slide
I am using twitter bootstrap carousel on my website to display a set of images. 我在我的网站上使用twitter bootstrap carousel来显示一组图像。 i am wondering if it is possible to allow a link to link to a specific slide in the carousel.
我想知道是否可以允许链接链接到轮播中的特定幻灯片。 So out of 10 slides a user can click a link to go directly to say slide 5 from a href link.
因此,在10个幻灯片中,用户可以单击链接直接从href链接说出幻灯片5。
Is this possible with bootstrap carousel or am i barking up the wrong tree? 这是可能的自助旋转木马还是我咆哮错误的树?
EDIT 编辑
Hi, so i have added the following to my javascript under document.ready function. 嗨,所以我在document.ready函数下添加了以下javascript。
However typing in the url www.example.com/models.php/5
doesnt do anything. 但是,在网址
www.example.com/models.php/5
输入内容并不能做任何事情。
Am i doing something wrong? 难道我做错了什么?
var url = window.location.href;
var slide = url.substring(url.lastIndexOf('/')+1); // 4
goToSlide(slide);
function goToSlide(number) {
$("#MyCarousel").carousel(number);
}
You can use data-slide-to
attribute for this, which accepts 0-based indexes represents the slide number. 您可以使用
data-slide-to
属性 ,接受从0开始的索引表示幻灯片编号。
Use data attributes to easily control the position of the carousel.
使用数据属性可以轻松控制轮播的位置。
data-slide
accepts the keywordsprev
ornext
, which alters the slide position relative to its current position.data-slide
接受关键字prev
或next
,这会改变相对于当前位置的幻灯片位置。 Alternatively, usedata-slide-to
to pass a raw slide index to the carouseldata-slide-to="2"
, which shifts the slide position to a particular index beginning with0
.或者,使用
data-slide-to
将原始幻灯片索引传递给轮播data-slide-to="2"
,这会将幻灯片位置移动到以0
开头的特定索引。
Just add an attribute to your anchor, and off you go. 只需在锚点中添加一个属性,然后就可以了。
<a href="#" data-slide-to="5">Go to slide #5</a>
Thanks to Cawas's comment on this answer and Jonas's answer on this question . 感谢Cawas对这个答案的评论以及Jonas对这个问题的回答 。
You need to use the .carousel(number)
function. 您需要使用
.carousel(number)
函数。
.carousel(number)
.carousel(数字)
Cycles the carousel to a particular frame (0 based, similar to an array).
将轮播循环到特定帧(基于0,类似于数组)。
Check out the bootstrap docs. 查看bootstrap文档。
To be more specific; 更具体;
Create a javascript function to call, it should look like this : 创建一个要调用的javascript函数,它应该如下所示:
function goToSlide(number) {
$("#carousel").carousel(number);
}
And then, just add an onClick event on your link. 然后,只需在链接上添加onClick事件即可。
<a href="#" onClick="javascript:goToSlide(5);">Go to slide #5</a>
The source link is www.example.com/models.php/4
源链接是
www.example.com/models.php/4
You could check the url on document.ready
, and if there's some data at the end of the url, you simply invoke your goToSlide function. 您可以检查
document.ready
上的url,如果url末尾有一些数据,则只需调用goToSlide函数即可。
var url = window.location.href;
var slide = url.substring(url.lastIndexOf('/')+1); // 4
// Remove the trailing slash if necessary
if( slide.indexOf("/") > -1 ) {
var slideRmvSlash = slide.replace('/','');
slide = parseInt(slideRmvSlash);
}
goToSlide(slide);
I think you're looking for this http://getbootstrap.com/javascript/#carousel 我想你正在寻找这个http://getbootstrap.com/javascript/#carousel
Use data attributes to easily control the position of the carousel.
使用数据属性可以轻松控制轮播的位置。
data-slide
accepts the keywordsprev
ornext
, which alters the slide position relative to its current position.data-slide
接受关键字prev
或next
,这会改变相对于当前位置的幻灯片位置。 Alternatively, usedata-slide-to
to pass a raw slide index to the carouseldata-slide-to="2"
, which shifts the slide position to a particular index beginning with0
.或者,使用
data-slide-to
将原始幻灯片索引传递给轮播data-slide-to="2"
,这会将幻灯片位置移动到以0
开头的特定索引。
Following up on Seçkin's answer, I had to perform two small string operations to make my slide
string usable. 继续Seçkin的回答,我不得不执行两个小的字符串操作来使我的
slide
可用。 These two mods let the goToSlide
function correctly parse external links. 这两个mod让
goToSlide
函数正确地解析外部链接。
Strip trailing slash from url. 从URL中删除尾部斜杠。 (My site's web server inserts the slash.)
(我的网站的Web服务器插入斜杠。)
var slideRmvSlash = slide.replace('/','');
Convert to an integer 转换为整数
var slideInt = parseInt(slideRmvSlash);
It worked fine after that. 之后它运作良好。
goToSlide(slideInt)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.