123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. import getParams from './getParams.js';
  2. index = function () {
  3. //private menbers
  4. let kolName; // 網址參數的kol
  5. let currentKol; //目前sroll到的kol
  6. let intro__tipTl;
  7. const kolMan = ['Yang', 'Hao', 'Drangadrang', 'Cookingdairy', 'Ku', 'Betty', 'Ralf', 'Chefchouchou', 'Albee', 'commute_Chang', 'commute_Li', 'commute_He','ordinary']
  8. const audio = document.querySelector('audio');
  9. //private methods
  10. function init() {
  11. console.log('index is loaded.');
  12. // 取得是哪位Kol
  13. if (getParams('kol') != null && kolMan.includes(getParams('kol'))) {
  14. kolName = getParams('kol')
  15. } else {
  16. kolName = 'ordinary' //預設kol
  17. }
  18. console.log("參數kol: "+kolName)
  19. if (getParams('share_id') != null || getParams('shareback') != null) {
  20. introLeavePage();
  21. $("#promoModalCenter").modal('show');
  22. console.log("share_id: "+getParams('share_id'))
  23. console.log("shareback: "+getParams('shareback'))
  24. }
  25. if (getParams('backIndexPage') != null) {
  26. introLeavePage();
  27. console.log("backIndexPage: "+getParams('backIndexPage'))
  28. }
  29. // 來自 BF Cache,若是則重新刷新頁面
  30. window.onpageshow = function(event) {
  31. if (event.persisted) {
  32. window.location.reload()
  33. console.log("reload")
  34. }
  35. };
  36. // 暫時
  37. // introLeavePage();
  38. createHtml();
  39. handler();
  40. }
  41. async function handler(){
  42. const intoPageHandler = await intoPage();
  43. const resizePageHandler = await resizePage();
  44. const aniIntro00Handler = await aniIntro00();
  45. }
  46. function resizePage(){
  47. let introMainH = 800;
  48. // let headerH = $(".header").height();
  49. let contentH = $(window).height();
  50. if (contentH < introMainH) {
  51. let scaleNum = contentH / introMainH;
  52. $(".indexWrapper .intro__container").css("transform", "scale(" + scaleNum+")");
  53. $(".intro__overlay").css("transform","scale(2)");
  54. // console.log(scaleNum)
  55. }
  56. }
  57. function aniIntro00(){
  58. gsap.set(".hamburger",{autoAlpha:0});
  59. gsap.set(".logo", { autoAlpha: 0 });
  60. // gsap.fromTo(".intro__title", { scale: 0.7 }, { duration: 1.5, autoAlpha: 1, scale: 1, ease: "elastic.out(1, 0.5)" });
  61. gsap.to(".intro__tip", { duration: 0.5, autoAlpha: 1, ease: "none", delay: 0.3});
  62. intro__tipTl = gsap.timeline({ repeat: -1, repeatDelay: 1, yoyo: true, delay: 1.5 });
  63. intro__tipTl.to(".intro__tip", { duration: 0.5, autoAlpha: 0.5, ease: "none" })
  64. intro__tipTl.to(".intro__tip", { duration: 0.5, autoAlpha: 1, ease: "none"})
  65. setTimeout(() => {
  66. aniIntro01();
  67. }, 1000);
  68. }
  69. function aniIntro01(){
  70. let isAniIntro01 = false;
  71. let isLeaveIntro = false;
  72. let isSkipShow = false;
  73. $(".intro").click(function(){
  74. if (!isAniIntro01) {
  75. $(".intro__overlay").css("z-index","0");
  76. $(".intro__tip").css("opacity","0").css("visibikity","hidden");
  77. $(".intro__protect").css("color","#666666");
  78. intro__tipTl.kill();
  79. // gsap.to(".intro__tip", { duration: 0, autoAlpha: 0, ease: "none", delay: 0.3 });
  80. gsap.to(".intro__logo", { duration: 0.5, autoAlpha: 1 });
  81. gsap.to(".intro__title", { duration: 0.7, autoAlpha: 0, scale: 0.7, ease: "elastic.in(1, 0.6)" });
  82. gsap.to(".intro__text img", { duration: 0.5, y: 0, autoAlpha: 1, ease: "none", stagger: 0.3, delay: 3 });
  83. gsap.to(".intro__skip", { duration: 0.7, autoAlpha: 1, delay: 5 });
  84. $(".intro__volume").addClass('active');
  85. audio.play();
  86. audio.addEventListener("timeupdate",function(){
  87. // console.log(audio.currentTime)
  88. // if(audio.currentTime>6.38) {
  89. // if(!isSkipShow) {
  90. // gsap.to(".intro__skip", { duration: 0.7, autoAlpha: 1 });
  91. // isSkipShow = true;
  92. // }
  93. // }
  94. if(audio.currentTime>15.34) {
  95. if(!isLeaveIntro) {
  96. introLeavePage();
  97. isLeaveIntro = true
  98. }
  99. }
  100. })
  101. $(".intro__skip").click(function(e){
  102. e.preventDefault();
  103. introLeavePage();
  104. isLeaveIntro = true
  105. });
  106. // audio.addEventListener('ended',function(){
  107. // introLeavePage();
  108. // },false)
  109. isAniIntro01 = true;
  110. $(".intro__volume").click(function () {
  111. if ($(this).hasClass('active')) {
  112. $(this).removeClass('active');
  113. audio.muted = true;
  114. } else {
  115. $(this).addClass('active');
  116. audio.muted = false;
  117. }
  118. });
  119. }
  120. })
  121. }
  122. function intoPage(){
  123. gsap.to(".indexWrapper",{duration:1,autoAlpha: 1})
  124. }
  125. function introLeavePage(){
  126. console.log("introLeavePage");
  127. $('.index').css('display', 'block');
  128. gsap.to(".hamburger", { duration: 0.5, autoAlpha: 1 })
  129. gsap.to(".logo", { duration: 0.5, autoAlpha: 1 })
  130. // audio.pause();
  131. // audio.currentTime = 0;
  132. gsap.to('.intro', { duration: 0.5, autoAlpha: 0,
  133. onComplete: function () {
  134. $(".indexWrapper .bg").fadeOut();
  135. $('.intro').css('display', 'none');
  136. gsap.to(".index", { duration: 0.7, autoAlpha: 1 })
  137. gsap.fromTo(".index__container", { scale: 1.5 }, { duration: 0.7, autoAlpha: 1, scale: 1 })
  138. gsap.fromTo(".index__title", { autoAlpha: 0, scale: 0.3, }, { duration: 0.8, scale: 1, autoAlpha: 1, ease: "back.inOut(2)"})
  139. // 找主婦
  140. inToCenter()
  141. setTimeout(() => {
  142. detectKol();
  143. inToIndexAni();
  144. bgParallax();
  145. multipleLink();
  146. indexLeavePage();
  147. }, 800);
  148. setTimeout(() => {
  149. if(kolName != 'ordinary'){
  150. goToKol();
  151. }
  152. detectAni();
  153. }, 1500);
  154. }
  155. })
  156. }
  157. function createHtml(){
  158. // intro頁 kol人物
  159. // $(".intro__kol img").attr("src", "https://d1xzlli46wohoc.cloudfront.net/images/indexPage/kol-" + kolName +".png")
  160. // $(".intro__skip").attr("href","./recipe.html?kol="+kolName)
  161. // index頁 kol菜名
  162. for(let i=0;i<kolMan.length;i++) {
  163. $(".index__dishes").append(`
  164. <a href="./recipe.html?kol=${kolMan[i]}" class="index__dish" data-dish="${kolMan[i]}">
  165. <img src="https://d1xzlli46wohoc.cloudfront.net/images/indexPage/dishname-${kolMan[i]}.png" alt="">
  166. </a>
  167. `);
  168. if(kolMan[i] == "ordinary") {
  169. $(".index__dishes a[data-dish='ordinary']").removeAttr("href");
  170. }
  171. }
  172. }
  173. // async function indexHandler(){
  174. // const toKol = await inToKol();
  175. // const detectScroll = await detectKol();
  176. // }
  177. // 找主婦
  178. function inToCenter(){
  179. let target = $(".index__slide[data-kol='ordinary']")
  180. let kolW = target.width();
  181. let kolLeft = target.offset().left
  182. let windowW = $(window).width();
  183. let space;
  184. if (kolW < windowW) {
  185. space = (windowW - kolW) / 2;
  186. // console.log(space)
  187. $(".index__kv").scrollLeft(kolLeft - space);
  188. // console.log(kolLeft - space)
  189. } else {
  190. space = (kolW - windowW) / 2;
  191. // console.log(space)
  192. $(".index__kv").scrollLeft(kolLeft + space);
  193. // console.log(kolLeft + space)
  194. }
  195. }
  196. // index移動至指定kol
  197. function goToKol(){
  198. let target = $(".index__slide[data-kol='" + kolName + "']")
  199. if (kolName.includes('commute')) {
  200. target = $(".index__slide[data-kol='commute']")
  201. }
  202. if (kolName.includes('Hao')) {
  203. target = $(".index__slide[data-kol='HaoYang']")
  204. }
  205. if (kolName.includes('Yang')) {
  206. target = $(".index__slide[data-kol='HaoYang']")
  207. }
  208. console.log(target)
  209. let kolW = target.width();
  210. let kolLeft = target.position().left
  211. if (kolName.includes('commute') || kolName.includes('Hao') || kolName.includes('Yang')) {
  212. kolW = $(".index__slide-item[data-kol='" + kolName + "']").width();
  213. kolLeft = kolLeft + $(".index__slide-item[data-kol='" + kolName + "']").position().left
  214. }
  215. let windowW = $(window).width();
  216. let space;
  217. if (kolW < windowW) {
  218. space = (windowW - kolW)/2;
  219. // console.log(space)
  220. $(".index__kv").animate({
  221. scrollLeft: kolLeft - space
  222. });
  223. // console.log(kolLeft - space)
  224. }else {
  225. space = (kolW - windowW) / 2;
  226. // console.log(space)
  227. $(".index__kv").animate({
  228. scrollLeft: kolLeft + space
  229. })
  230. // console.log(kolLeft + space)
  231. }
  232. }
  233. // 偵測中間KOL
  234. function detectKol(){
  235. currentKol = kolName;
  236. $(".index__kv").scroll(function(){
  237. let containerLeft = $(".index__kv").scrollLeft();
  238. let containerCenterStart = containerLeft + ($(window).width() / 2);
  239. // console.log("containerLeft: " + containerLeft)
  240. $(".index__slide").each(function(){
  241. let dataKol = $(this).data('kol');
  242. let target = $(this);
  243. let targetW = target.width();
  244. let targetLeft = target.position().left
  245. // console.log(dataKol + "-targetLeft: " + targetLeft);
  246. if (containerCenterStart > targetLeft && containerCenterStart < (targetLeft + targetW)) {
  247. currentKol = dataKol;
  248. // console.log(currentKol + "-最小: " + targetLeft + ", 最大: " + targetLeft + targetW)
  249. if (currentKol == 'commute' || currentKol == 'HaoYang') {
  250. target.find(".index__slide-item").each(function () {
  251. let childDataKol = $(this).data('kol');
  252. let childtarget = $(this);
  253. let childtargetW = childtarget.width();
  254. let childtargetLeft = childtarget.position().left
  255. let childStart = targetLeft + childtargetLeft
  256. let childEnd = targetLeft + childtargetLeft + childtargetW
  257. // console.log(childDataKol+" - childStart: " + childStart + ", childEnd: " + childEnd)
  258. if (containerCenterStart > childStart && containerCenterStart < childEnd) {
  259. currentKol = childDataKol;
  260. // console.log(currentKol + "-最小: " + targetLeft + ", 最大: " + targetLeft + targetW)
  261. // console.log("currentKol: " + currentKol)
  262. }
  263. });
  264. }
  265. }
  266. });
  267. });
  268. }
  269. function inToIndexAni(){
  270. let $light = ".index__slide[data-kol='ordinary'] .index__light"
  271. gsap.to($light, { duration: 0.5, autoAlpha: 0.9 })
  272. let $allDish = ".index__dish"
  273. let $dish = ".index__dish[data-dish='ordinary']"
  274. gsap.to($allDish, { duration: 0, autoAlpha: 0, y: 20 })
  275. gsap.to($dish, { duration: 0.3, autoAlpha: 1, y: 0 });
  276. }
  277. // 定位動畫
  278. function detectAni(){
  279. let aniKol = "ordinary";
  280. let dishTl = gsap.timeline();
  281. $(".index__kv").on('scroll', function () {
  282. // console.log("aniKol: "+aniKol)
  283. // console.log("currentKol: " + currentKol)
  284. if (aniKol != currentKol) {
  285. // 上帝光動畫
  286. let $light = ".index__slide[data-kol='" + currentKol + "'] .index__light"
  287. let $elseLight = $(".index__slide").not(".index__slide[data-kol='" + currentKol + "']").find(".index__light")
  288. if (currentKol.includes('commute') || currentKol.includes('Hao') || currentKol.includes('Yang')) {
  289. $light = ".index__slide-item[data-kol='" + currentKol + "'] .index__light"
  290. $elseLight = $(".index__slide-item").not(".index__slide-item[data-kol='" + currentKol + "']").find(".index__light")
  291. }
  292. gsap.to(".index__light", { duration: 0.2, autoAlpha: 0 })
  293. gsap.to($light, { duration: 0.2, autoAlpha: 0.9, delay: 0.2 })
  294. gsap.to($elseLight, { duration: 0.2, autoAlpha: 0, delay: 0.2 })
  295. if (dishTl) {
  296. // 菜名動畫摧毀
  297. dishTl.kill();
  298. }
  299. // 菜名動畫
  300. let $allDish = ".index__dish"
  301. let $dish = ".index__dish[data-dish='" + currentKol + "']"
  302. let $elseDish = $(".index__dish").not(".index__dish[data-dish='" + currentKol + "']")
  303. dishTl = gsap.timeline();
  304. dishTl.to($allDish, { duration: 0.3, autoAlpha: 0, y: 20 })
  305. dishTl.to($dish, { duration: 0.5, autoAlpha: 1, y: 0 });
  306. dishTl.to($elseDish, { duration: 0.3, autoAlpha: 0, y: 20 })
  307. dishTl.fromTo($dish, { filter: "brightness(1)"}, { duration: 0.6, filter: "brightness(1.3)", ease: "none",yoyo: true, repeat: -1, delay: 0.5 });
  308. aniKol = currentKol;
  309. console.log("aniKol: "+aniKol)
  310. }111
  311. // console.log('scrollEnd')
  312. });
  313. }
  314. // 背景視差
  315. function bgParallax(){
  316. $(".index__kv").scroll(function(){
  317. let containerLeft = $(".index__kv").scrollLeft();
  318. let slidesWCenter = $(".index__slides").width() / 2
  319. let moveLeft = (34 - ((containerLeft - slidesWCenter)/80))
  320. // let containerTop = $(".index__kv").scrollTop();
  321. // let slidesHCenter = $(".index__slides").height() / 2
  322. // let moveTop = (50 + ((containerTop - slidesHCenter) / 10))
  323. $(".index__bg__img").css('background-position', moveLeft + '% top');
  324. // console.log(".index__kv scroll")
  325. });
  326. }
  327. // 多人組連結
  328. function multipleLink(){
  329. $(".index__kv").scroll(function(){
  330. if (currentKol == 'Yang') {
  331. $('.index__slide[data-kol="HaoYang"').find('a')
  332. .attr('href','./recipe.html?kol=Yang');
  333. }
  334. else if (currentKol == 'Hao') {
  335. $('.index__slide[data-kol="HaoYang"').find('a')
  336. .attr('href', './recipe.html?kol=Hao');
  337. }
  338. else if (currentKol == 'commute_Li') {
  339. $('.index__slide[data-kol="commute"').find('a')
  340. .attr('href', './recipe.html?kol=commute_Li');
  341. }
  342. else if (currentKol == 'commute_Chang') {
  343. $('.index__slide[data-kol="commute"').find('a')
  344. .attr('href', './recipe.html?kol=commute_Chang');
  345. }
  346. else if (currentKol == 'commute_He') {
  347. $('.index__slide[data-kol="commute"').find('a')
  348. .attr('href', './recipe.html?kol=commute_He');
  349. }
  350. });
  351. }
  352. function indexLeavePage(){
  353. $(".index__slide a,.index__dish").click(function(e){
  354. e.preventDefault();
  355. let targetHref = $(this).attr("href");
  356. // console.log(targetHref)
  357. if(targetHref != undefined) {
  358. gsap.to(".index__outer",{duration: 0.7, y:'-200px',ease: "expo.out"})
  359. gsap.to(".indexWrapper",{duration: 0.6, autoAlpha: 0, delay: 0.1,
  360. onComplete: function(){
  361. location.href = targetHref;
  362. }})
  363. }
  364. });
  365. }
  366. {
  367. $(document).ready(function () {
  368. init();
  369. });
  370. }
  371. //public
  372. return {
  373. };
  374. };
  375. var index = new index();