简体   繁体   English

截断Twitter Bootstrap轮播的文本

[英]Truncate text of Twitter Bootstrap carousel

I want to truncate the text of the Bootstrap carousel .carousel-caption element. 我想截断Bootstrap轮播.carousel-caption元素的文本。 I am using shave.js to truncate the text. 我正在使用shave.js来截断文本。 However, the truncating only works on the first .carousel-caption element. 但是,截断仅适用于第一个.carousel-caption元素。

I`ve created a jsfiddle to demonstrate my problem: jsfiddle 我创建了一个jsfiddle来演示我的问题: jsfiddle

When I remove the CSS class carousel-inner from the carousel, the truncating works perfectly on booth .carousel-caption elements. 当我从旋转carousel-inner移除CSS类carousel-inner ,截断功能完全适用于booth .carousel-caption元素。

I am not pretty sure about the issue, but I think it's due to the dynamic behavior of the carousel so the shave code will only work on the first caption because it's visible and its height is set so your code should also be dynamic to call the shave funcion for each slide. 我不太确定这个问题,但我认为这是由于轮播的动态行为所以剃须代码只能在第一个标题上工作,因为它是可见的并且它的高度已经设置好所以你的代码也应该动态调用每张幻灯片的剃须函数。

An idea is to use the event provided by the carousel component and call the shave function there. 一个想法是使用轮播组件提供的事件并在那里调用剃须函数。

$('#carousel-example-generic').on('slid.bs.carousel', function () {
  $(".carousel-caption").shave(70);
})

slid.bs.carousel : This event is fired when the carousel has completed its slide transition. slid.bs.carousel :当轮播完成幻灯片过渡时会触发此事件。 ref REF

Full code : https://jsfiddle.net/dpnpo4o7/2/ 完整代码: https//jsfiddle.net/dpnpo4o7/2/


I used the jQuery version of the plugin in the code above but it works the same with the JS version: 我在上面的代码中使用了插件的jQuery版本,但它与JS版本的工作方式相同:

$('#carousel-example-generic').on('slid.bs.carousel', function () {
  shave(".carousel-caption", 70);
})

Full code : https://jsfiddle.net/dpnpo4o7/3/ 完整代码: https//jsfiddle.net/dpnpo4o7/3/

I went through the fiddle. 我经历了小提琴。 And shave must be finding those second captions but since it is invisible at that moment, it is failing to do that. 剃须必须找到那些第二个字幕,但由于它在那一刻是不可见的,所以它没有做到这一点。

Solution: You might want to capture the prev and next events and then try your code. 解决方案:您可能希望捕获上一个和下一个事件,然后尝试使用您的代码。

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

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