圖片+說明

某網友問

您好,您網站上的圖片+圖說的樣式很好看,可以請教一下,是怎樣做出來的呢?

是不是有plugin可使用?

答:沒有 plugin 可用,這是為了分享我家天秘近況,使用 JavaScript + css 小技巧弄出來的玩意兒,以下為代碼

JavaScript

javascript · [高亮] · [原始]

  1. (function($) {
  2.   $.fn.figure = function() {
  3.     $(this).each(function(i) {
  4.       var $self = $(this),
  5.           $img  = $self.find(‘img’);
  6.           $html = $self.find(‘p.figure_html’),
  7.           width = $img.width(),
  8.           text  = $html.length ? $html.remove().html() : $img.attr(‘alt’);
  9.  
  10.       $self.width(width).append(‘<p class="caption"><em class="title">Figure ‘+(++i)+‘</em>’+text+‘</p>’);
  11.     });
  12.   };
  13.  
  14.   $(document).ready(function() {
  15.     $(‘div.figure’).figure();
  16.   });
  17. })(jQuery);
(function($) {
  $.fn.figure = function() {
    $(this).each(function(i) {
      var $self = $(this),
          $img  = $self.find(\'img\');
          $html = $self.find(\'p.figure_html\'),
          width = $img.width(),
          text  = $html.length ? $html.remove().html() : $img.attr(\'alt\');

      $self.width(width).append(\'<p class="caption"><em class="title">Figure \'+(++i)+\'</em>\'+text+\'</p>\');
    });
  };

  $(document).ready(function() {
    $(\'div.figure\').figure();
  });
})(jQuery);

以上代碼需存為 .js 檔,搭配 jQuery 一同服用。
在網頁的 <head> 範圍內或 </body> 之前加入讀取 JavaScript 的代碼

xml · [高亮] · [原始]

  1. <script type="text/javascript" src="http://your.domain.com/path/to/jquery.js"></script>
  2. <script type="text/javascript" src="http://your.domain.com/path/to/figure.js"></script>
<script type="text/javascript" src="http://your.domain.com/path/to/jquery.js"></script>
<script type="text/javascript" src="http://your.domain.com/path/to/figure.js"></script>

CSS

css · [高亮] · [原始]

  1. .figure {
  2.   margin: 0 0 1.5em;
  3.   color: black;
  4.   background: #fee;
  5.   border: 1px solid #edd;
  6.   padding: 5px;
  7.  
  8. }
  9. .figure .figure_html {
  10.   display: none;
  11. }
  12. .figure img,
  13. .figure .caption,
  14. .figure .figure_html {
  15.   margin: 0;
  16.   padding: 0;
  17. }
  18. .figure .title {
  19.   font-weight: bold;
  20.   padding-right: 2px
  21. }
  22. .figure .title:after {
  23.   content: ‘:’
  24. }
.figure {
  margin: 0 0 1.5em;
  color: black;
  background: #fee;
  border: 1px solid #edd;
  padding: 5px;

}
.figure .figure_html {
  display: none;
}
.figure img,
.figure .caption,
.figure .figure_html {
  margin: 0;
  padding: 0;
}
.figure .title {
  font-weight: bold;
  padding-right: 2px
}
.figure .title:after {
  content: \':\'
}

使用方式:

xml · [高亮] · [原始]

  1. <!–無 HTML 的圖片說明–>
  2. <div class="figure">
  3.   <img src="your_photo.png" alt="圖片說明" />
  4. </div>
  5.  
  6. <!–使用 HTML 的圖片說明–>
  7. <div class="figure">
  8.   <img src="your_photo.png" alt="圖片說明" />
  9.   <p class="figure_html">使用 <acronym title="Hypertext Markup Language">HTML</acronym> 的圖片說明</p>
  10. </div>
<!--無 HTML 的圖片說明-->
<div class="figure">
  <img src="your_photo.png" alt="圖片說明" />
</div>

<!--使用 HTML 的圖片說明-->
<div class="figure">
  <img src="your_photo.png" alt="圖片說明" />
  <p class="figure_html">使用 <acronym title="Hypertext Markup Language">HTML</acronym> 的圖片說明</p>
</div>

參考文件:If I Told You You Had a Beautiful Figure…


5 Responses to “圖片+說明”

  1. Akay Says:

    這個的確蠻有趣,這樣圖片版式好看多了,謝謝分享。

  2. d4mnphr34k3r Says:

    thanks 4 the code

  3. Wordpress博客的图片与文字分段 | IamVIP!-Jack Park Says:

    [...] Update: 大家也可以去看看Akay推荐的解决方法。 [...]

  4. converter Says:

    的确是很棒的效果
    谢谢提供代码

  5. bolo Says:

    效果在哪里?

Leave a Comment