news-main.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <div class="news-main container">
  2. <div class="row">
  3. {{ $pages := first 6 (sort .Site.RegularPages "Date" "desc") }}
  4. <!-- 左側:index 0, 1, 2 -->
  5. <div class="col-lg-4">
  6. {{ range $index, $page := first 3 $pages }}
  7. <div class="border rounded overflow-hidden position-relative mb-3 left-content">
  8. <a href="{{ $page.Permalink }}">
  9. <img src="{{ $page.Params.image | relURL }}" alt="{{ $page.Params.title }}" class="img-fluid main-img">
  10. </a>
  11. <div class="p-3 title-info">
  12. {{ range ($page.GetTerms "categories") }}
  13. <a href="{{ .Permalink }}" class="me-2 badge-link">{{ .Params.title }}</a>
  14. {{ end }}
  15. <h2>
  16. <a href="{{ $page.Permalink }}">{{ $page.Params.title }}</a>
  17. </h2>
  18. <small class="d-block">{{ $page.Date | time.Format ":date_long" }}</small>
  19. </div>
  20. </div>
  21. {{ end }}
  22. </div>
  23. <!-- 右側:從 index 3 開始(3, 4, 5) -->
  24. <div class="col-lg-6">
  25. {{ range $index, $page := $pages }}
  26. {{ if eq $index 3 }}
  27. <!-- index 3:上方大卡片 -->
  28. <div class="d-flex mb-3 border rounded overflow-hidden position-relative right-content">
  29. <a href="{{ $page.Permalink }}" class="w-100">
  30. <img src="{{ $page.Params.image | relURL }}" alt="{{ $page.Params.title }}" class="img-fluid main-img">
  31. </a>
  32. <div class="p-3 title-info">
  33. {{ range ($page.GetTerms "categories") }}
  34. <a href="{{ .Permalink }}" class="me-2 badge-link">{{ .LinkTitle }}</a>
  35. {{ end }}
  36. <h2>
  37. <a href="{{ $page.Permalink }}">{{ $page.Params.title }}</a>
  38. </h2>
  39. <small class="d-block">{{ $page.Date | time.Format ":date_long" }}</small>
  40. </div>
  41. </div>
  42. {{ end }}
  43. {{ if eq $index 4 }}
  44. <!-- index 4 & 5:下方兩欄 -->
  45. <div class="row g-3">
  46. {{ end }}
  47. {{ if or (eq $index 4) (eq $index 5) }}
  48. <div class="col-sm-6">
  49. <div class="border rounded overflow-hidden position-relative bottom-content">
  50. <a href="{{ $page.Permalink }}">
  51. <img src="{{ $page.Params.image | relURL }}" alt="{{ $page.Params.title }}" class="img-fluid">
  52. </a>
  53. <div class="p-3 title-info">
  54. <h2>
  55. <a href="{{ $page.Permalink }}">{{ $page.Params.title }}</a>
  56. </h2>
  57. <small class="td-block">{{ $page.Date | time.Format ":date_long" }}</small>
  58. </div>
  59. </div>
  60. </div>
  61. {{ end }}
  62. {{ if eq $index 5 }}
  63. </div>
  64. {{ end }}
  65. {{ end }}
  66. </div>
  67. <!-- tags 生活 -->
  68. <div class="col-lg-2 d-none d-lg-block">
  69. <div class="local-video" id="local-news">
  70. <h3 class="post-block-title">
  71. <span>Local News</span>
  72. </h3>
  73. <!-- 收集前 6 則已經顯示過的文章(index 0~5) -->
  74. {{ $shownPermalinks := slice }}
  75. {{ range first 6 $pages }}
  76. {{ $shownPermalinks = $shownPermalinks | append .Permalink }}
  77. {{ end }}
  78. <!-- 先抓 tags 含「生活」的文章 -->
  79. {{ $lifeAll := where .Site.RegularPages "Params.tags" "intersect" (slice "生活") }}
  80. <!-- 排除掉前 6 則已顯示的文章 -->
  81. {{ $lifePages := where $lifeAll "Permalink" "not in" $shownPermalinks }}
  82. <!-- JS 處理隨機 -->
  83. {{ range first 12 $lifePages }}
  84. <div class="row align-items-center mb-2 local-item">
  85. <div class="col-12">
  86. <a href="{{ .Permalink }}">
  87. <img class="img-fluid" src="{{ .Params.image | relURL }}" alt="{{ .Params.title }}" />
  88. </a>
  89. <h2 class="mt-2 mb-1 post-title">
  90. <a href="{{ .Permalink }}">{{ .Params.title }}</a>
  91. </h2>
  92. </div>
  93. </div>
  94. {{ end }}
  95. </div>
  96. </div>
  97. <!-- <div class="col-lg-2 d-none d-lg-block">
  98. <div class="local-video" id="local-news">
  99. <h3 class="post-block-title">
  100. <span>Local News</span>
  101. </h3>
  102. {{/* 抓 tags 含「生活」的文章 */}}
  103. {{ $lifePages := where .Site.RegularPages "Params.tags" "intersect" (slice "生活") }}
  104. {{/* 這裡先不要 shuffle,交給 JS 處理隨機 */}}
  105. {{ range first 12 $lifePages }}
  106. <div class="row align-items-center mb-2 local-item">
  107. <div class="col-12">
  108. <a href="{{ .Permalink }}">
  109. <img class="img-fluid" src="{{ .Params.image | relURL }}" alt="{{ .Params.title }}" />
  110. </a>
  111. <h2 class="mt-2 mb-1 post-title">
  112. <a href="{{ .Permalink }}">{{ .Params.title }}</a>
  113. </h2>
  114. </div>
  115. </div>
  116. {{ end }}
  117. </div>
  118. </div> -->
  119. <!-- 影片 -->
  120. <!-- <div class="col-lg-2 d-none d-lg-block">
  121. <div class="news-video">
  122. <h3 class="post-block-title">
  123. <span>Video News</span>
  124. </h3>
  125. {{ range first 4 (sort (where .Site.RegularPages "Params.type" "video") "Date" "desc") }}
  126. <div class="row align-items-center mb-2">
  127. <div class="col-12">
  128. <a href="{{ .Permalink }}">
  129. <img class="img-fluid" src="{{ .Params.image | relURL }}" alt="" />
  130. </a>
  131. <h2 class="mt-2 mb-1 post-title">
  132. <a href="{{ .Params.url }}">{{ .Params.title }}</a>
  133. </h2>
  134. </div>
  135. </div>
  136. {{ end }}
  137. </div>
  138. </div> -->
  139. </div>
  140. </div>
  141. <!-- <div class="news-main mt-3 mt-lg-0">
  142. {{ range first 2 (where .Site.Pages "Params.type" "main") }}
  143. <div class="col-12 mb-3 d-flex flex-column-reverse flex-lg-column">
  144. <div>
  145. <section class="news-info">
  146. {{ range (.GetTerms "categories") }}
  147. <a href="{{ .Permalink }}">{{ .LinkTitle }}</a>
  148. {{ end }}
  149. <small>{{ .Date | time.Format ":date_long" }}</small>
  150. </section>
  151. <h2 class="post-title my-0 my-lg-3">
  152. <a href="{{ .Params.url }}">{{ .Params.title }}</a>
  153. </h2>
  154. </div>
  155. <a href="{{ .Permalink }}">
  156. <img class="img-fluid" src="{{ .Params.image | relURL }}" alt="" />
  157. </a>
  158. </div>
  159. {{ end }}
  160. </div> -->
  161. <script>
  162. document.addEventListener('DOMContentLoaded', function () {
  163. const container = document.querySelector('#local-news');
  164. if (!container) return;
  165. const items = Array.from(container.querySelectorAll('.local-item'));
  166. if (items.length <= 4) {
  167. // 不足 4 則就全部顯示,不用隨機
  168. return;
  169. }
  170. // Fisher-Yates 洗牌
  171. for (let i = items.length - 1; i > 0; i--) {
  172. const j = Math.floor(Math.random() * (i + 1));
  173. [items[i], items[j]] = [items[j], items[i]];
  174. }
  175. // 先全部隱藏
  176. items.forEach(function (item) {
  177. item.style.display = 'none';
  178. });
  179. // 顯示隨機後的前 4 則
  180. items.slice(0, 4).forEach(function (item) {
  181. item.style.display = '';
  182. });
  183. });
  184. </script>