[英]Twitter Bootstrap expanding text using the “Collapse” plugin
有没有人想出如何使用twitter bootstrap collapse插件显示一些文本(例如,100个字符),然后单击一个按钮展开文本以显示其余部分?
icon-plus-sign
指示符的显示不会切换(显示隐藏文本时不会消失,反之亦然)。 完全正常的HTML:
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.0/css/bootstrap-combined.min.css" rel="stylesheet">
<script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.0/js/bootstrap.min.js"></script>
<div class="" data-toggle="collapse" data-target="#div1" >
<span><b>Div1:</b> This is a sentence at the end of the first 100 characters that gets truncated </span>
<i class='icon-plus-sign'></i>
</div>
<div id="div1" class="collapse" >such that characters 101 to infinity are not shown inline with the beginning of the sentence (<b>Div1</b>).</div>
<div class="" data-toggle="collapse" data-target="#div2" >
<span><b>Div2:</b>This is a sentence at the end of the first 100 characters that gets truncated </span>
<i class='icon-plus-sign'></i>
</div>
<div id="div2" class="collapse" >such that characters 101 to infinity are not shown inline with the beginning of the sentence (<b>Div2</b>).</div>
我通过添加以下JavaScript来实现此目的:
$("div").on("hide", function() {
$(this).prev("div").find("i").attr("class","icon-plus-sign");
$(this).css("display","");
$(this).css("height","5px");
});
$("div").on("show", function() {
$(this).prev("div").find("i").attr("class","icon-minus-sign");
$(this).css("display","inline");
$(this).css("height","5px");
});
以下CSS:
div[data-toggle="collapse"] {
float: left;
}
示例小提琴在这里 。
我个人不会为此使用Bootstrap崩溃插件。 我会使用像JQuery Expander这样的专用插件和Bootstrap图标。
我同意另一篇文章说崩溃插件不适用于此。 实际上很容易写出能很好地处理这个问题的东西。 例如,通过将隐藏文本包装在<span class="truncated">
:
<p>
This is the visible part <span class="truncated">this is the hidden part.</span>
</p>
然后隐藏它,附加隐藏/显示图标,并切换状态:
$('.truncated').hide() // Hide the text initially
.after('<i class="icon-plus-sign"></i>') // Create toggle button
.next().on('click', function(){ // Attach behavior
$(this).toggleClass('icon-minus-sign') // Swap the icon
.prev().toggle(); // Hide/show the text
});
这利用了icon-minus-sign
类的优先级优于icon-plus-sign
,但如果它让你觉得更安全,你可以添加两者的切换。
演示: http : //jsfiddle.net/VNdmZ/4/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.