DIV CSS 佈局教程網

jQuery 實作文字展開/收合特效的兩種寫法
編輯:JavaScript綜合知識     

版面空間有限,但說明文字很長——這種時候「先顯示一部分,點擊後展開」是很實用的解法。本文示範兩種以 jQuery 實作的展開/收合效果。

方法一:以點擊事件搭配狀態旗標

理論上 jQuery 內建的 toggle() 就能勝任,但實作時若遇到 toggle 不如預期運作,改用 click 加上一個狀態變數會更可控。做法是:先記錄原始文字與原始高度,再把內容截短並記下截短後的高度,之後依狀態在兩者之間切換並搭配高度動畫。

<script language="javascript" src="jquery.js"></script>
<script language="javascript">
var cur_status = "less";

$.extend({
  show_more_init: function () {
    var charNumbers = $(".content").html().length;  // 總字數
    var limit = 100;                                // 顯示字數

    if (charNumbers > limit) {
      var orgText = $(".content").html();          // 原始文本
      var orgHeight = $(".content").height();      // 原始高度
      var showText = orgText.substring(0, limit);  // 截短後的文本

      $(".content").html(showText);
      var contentHeight = $(".content").height();  // 截短後的高度

      $(".switch").click(function () {
        if (cur_status == "less") {
          $(".content").height(contentHeight).html(orgText)
            .animate({ height: orgHeight }, { duration: "slow" });
          $(this).html("收合");
          cur_status = "more";
        } else {
          $(".content").height(orgHeight).html(showText)
            .animate({ height: contentHeight }, { duration: "fast" });
          $(this).html("展開");
          cur_status = "less";
        }
      });
    } else {
      $(".switch").hide();   // 文字不夠長就不需要切換鈕
    }
  }
});

$(document).ready(function () {
  $.show_more_init();
});
</script>

結構與樣式:

<div id="limittext">
  <div class="content" style="padding-bottom:15px;">
    這裡是一段很長的說明文字……
  </div>
  <div class="switch">展開</div>
</div>

<style>
#limittext {
  width: 640px;
  height: auto;
  position: relative;
  background-color: #ccc;
  color: black;
}
.switch {
  font-size: 12px;
  text-align: center;
  cursor: pointer;
  font-family: Verdana;
  font-weight: 800;
  position: absolute;
  bottom: 0;
  width: 100%;
  height: 40px;
  line-height: 80px;
}
</style>

這個版本的好處是:只有在文字超過門檻時才顯示切換鈕,短內容不會出現多餘的按鈕。

方法二:直接對標題做 toggle 動畫

若需求只是「點標題收合/展開下方區塊」,用 toggle() 搭配 animate() 就夠簡潔了。

<script type="text/javascript">
$(document).ready(function () {
  $(".box h1").toggle(function () {
    $(this).next(".text").animate({ height: 'toggle', opacity: 'toggle' }, "slow");
  }, function () {
    $(this).next(".text").animate({ height: 'toggle', opacity: 'toggle' }, "slow");
  });
});
</script>

<style>
.text { line-height: 22px; padding: 0 6px; color: #666; }
.box h1 {
  padding-left: 10px;
  height: 22px;
  line-height: 22px;
  background: #f1f1f1;
  font-weight: bold;
}
.box { position: relative; border: 1px solid #e7e7e7; }
</style>

<div class="box">
  <h1>收合展開效果</h1>
  <div class="text">內容第一行<br />內容第二行</div>
</div>

這裡的關鍵是 next():它取得 h1 的下一個同層元素,讓同一段程式碼能套用到頁面上多個區塊,不必為每個區塊寫一次。第一次運行時若沒看到效果,重新整理頁面讓 jQuery 完整載入即可。

兩種方法的取捨

需要「文字截斷 + 高度動畫」的情境用方法一,因為它明確掌握了截短前後的高度差;只是單純收合整段區塊則用方法二,程式碼量少得多。另外提醒,.toggle(fn, fn) 這種雙函式寫法在較新版本的 jQuery 中已被移除,若使用 jQuery 1.9 以後的版本,請改用點擊事件搭配狀態判斷,也就是方法一的思路。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved