Javascript 利用 Bootstrap Carousel "slide" 事件和 .next 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14886233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 18:14:05  来源:igfitidea点击:

Utilise the Bootstrap Carousel "slide" event and the .next class

javascriptjqueryhtmltwitter-bootstrapcarousel

提问by Sheixt

So I've got a little problem (similar to this one I posted the other day: http://bit.ly/11JpbdY) with using SlabText on a piece of content that is hidden on load. This time, I'm trying to get slabText to update the display of some content that is in a slider (using Twitter Bootstrap's Carousel plugin).

所以我遇到了一个小问题(类似于我前几天发布的这个:http: //bit.ly/11JpbdY)在加载时隐藏的一段内容上使用 SlabText。这一次,我试图让slabText 更新滑块中某些内容的显示(使用Twitter Bootstrap 的Carousel 插件)。

Following Twitter's documentation (http://twitter.github.com/bootstrap/javascript.html#carousel) for Bootstrap's Carousel plugin, I'm trying to use slideevent so that I re-call SlabText to make it display correctly.

按照 Twitter 的文档 ( http://twitter.github.com/bootstrap/javascript.html#carousel) 的 Bootstrap 轮播插件,我尝试使用slide事件,以便我重新调用 SlabText 以使其正确显示。

Using developer tools I can see that Carousel adds a .nextclass as it processes the slide of one .itemelement to the next. This is then removed before the .activeclass is transferred.

使用开发人员工具,我可以看到 Carousel.next在处理一个.item元素到下一个元素的滑动时添加了一个类。然后在.active转移班级之前将其删除。

I can access the "slide" event without any issue, but trying to get hold of the .nextelement is proving troublesome. Here is a JSFiddle of my code thus far: http://jsfiddle.net/peHDQ/

我可以毫无问题地访问“幻灯片”事件,但尝试获取.next元素证明很麻烦。这是迄今为止我的代码的 JSFiddle:http: //jsfiddle.net/peHDQ/

To put my question simply; how should I correctly use the slideevent to trigger a function?

简单地说我的问题; 我应该如何正确使用slide事件来触发功能?

Please let me know if any additional information would be useful.

请让我知道是否有任何其他信息有用。



Further notes:

补充说明:

As I've been unable to get hold of the .nextclass, I'm attempting to do this with some jQuery. Here is my code thus far:

由于我一直无法掌握.next课程,因此我正在尝试使用一些 jQuery 来做到这一点。到目前为止,这是我的代码:

$('.carousel').carousel({
    interval: 5000
}).on('slide', function (e) {
    $(this).find('.active').next().find('.slab').slabText();
});

From what I understand this should be grabbing each .slabelement and triggering the SlabText plugin.... alas I get an error:

据我了解,这应该是抓取每个.slab元素并触发 SlabText 插件......唉,我收到一个错误:

"Uncaught TypeError: Object [object Object] has no method 'slabtext' "

“未捕获的类型错误:对象 [对象对象] 没有方法 'slabtext'”

Can anyone advise what I am doing wrong here...?I've used the exact same process to add a class and it works fine (as per this JSFiddle: http://jsfiddle.net/peHDQ/2/)

任何人都可以建议我在这里做错了什么......?我使用了完全相同的过程来添加一个类并且它工作正常(根据这个 JSFiddle:http: //jsfiddle.net/peHDQ/2/

回答by Tarandeep Gill

I identified the problem. The issue is that the event 'slide' is called before the next slide is made visible. I added a little delay, and it works fine now. Try this:

我发现了问题。问题是在下一张幻灯片可见之前调用了事件“幻灯片”。我添加了一点延迟,现在工作正常。尝试这个:

   $('.carousel').carousel({
       interval: 5000
   }).on('slide', function (e) {
       var xx = $(this);
       setTimeout(function() {
           xx.find('.active').next().find('.slab').slabText();
       } , 0);
   });