te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Twitter Bootstrap expanding text using the "Collapse" plugin - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Twitter Bootstrap expanding text using the "Collapse" plugin - Stack Overflow

programmeradmin3浏览0评论

Has anyone figured out how to use twitter bootstrap collapse plugin where some text is shown (say, 100 characters) and you click a button to expand the text to show the rest of it?

Here's a JSFiddle that demonstrates the following issues I'm having:

  1. The hidden text is shown on a new line. Would be preferable if sentences wouldn't be visually disrupted.
  2. the icon-plus-sign indicator's display isn't toggled (doesn't go away on showing hidden text and re-appear vice versa).

Fully working HTML:

<link href="//netdna.bootstrapcdn/twitter-bootstrap/2.3.0/css/bootstrap-bined.min.css" rel="stylesheet">
<script src="//netdna.bootstrapcdn/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>

Has anyone figured out how to use twitter bootstrap collapse plugin where some text is shown (say, 100 characters) and you click a button to expand the text to show the rest of it?

Here's a JSFiddle that demonstrates the following issues I'm having:

  1. The hidden text is shown on a new line. Would be preferable if sentences wouldn't be visually disrupted.
  2. the icon-plus-sign indicator's display isn't toggled (doesn't go away on showing hidden text and re-appear vice versa).

Fully working HTML:

<link href="//netdna.bootstrapcdn./twitter-bootstrap/2.3.0/css/bootstrap-bined.min.css" rel="stylesheet">
<script src="//netdna.bootstrapcdn./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>
Share Improve this question edited Feb 18, 2013 at 17:07 tim peterson asked Feb 18, 2013 at 15:53 tim petersontim peterson 24.3k63 gold badges184 silver badges302 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

I got this to work by adding the following 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");
});

And the following CSS:

div[data-toggle="collapse"] {
    float: left;
}

Example fiddle is here.

Sidenote

I personally would not use the Bootstrap collapse plugin for this. I would use a dedicated plugin like JQuery Expander in bination with Bootstrap icons.

A different example fiddle is here.

I agree with the other post that the collapse plugin is not meant for this. It is actually quite easy to write something that handles this nicely. For example, by wrapping the hidden text in <span class="truncated">:

<p>
 This is the visible part <span class="truncated">this is the hidden part.</span>
</p>

Then hide it, append the hide/show icon, and toggle state:

$('.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
});

This takes advantage of the priority of the icon-minus-sign class over the icon-plus-sign one, but you can add in a toggle for both if it makes you feel safer.

Demo: http://jsfiddle/VNdmZ/4/

发布评论

评论列表(0)

  1. 暂无评论