nissaya.js 3.7 KB

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