nissaya.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. var _nsy_book_dir = "";
  2. var _nsy_book_id = "";
  3. function nissaya_load(nsyid,book, para, begin = 0, end = 0){
  4. if(nsyid==0){
  5. nissaya_get_books(book, para);
  6. }else{
  7. nissaya_get(nsyid,book, para, begin, end );
  8. }
  9. }
  10. function render_nissaya_books(list,book,para,begin=0,end=0){
  11. let books = JSON.parse(list);
  12. let html ="<ol>";
  13. for (const iterator of books) {
  14. html += "<li>";
  15. html += "<a href='index.php?book="+book+"&par="+para+"&begin="+begin+"&end="+end+"&nsyid=a"+iterator.nsyid+"'>";
  16. html += iterator.nsyid + "-" + iterator.name;
  17. html += "</a>";
  18. html += "</li>";
  19. }
  20. html += "</ol>";
  21. return html;
  22. }
  23. function nissaya_get_books(book, para) {
  24. $.get(
  25. "../nissaya/get_book_list.php",
  26. {
  27. book: book,
  28. para: para,
  29. },
  30. function (data) {
  31. let result = JSON.parse(data);
  32. if (result.error == "") {
  33. if (result.data.length > 0) {
  34. //找到的书的列表
  35. $("#contence").html(render_nissaya_books(result.data,book,para));
  36. }
  37. }
  38. }
  39. );
  40. }
  41. function nissaya_get(nissayabook,book, para, begin = 0, end = 0) {
  42. if (book == 0 || para == 0) {
  43. return;
  44. }
  45. $.get(
  46. "../nissaya/get.php",
  47. {
  48. book: book,
  49. para: para,
  50. begin: begin,
  51. end: end,
  52. nsyid:nissayabook,
  53. },
  54. function (data) {
  55. let result = JSON.parse(data);
  56. if (result.error == "") {
  57. if (result.data.length > 0) {
  58. //找到的书的列表
  59. for (const iterator of result.data) {
  60. }
  61. $("#contence").html(render_nissaya_init(result.data[0]));
  62. insert_new_end(1);
  63. }
  64. }
  65. }
  66. );
  67. }
  68. function render_on_page(params) {
  69. //加入前导零补足3位
  70. let prefix='';
  71. if(params.page < 10){
  72. prefix='00';
  73. }else if(params.page < 100){
  74. prefix='0';
  75. }
  76. let filename = params.dir + "/" + params.book + "/" + prefix + params.page + ".png";
  77. let html = "";
  78. html += "<div class='img_box' dir='" + params.dir + "' book='" + params.book + "' page='" + params.page + "'>";
  79. html += "<div>" + filename + "</div>";
  80. let filePath = ASSETS_SERVER + "/nissaya/";
  81. if (params.show) {
  82. html += "<img class='book_page' src='" + filePath + filename + "' />";
  83. } else {
  84. html += "<img class='book_page' data-src='" + filePath + filename + "' />";
  85. }
  86. html += "</div>";
  87. return html;
  88. }
  89. function render_nissaya_init(data) {
  90. let dir = data.dir;
  91. return render_on_page({
  92. dir: dir,
  93. book: data.nsyid,
  94. page: data.nsypagenumber,
  95. show: true,
  96. });
  97. }
  98. function insert_new_befor(num = 2) {
  99. let head = $("#contence").children().first();
  100. let pageid = parseInt(head.attr("page"));
  101. if (pageid <= 1) {
  102. return "";
  103. }
  104. let html = render_on_page({
  105. dir: head.attr("dir"),
  106. book: head.attr("book"),
  107. page: parseInt(head.attr("page")) - 1,
  108. show: false,
  109. });
  110. $("#contence").prepend(html);
  111. }
  112. function insert_new_end(num = 2) {
  113. let last = $("#contence").children().last();
  114. let html = render_on_page({
  115. dir: last.attr("dir"),
  116. book: last.attr("book"),
  117. page: parseInt(last.attr("page")) + 1,
  118. show: false,
  119. });
  120. $("#contence").append(html);
  121. }
  122. function check_end() {
  123. let last = $(".book_page").last();
  124. let attr = last.attr("src");
  125. if (typeof attr == typeof undefined || attr == false || attr.length == 0) {
  126. return;
  127. } else {
  128. insert_new_end();
  129. }
  130. }
  131. function check_head() {
  132. let first = $(".book_page").first();
  133. let attr = first.attr("src");
  134. if (typeof attr == typeof undefined || attr == false || attr.length == 0) {
  135. return;
  136. } else {
  137. insert_new_befor();
  138. }
  139. }
  140. window.onload = function () {
  141. window.onscroll = () => {
  142. checkImgs();
  143. };
  144. function checkImgs(el) {
  145. const imgs = document.querySelectorAll(".book_page");
  146. Array.from(imgs).forEach((el) => {
  147. // 在这里更换不同的方法即可
  148. if (isInViewPort(el)) loadImg(el);
  149. });
  150. check_end();
  151. check_head();
  152. }
  153. function loadImg(el) {
  154. if (!el.src) {
  155. const source = el.dataset.src;
  156. el.src = source;
  157. }
  158. // 这里应该是有一个优化的地方,设一个标识符标识已经加载图片的index,当滚动条滚动时就不需要遍历所有的图片,只需要遍历未加载的图片即可。
  159. }
  160. function isInViewPort(el) {
  161. const viewPortHeight =
  162. window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
  163. let rect = el.parentNode.getBoundingClientRect();
  164. if (rect.top > viewPortHeight || rect.bottom < 0) {
  165. return false;
  166. } else {
  167. return true;
  168. }
  169. }
  170. };
  171. /*
  172. 作者:付出
  173. 链接:https://juejin.cn/post/6844903725249609741
  174. 来源:掘金
  175. 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  176. */
  177. /*
  178. function isInViewPortOfOne(el) {
  179. // viewPortHeight 兼容所有浏览器写法
  180. const viewPortHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
  181. const offsetTop = el.offsetTop;
  182. const scrollTop = document.documentElement.scrollTop;
  183. const top = offsetTop - scrollTop;
  184. console.log("top", top);
  185. // 这里有个+100是为了提前加载+ 100
  186. return top <= viewPortHeight + 100;
  187. }
  188. */