searchPage.js 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. searchPage = function () {
  2. //private menbers
  3. const ordinaryUrl_left = "./json/ordinaryData_left.json";
  4. const ordinaryUrl_right = "./json/ordinaryData_right.json";
  5. let ordinaryData_left;
  6. let ordinaryData_right;
  7. let ordinaryData = [];
  8. let currentTarget = "26"; // 目前的素人
  9. let scaleNum = 1;
  10. //private methods
  11. function init() {
  12. console.log('searchPage is loaded.');
  13. // safariHacks();
  14. getOrdinaryData()
  15. }
  16. function resizePage() {
  17. let introMainH = 667;
  18. // let headerH = $(".header").height();
  19. let contentH = $(window).height();
  20. if (contentH < introMainH) {
  21. scaleNum = contentH / introMainH;
  22. $(".search .search__kv__slides").css("transform", "scale(" + scaleNum + ")");
  23. $(".search .search__kv__dishes").css("transform", "scale(" + scaleNum + ")");
  24. console.log(scaleNum)
  25. }else {
  26. scaleNum = 1;
  27. }
  28. }
  29. function safariHacks() {
  30. let windowsVH = window.innerHeight / 100;
  31. document.querySelector('.search__kv').style.setProperty('--vh', windowsVH + 'px');
  32. window.addEventListener('resize', function () {
  33. document.querySelector('.search__kv').style.setProperty('--vh', windowsVH + 'px');
  34. });
  35. }
  36. function getOrdinaryData(){
  37. fetch(ordinaryUrl_left)
  38. .then(res => res.json())
  39. .then(data => {
  40. ordinaryData_left = data.map(item => (
  41. {
  42. index: item.index,
  43. id: item.id,
  44. name: item.name,
  45. picture: item.picture,
  46. text: item.text
  47. }
  48. ))
  49. .sort((a,b)=>{
  50. return -1
  51. });
  52. ordinaryData = ordinaryData.concat(ordinaryData_left)
  53. // console.log(ordinaryData)
  54. })
  55. .then(() => {
  56. fetch(ordinaryUrl_right)
  57. .then(res => res.json())
  58. .then(data => {
  59. ordinaryData_right = data.map(item => (
  60. {
  61. index: item.index,
  62. id: item.id,
  63. name: item.name,
  64. picture: item.picture,
  65. text: item.text
  66. }
  67. ))
  68. .sort((a, b) => {
  69. return a.index - b.index
  70. });
  71. ordinaryData = ordinaryData.concat(ordinaryData_right)
  72. console.log(ordinaryData)
  73. })
  74. .then(()=>{
  75. createOrdinaryHtml();
  76. })
  77. .then(() => {
  78. setTimeout(() => {
  79. initHandler();
  80. }, 400);
  81. })
  82. });
  83. }
  84. async function initHandler(){
  85. // const resizePageBuild = await resizePage();
  86. const inToCenterBuild = await inToCenter();
  87. const detectKolBuild = await detectKol();
  88. const detectAniBuild = await detectAni();
  89. const bgParallaxBuild = await bgParallax();
  90. const intoPageBuild = await intoPage();
  91. }
  92. function intoPage() {
  93. // console.log(".intoPage")
  94. gsap.to(".search", { duration: 0.3, autoAlpha: 1 })
  95. }
  96. function createOrdinaryHtml(){
  97. // 素人頭像html
  98. for (let i = 0; i < ordinaryData.length;i++) {
  99. $(".search__kv__slides").append(`
  100. <div class="search__kv__slide" data-ordinary="${ordinaryData[i].id}">
  101. <div class="search__kv__light"></div>
  102. <a class="search__kv__ordinary" href="./recipe_ordinary.html?ordinary=${ordinaryData[i].id}">
  103. <img src="./images/searchPage/kv-ordinary/${ordinaryData[i].picture}" alt="">
  104. </a>
  105. <a href="./recipe_ordinary.html?ordinary=${ordinaryData[i].id}" class="search__kv__basket">
  106. <img src="./images/searchPage/kv-basket.png" alt="">
  107. </a>
  108. </div>
  109. `);
  110. // 菜色名稱html
  111. $(".search__kv__dishes").append(`
  112. <a href="./recipe_ordinary.html?ordinary=${ordinaryData[i].id}" class="search__kv__dish" data-dish="${ordinaryData[i].id}"><span>民間料理救星 - ${ordinaryData[i].name}</span>${ordinaryData[i].text}</a>
  113. `);
  114. }
  115. }
  116. // 找中間素人
  117. function inToCenter() {
  118. let target = $(".search__kv__slide[data-ordinary='" + currentTarget+"']")
  119. let targetW = target.width();
  120. let targetLeft = target.position().left
  121. let windowW = $(window).width();
  122. let space;
  123. if (targetW < windowW) {
  124. space = (windowW - targetW) / 2;
  125. // console.log(space)
  126. $(".search__kv").scrollLeft(targetLeft - space);
  127. // console.log(targetLeft - space)
  128. // console.log("targetLeft: " + targetLeft)
  129. } else {
  130. space = (targetW - windowW) / 2;
  131. // console.log(space)
  132. $(".search__kv").scrollLeft(targetLeft + space);
  133. // console.log(targetLeft + space)
  134. }
  135. }
  136. // 偵測中間KOL
  137. function detectKol() {
  138. $(".search__kv").scroll(function () {
  139. let containerLeft = $(".search__kv").scrollLeft();
  140. let containerCenterStart = containerLeft + ($(window).width() / 2);
  141. // console.log("containerLeft: " + containerLeft)
  142. $(".search__kv__slide").each(function () {
  143. let dataOrdinary = $(this).data('ordinary');
  144. let target = $(this);
  145. let targetW = target.width();
  146. let targetLeft = target.position().left
  147. // console.log(dataOrdinary + "-targetLeft: " + targetLeft);
  148. if (containerCenterStart > targetLeft && containerCenterStart < (targetLeft + targetW)) {
  149. currentTarget = dataOrdinary;
  150. // console.log(currentTarget + "-最小: " + targetLeft + ", 最大: " + targetLeft + targetW)
  151. // console.log(currentTarget)
  152. }
  153. });
  154. });
  155. }
  156. // 定位動畫
  157. function detectAni() {
  158. let aniKol = "";
  159. let dishTl = gsap.timeline();
  160. $(".search__kv").on('scroll', function () {
  161. // console.log("aniKol: "+aniKol)
  162. // console.log("currentTarget: " + currentTarget)
  163. if (aniKol != currentTarget) {
  164. // 上帝光動畫
  165. let $light = ".search__kv__slide[data-ordinary='" + currentTarget + "'] .search__kv__light"
  166. let $elseLight = $(".search__kv__slide").not(".search__kv__slide[data-ordinary='" + currentTarget + "']").find(".search__kv__light")
  167. gsap.to(".search__kv__light", { duration: 0.2, autoAlpha: 0 })
  168. gsap.to($light, { duration: 0.2, autoAlpha: 0.9, delay: 0.2 })
  169. gsap.to($elseLight, { duration: 0.2, autoAlpha: 0, delay: 0.2 })
  170. if (dishTl) {
  171. // 菜名動畫摧毀
  172. dishTl.kill();
  173. }
  174. // 菜名動畫
  175. let $allDish = ".search__kv__dish"
  176. let $dish = ".search__kv__dish[data-dish='" + currentTarget + "']"
  177. let $elseDish = $(".search__kv__dish").not(".search__kv__dish[data-dish='" + currentTarget + "']")
  178. dishTl = gsap.timeline();
  179. dishTl.to($allDish, { duration: 0, autoAlpha: 0, scale: 1, ease: "none" })
  180. dishTl.to($dish, { duration: 0.5, autoAlpha: 1, scale: 1, ease: "none" });
  181. dishTl.to($elseDish, { duration: 0, autoAlpha: 0, scale: 1, ease: "none" })
  182. // dishTl.fromTo($dish, { filter: "brightness(1)" }, { duration: 0.6, filter: "brightness(1.3)", ease: "none", yoyo: true, repeat: -1, delay: 0.5 });
  183. aniKol = currentTarget;
  184. console.log("aniKol: " + aniKol)
  185. }
  186. // console.log('scrollEnd')
  187. });
  188. }
  189. // 背景視差
  190. function bgParallax() {
  191. let defautCenter = 49.1
  192. let offset = (defautCenter - (defautCenter * scaleNum)) * 0.1
  193. let currentCenter = defautCenter + offset
  194. let containerLeft = $(".search__kv").scrollLeft();
  195. let slidesWCenter = ($(".search__kv__slides").width() / 2)
  196. let scaleOffset = containerLeft - slidesWCenter
  197. $(".search__kv__bg__img").css('background-position', currentCenter + '% top');
  198. // console.log(defautCenter)
  199. // console.log(scaleNum)
  200. // console.log(containerLeft)
  201. // console.log(slidesWCenter)
  202. // console.log(containerLeft - slidesWCenter)
  203. setTimeout(() => {
  204. $(".search__kv").on("scroll",function () {
  205. containerLeft = $(".search__kv").scrollLeft();
  206. slidesWCenter = ($(".search__kv__slides").width() / 2) + scaleOffset
  207. let moveLeft = (currentCenter + ((containerLeft - slidesWCenter) / 200))
  208. // moveLeft = moveLeft.toFixed(0)
  209. // console.log(currentCenter)
  210. // console.log(containerLeft)
  211. // console.log(slidesWCenter)
  212. // console.log(containerLeft - slidesWCenter)
  213. // console.log(moveLeft)
  214. $(".search__kv__bg__img").css('background-position', moveLeft + '% top');
  215. // console.log(".width scroll")
  216. });
  217. }, 500);
  218. }
  219. {
  220. $(document).ready(function () {
  221. init();
  222. });
  223. }
  224. //public
  225. return {
  226. intoPage: function(){
  227. intoPage();
  228. },
  229. };
  230. };
  231. var searchPage = new searchPage();