gaga.infinite.scrollLayer.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Inifinite Scroll Java Script written by gagamel.
  3. *
  4. * Copyright (c) 2016 gagamel (nogdoo.com)
  5. * Dual licensed under the MIT (MIT-LICENSE.txt)
  6. * and GPL (GPL-LICENSE.txt) licenses.
  7. *
  8. * $Date: 2016-04-28 $
  9. *
  10. * 사용 예)
  11. * // HTML 태그는 다음과 같은 구조로 되어 있어야 하며
  12. * // data-id는 layer_infinite,layer_infinite_item 가 필요하고 id 명칭은 infiniteContainer, listBoxOuter, listBox 가 필요하다.
  13. * <div id="layer_goods_qna" data-id="" > 레어팝업
  14. * <div class="modal-body" id="goodsDealQna" data-id="layer_infinite_item" >
  15. * <section id="infiniteContainer">
  16. * <div id="listBoxOuter">
  17. * <ul id="listBox">
  18. * </ul>
  19. * </div>
  20. * </section>
  21. * </div>
  22. * </div>
  23. *
  24. * // JQUERY History 파일과 인피니트 스크롤 관련 자바스크립트 파일을 import 되어 있어야 한다.
  25. * <script type="text/javascript" src="/ux/plugins/jquery.history.min.js"></script>
  26. * <script type="text/javascript" src="/ux/plugins/gaga.infinite.scrollLyer.js"></script>
  27. *
  28. * <script type="text/javascript">
  29. * // 인피니트 스크롤에 대한 History 정보를 가져오기 위해 함수를 호출한다.
  30. * // History 정보가 없을 경우 fnGetInfiniteScrollDataList 함수가 호출된다.
  31. * $(function() { gagaInfiniteScroll.getHistory(); });
  32. *
  33. * // 인피니트 스크롤 이벤트 발생 시 데이터 가져오기
  34. * var fnGetInfiniteScrollDataList = function(pageNum) {
  35. * // 콜백함수인 gagaInfiniteScroll.jsonToHtml 에서는 fnDrawInfiniteScrollData 함수를 호출한다.
  36. * gagajf.ajaxSubmitForm(document.searchForm, "/goods/list", "json", gagaInfiniteScroll.jsonToHtml);
  37. * }
  38. *
  39. * // 인피니트 스크롤 이벤트 발생 시 가져온 데이터를 특정 태그에 append
  40. * // fnDrawInfiniteScrollData(result, pageNum) 함수 구현
  41. * // 이 함수 내에서의 로직은
  42. * // 1. 가져온 데이터로 HTML 태그를 만든다.
  43. * // 2. 만든 태그를 append 하는 gagaInfiniteScroll.draw() 함수를 호출
  44. * </script>
  45. */
  46. var gagaInfiniteScroll = {
  47. pageStatus : {
  48. pageNum : [] // [0,1,2...] 로드된 페이지 (Array)
  49. , loadPage : 0 // 로드할 페이지
  50. , loadAlign : 'not' // 로드 상태(prev, next, not)
  51. , historyScroll : 0 //
  52. , nowPage : null // 현재 페이지
  53. , pageUrl : { // page url
  54. }
  55. },
  56. obj : {
  57. $ajaxBoxOuter : $('#listBoxOuter')
  58. , $ajaxBox : $('#listBox')
  59. , $window : $(window)
  60. , $loadingBar : '<div style="width: 100%; padding-top: 50px; text-align: center;" id="loadingBar"><img src="/ux/plugins/gaga/loader.gif" style="width:25px; height:25px;"/></div>'
  61. // , $loadingBar : '<div style="width: 100%; padding-top: 50px; text-align: center;" id="loadingBar"><img src="/images/hsmob/common/loading_bar_01.gif" width="24px" height=" 22px"/></div>'
  62. },
  63. scrollCheck : function() {
  64. var obj = this.obj, stat = this.pageStatus;
  65. if ($('[data-id="layer_infinite"]').scrollTop() + $('[data-id="layer_infinite"]').height() >= $('[data-id="layer_infinite_item"]').height() ) { // 더보기
  66. if (stat.loadAlign == 'not') {
  67. var pageLen = this.pageStatus.pageNum.length - 1;
  68. var nextPageNum = this.pageStatus.pageNum[pageLen] + 1;
  69. this.pageStatus.pageNum.push(nextPageNum);
  70. this.pageStatus.loadPage = nextPageNum;
  71. stat.loadAlign = 'next';
  72. this.getDataList();
  73. };
  74. }
  75. },
  76. getDataList : function() {
  77. /*if (this.pageStatus.loadAlign == 'prev') {
  78. this.obj.$ajaxBoxOuter.prepend($(this.obj.$loadingBar));
  79. } else if (this.pageStatus.loadAlign == 'next') {
  80. this.obj.$ajaxBoxOuter.append($(this.obj.$loadingBar));
  81. };*/
  82. $(".wrap-loading").removeClass('display-none');
  83. // 인피니트 스크롤 이벤트에서 호출될 함수(데이터 가져오기 등)
  84. // fnGetInfiniteScrollDataList 함수는 개발단에서 구현해야 한다.
  85. fnGetInfiniteScrollDataList(this.pageStatus.loadPage);
  86. },
  87. getHistory : function() {
  88. var historyData = History.getState();
  89. if (historyData.data.pageNum === undefined || historyData.data.pageNum === '' || historyData.data.htm === undefined || historyData.data.htm == '') {
  90. this.pageStatus.loadPage = 0;
  91. this.pageStatus.pageNum[0] = 0;
  92. this.pageStatus.loadAlign = 'next';
  93. this.pageStatus.historyScroll = 0;
  94. this.getDataList();
  95. } else {
  96. this.pageStatus.loadPage = historyData.data.pageNum;
  97. this.pageStatus.pageNum[0] = historyData.data.pageNum;
  98. this.pageStatus.historyScroll = historyData.data.dataIndex;
  99. this.pageStatus.loadAlign = 'next';
  100. gagaInfiniteScroll.draw(historyData.data.htm);
  101. };
  102. // $(window).on('scroll', function() {
  103. // gagaInfiniteScroll.scrollCheck();
  104. // });
  105. $('[data-id="layer_infinite"]').on('scroll', function() {
  106. gagaInfiniteScroll.scrollCheck();
  107. });
  108. },
  109. draw : function(htm, scrollTop, containerHeight) {
  110. if (htm == 'not') {
  111. $(".wrap-loading").addClass('display-none');
  112. this.pageStatus.loadAlign = 'top';
  113. } else {
  114. var startH = $('#infiniteContainer').height();
  115. var $addHtm = $(htm);
  116. var $imgs = $addHtm.find('img');
  117. var loadCheck = 0;
  118. var len = $imgs.length;
  119. if (scrollTop && containerHeight) {
  120. } else {
  121. }
  122. $addHtm.find('img.lazy').lazyload({root: null,
  123. rootMargin: "100px",
  124. threshold: 0});
  125. $.each($imgs, function(index) {
  126. $imgs.eq(index).on('load',function() {
  127. loadCheck++;
  128. imgLpadComp();
  129. });
  130. });
  131. var imgLpadComp = function() {
  132. if (len - 1 == loadCheck) {
  133. gagaInfiniteScroll.pushHistory(
  134. gagaInfiniteScroll.pageStatus.nowPage
  135. , $(window).scrollTop()
  136. , $('#infiniteContainer').height()
  137. , gagaInfiniteScroll.pageStatus.loadPage
  138. , 0
  139. , gagaInfiniteScroll.obj.$ajaxBox.html());
  140. if (scrollTop && containerHeight) {
  141. gagaInfiniteScroll.obj.$window.scrollTop.scrollTop(0).scrollTop(scrollTop);
  142. } else {
  143. gagaInfiniteScroll.obj.$window.scrollTop(gagaInfiniteScroll.obj.$window.scrollTop() + 1);
  144. }
  145. };
  146. };
  147. $(".wrap-loading").addClass('display-none');
  148. // Append HTML
  149. this.obj.$ajaxBox.append($addHtm);
  150. if (scrollTop && containerHeight) {
  151. gagaInfiniteScroll.obj.$window.scrollTop.scrollTop(0).scrollTop(scrollTop);
  152. } else {
  153. $(window).scrollTop($(window).scrollTop() + 1);
  154. }
  155. this.pageStatus.loadAlign = 'not';
  156. }
  157. },
  158. pushHistory : function(page, sScrollTop, sHeight, sPageNum, sDataIndex, sHtml) {
  159. History.replaceState({
  160. state: page
  161. , scroll: sScrollTop
  162. , height: sHeight
  163. , pageUrl: gagaInfiniteScroll.pageStatus.pageUrl
  164. , name: gagaInfiniteScroll.pageStatus.nowPage
  165. , pageNum : sPageNum
  166. , dataIndex : sDataIndex
  167. , htm : sHtml
  168. }, "", "");
  169. },
  170. pushLinkHistory : function(pageNum, dataIndex, obj) { // 링크에서 사용
  171. var $pageNum = (pageNum || pageNum == 0) ? pageNum : '';
  172. var $dataIndex = (dataIndex || dataIndex == 0) ? dataIndex : '';
  173. var $html = (obj) ? $(obj).parents('#listBox').html() : '';
  174. History.replaceState({
  175. state : gagaInfiniteScroll.pageStatus.nowPage
  176. , scroll : $(window).scrollTop()
  177. , height : $('#infiniteContainer').height()
  178. , pageUrl : gagaInfiniteScroll.pageStatus.pageUrl
  179. , name : gagaInfiniteScroll.pageStatus.nowPage
  180. , pageNum : $pageNum
  181. , dataIndex : $dataIndex
  182. , htm : $html
  183. }, "", "");
  184. return false;
  185. },
  186. jsonToHtml : function(result) {
  187. // Data 가져온 후 실행될 함수
  188. // fnDrawInfiniteScrollData 함수는 개발단에서 구현해야 한다.
  189. if (result.dataList == "not") // data가 더 이상 없으면
  190. fnDrawInfiniteScrollData('not', result.CURRENT_PAGE - 1);
  191. else
  192. fnDrawInfiniteScrollData(result, result.CURRENT_PAGE - 1);
  193. }
  194. };