星火官网,前端页面(前台)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

news.js 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. $(function () {
  2. // $(".header_root").load("./comm/header.html");
  3. $(".footer_root").load("./comm/footer.html");
  4. // nav传值显示下划线
  5. // $(".header_root").data("nav_info", 5);
  6. // 新闻资讯搜索信息
  7. let newsInfo = {
  8. page: Number($.getQueryVariable("page") || 1),
  9. size: 9,
  10. newstitle: $.getQueryVariable("newstitle") || "",
  11. total: 0,
  12. };
  13. $(".news_info #search_help").val(newsInfo.newstitle);
  14. // 加载新闻资讯列表
  15. function loadNewsList(params) {
  16. let newsItemHtml = "";
  17. $.each(params.list, function (newsIndex, newsItem) {
  18. newsItemHtml +=
  19. '<div class="news_list_item" data-newsid="' +
  20. newsItem.newsid +
  21. '"><img class="news_item_img" src="' +
  22. $.showImageUrl +
  23. newsItem.newspic +
  24. '" /><div class="news_item_title">' +
  25. newsItem.newstitle +
  26. '</div><div class="news_item_stitle">' +
  27. $.msToDateString(newsItem.createtime * 1000) +
  28. "</div></div>";
  29. });
  30. $(".news_list_box").html(newsItemHtml);
  31. $(".news_list_box").on("click", ".news_list_item", function () {
  32. window.location.href =
  33. "/sections/news_detail.html?newsid=" + $(this).data("newsid");
  34. });
  35. }
  36. // 加载新闻资讯页码
  37. function loadNewsPages(params) {
  38. newsInfo.total = params.total || 0;
  39. if (newsInfo.total === 0) {
  40. $(".news_page_box").html("");
  41. return;
  42. }
  43. let newsPageHtml =
  44. '<div class="news_page_prev"><img src="../img/home/scheme/jiantou_right.png" /></div>';
  45. for (let _page = 0; _page < Math.ceil(newsInfo.total / 9); _page++) {
  46. newsPageHtml +=
  47. '<div data-page="' +
  48. (_page + 1) +
  49. '" class="news_page_num ' +
  50. (_page + 1 === newsInfo.page ? "selected" : "") +
  51. '">' +
  52. (_page + 1) +
  53. "</div>";
  54. }
  55. newsPageHtml +=
  56. '<div class="news_page_next"><img src="../img/home/scheme/jiantou_right.png" /></div>';
  57. $(".news_page_box").html(newsPageHtml);
  58. // 上下页禁止点击
  59. function setNotAllowed() {
  60. if (newsInfo.page === 1) {
  61. $(".news_page_box .news_page_prev").addClass("disabled");
  62. } else {
  63. $(".news_page_box .news_page_prev.disabled").removeClass("disabled");
  64. }
  65. if (newsInfo.page === Math.ceil(newsInfo.total / 9)) {
  66. $(".news_page_box .news_page_next").addClass("disabled");
  67. } else {
  68. $(".news_page_box .news_page_next.disabled").removeClass("disabled");
  69. }
  70. }
  71. setNotAllowed();
  72. $(".news_page_box").on("click", ".news_page_prev", function () {
  73. if (newsInfo.page > 1) {
  74. newsInfo.page--;
  75. $(".news_page_box .news_page_num.selected").removeClass("selected");
  76. $(
  77. ".news_page_box .news_page_num[data-page='" + newsInfo.page + "']"
  78. ).addClass("selected");
  79. setNotAllowed();
  80. // searchList();
  81. window.location.href = "/sections/news.html?page=" + newsInfo.page + "&newstitle=" + newsInfo.newstitle;
  82. }
  83. });
  84. $(".news_page_box").on("click", ".news_page_num", function () {
  85. newsInfo.page = $(this).data("page");
  86. $(".news_page_box .news_page_num.selected").removeClass("selected");
  87. $(
  88. ".news_page_box .news_page_num[data-page='" + newsInfo.page + "']"
  89. ).addClass("selected");
  90. setNotAllowed();
  91. // searchList();
  92. window.location.href = "/sections/news.html?page=" + newsInfo.page + "&newstitle=" + newsInfo.newstitle;
  93. });
  94. $(".news_page_box").on("click", ".news_page_next", function () {
  95. if (newsInfo.page < Math.ceil(newsInfo.total / 9)) {
  96. newsInfo.page++;
  97. $(".news_page_box .news_page_num.selected").removeClass("selected");
  98. $(
  99. ".news_page_box .news_page_num[data-page='" + newsInfo.page + "']"
  100. ).addClass("selected");
  101. setNotAllowed();
  102. // searchList();
  103. window.location.href = "/sections/news.html?page=" + newsInfo.page + "&newstitle=" + newsInfo.newstitle;
  104. }
  105. });
  106. }
  107. // 搜索新闻列表
  108. function searchList() {
  109. let _url = $.baseUrl + "n/listc";
  110. $.ajax({
  111. type: "POST",
  112. url: _url,
  113. dataType: "json",
  114. contentType: "application/json; charset=utf-8",
  115. data: JSON.stringify({
  116. page: newsInfo.page,
  117. size: newsInfo.size,
  118. newstitle: newsInfo.newstitle,
  119. }),
  120. success: function (data) {
  121. if (data.code === 0) {
  122. if (data.obj.list.length === 0 && newsInfo.page > 1) {
  123. newsInfo.page--;
  124. searchList();
  125. } else {
  126. loadNewsList(data.obj);
  127. loadNewsPages(data.obj);
  128. }
  129. } else {
  130. alert(data.msg);
  131. }
  132. },
  133. });
  134. }
  135. // 回车搜索
  136. function enterSearch(event) {
  137. // 兼容FF和IE和Opera
  138. let theEvent = event || window.event;
  139. let code = theEvent.keyCode || theEvent.which || theEvent.charCode;
  140. let content = theEvent.target.value;
  141. newsInfo.newstitle = content;
  142. if (code == 13) {
  143. // searchList();
  144. window.location.href = "/sections/news.html?page=" + newsInfo.page + "&newstitle=" + newsInfo.newstitle;
  145. }
  146. }
  147. // 输入内容改变
  148. function searchInputChange(event) {
  149. let theEvent = event || window.event;
  150. newsInfo.newstitle = theEvent.target.value;
  151. }
  152. $(".news_info #search_help").on("change", searchInputChange);
  153. $(".news_info #search_help").on("keydown", enterSearch);
  154. $(".news_info .search_box").click(function () {
  155. // searchList();
  156. window.location.href = "/sections/news.html?page=" + newsInfo.page + "&newstitle=" + newsInfo.newstitle;
  157. });
  158. searchList();
  159. });