recipePage.js 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. import getParams from './getParams.js';
  2. recipe = function () {
  3. //private menbers
  4. let audioDom;
  5. let player
  6. const dataUrl = "./json/kolData.json";
  7. const kolMan = ['Yang', 'Hao', 'Drangadrang', 'Cookingdairy', 'Ku', 'Betty', 'Ralf', 'Chefchouchou', 'Albee', 'commute_Chang', 'commute_Li', 'commute_He', 'ordinary']
  8. let kolName;
  9. let allData;
  10. let recipeData;
  11. let isMoreScroll = false;
  12. let isIngradiebtsScroll = false;
  13. //private methods
  14. function init() {
  15. console.log('recipe is loaded.');
  16. // 取得是哪位Kol
  17. if (getParams('kol') != null && kolMan.includes(getParams('kol'))){
  18. kolName = getParams('kol')
  19. }else {
  20. kolName = 'Ralf' // 預設kol
  21. }
  22. console.log(kolName)
  23. fetch(dataUrl)
  24. .then(res => res.json())
  25. .then(data => {
  26. allData = data;
  27. let getRecipe = data.filter(item => item.kol.id == kolName
  28. );
  29. recipeData = getRecipe[0];
  30. // console.log(recipeData)
  31. })
  32. .then(()=>{
  33. handler();
  34. });
  35. }
  36. async function handler(){
  37. const handleraudio = await audioStyle(); //初始化播放器
  38. const handlerHtml = await createHtml();
  39. // const handlerTouch = await detectTouch();
  40. const handlerInto = await intoPage();
  41. }
  42. function audioStyle(){
  43. audioDom = $("#audioPlayer")[0]
  44. player = new Plyr('#audioPlayer',{
  45. controls: [
  46. // 'restart',
  47. 'play',
  48. 'progress',
  49. 'current-time',
  50. 'duration',
  51. // 'mute',
  52. // 'volume',
  53. ],
  54. });
  55. setTimeout(function(){$(".recipe__teaching audio").css("opacity",1)},200)
  56. $(".plyr__progress__container").css("pointer-events","none");
  57. player.on('play', (event) => {
  58. $(".plyr__progress__container").css("pointer-events","auto");
  59. });
  60. }
  61. // 把生成的HTML顯示到介面上
  62. function createHtml() {
  63. // kv DOM
  64. $(".recipe__kv__dish").append(`
  65. <img src="https://d1xzlli46wohoc.cloudfront.net/images/recipePage/${recipeData.kv_img}" alt="">
  66. `)
  67. //食材保文
  68. if(kolName == 'Drangadrang') {
  69. $(".recipe__ingredients p").html(`
  70. *商品依各店庫存為主,如遇缺貨可自行更換商品或服務門市<br>*芒果為季節性商品,此料理可將芒果替換為香蕉、柳橙、鳳梨等水果或依喜好更換食材
  71. `)
  72. }
  73. if(kolName == 'commute_Chang') {
  74. $(".recipe__ingredients p").html(`
  75. *商品依各店庫存為主,如遇缺貨可自行更換商品或服務門市<br>*水果可依個人喜好添加
  76. `)
  77. }
  78. // 分享
  79. $(".recipe__btn-share").attr("href", "./share.html?kol=" + kolName)
  80. // let path = "./photograph.html?kol=" + kolName + "&openExternalBrowser=1"
  81. // console.log(path)
  82. $(".recipe__btn-filter").click(function(e){
  83. e.preventDefault();
  84. let path = "./photograph.html?kol=" + kolName + "&openExternalBrowser=1"
  85. location.href = path
  86. console.log(path)
  87. });
  88. //瀏覽其他名菜圖
  89. let elseData = allData.filter(item=>item.kol.id != kolName);
  90. // console.log(elseData)
  91. for(let i=0;i<elseData.length;i++){
  92. $(".recipe__container").append(`
  93. <a href="./recipe.html?kol=${elseData[i].kol.id}" class="recipe__else">
  94. <img class="recipe__else__dish" src="https://d1xzlli46wohoc.cloudfront.net/images/recipePage/${elseData[i].kv_img_s}" alt="">
  95. <img class="recipe__else__name" src="https://d1xzlli46wohoc.cloudfront.net/images/recipePage/${elseData[i].kv_img_s_name}" alt="">
  96. </a>
  97. `)
  98. }
  99. //料理標籤
  100. let tagData = recipeData.tag.split(',');
  101. // console.log(tagData)
  102. for(let i=0;i<tagData.length;i++) {
  103. $(".recipe__tag__list").append(`
  104. <a href="./search.html?search=${tagData[i]}" class="recipe__tag__item">${tagData[i]}</a>
  105. `)
  106. }
  107. let ctaUrl = getOrderURL(recipeData);
  108. $(".recipe__buy").attr("href",ctaUrl);
  109. $(".recipe__buy").click(function(e){
  110. e.preventDefault();
  111. player.pause();
  112. setTimeout(function(){
  113. location.href = ctaUrl
  114. },200)
  115. })
  116. }
  117. // 偵測上滑下滑
  118. function detectTouch(){
  119. var startX, startY, moveX, moveY;
  120. //here clientX, and clientY means X and Y coordinates
  121. function touchStart(e) {
  122. startX = e.touches[0].clientX;
  123. startY = e.touches[0].clientY;
  124. }
  125. function touchMove(e) {
  126. moveX = e.touches[0].clientX;
  127. moveY = e.touches[0].clientY;
  128. }
  129. function touchEnd() {
  130. if (startX + 100 < moveX) {
  131. console.log('right');
  132. } else if (startX - 100 > moveX) {
  133. console.log('left');
  134. }
  135. if (startY < moveY) {
  136. console.log('down');
  137. // $(".recipe__buy").css('bottom', '-29px')
  138. } else if (startY > moveY) {
  139. console.log('up');
  140. // $(".recipe__buy").css('bottom', '-49px')
  141. }
  142. }
  143. document.addEventListener('touchstart', touchStart,false);
  144. document.addEventListener('touchmove', touchMove, false);
  145. document.addEventListener('touchend', touchEnd, false);
  146. }
  147. // 偵測播放
  148. function detectPlay(){
  149. let headerH = $(".header").height();
  150. let teachingTop = $(".recipe__teaching").offset().top
  151. let isClick = false;
  152. $(".recipeWrapper .main").click(function(e){
  153. // console.log(e.target.className)
  154. if(!isClick) {
  155. let recipe__teachingT = $(".recipe__teaching").offset().top
  156. let windowScrollTop = $(window).scrollTop();
  157. // console.log("recipe__teachingT: "+recipe__teachingT)
  158. // console.log("windowScrollTop: "+windowScrollTop)
  159. if (e.target.className == 'recipe__buy__img' || e.target.className == 'recipe__btn-filter__img') {
  160. return false;
  161. }
  162. if(windowScrollTop>recipe__teachingT) {
  163. player.play();
  164. }else {
  165. player.play();
  166. $("html,body").animate({
  167. scrollTop: teachingTop - headerH
  168. })
  169. }
  170. $(".recipe__outer-ingredients").animate({
  171. scrollLeft: 0
  172. })
  173. isIngradiebtsScroll = true
  174. isClick = true;
  175. }
  176. });
  177. }
  178. // 一鍵下單動畫
  179. function buyCtaAni(){
  180. let buyCtaTl = gsap.timeline({ repeat: -1, repeatDelay: 3, delay: 1.5});
  181. buyCtaTl.to(".recipe__buy", {duration: 0.2,scale: 1, y:0, ease: "none" })
  182. buyCtaTl.to(".recipe__buy", { duration: 0.2,scale: 1.1, y:-10, ease: "none"})
  183. buyCtaTl.to(".recipe__buy", {duration: 0.2,scale: 1, y:0, ease: "none" })
  184. buyCtaTl.to(".recipe__buy", { duration: 0.2,scale: 1.1, y:-10, ease: "none" })
  185. buyCtaTl.to(".recipe__buy", {duration: 0.3,scale: 1, y:0, ease: "none" })
  186. }
  187. // 瀏覽更多名菜滑動
  188. function scrollAni(){
  189. let windowMoreH = $(window).height() / 2;
  190. let windowIngradientsH = $(window).height() / 2;
  191. let recipe__ingradientsT = $(".recipe__ingredients").offset().top
  192. let recipe__moreT = $(".recipe__more").offset().top
  193. let ingradientsH = recipe__ingradientsT - windowIngradientsH
  194. let moreH = recipe__moreT - windowMoreH
  195. let ingradientsOver = false;
  196. if($(".recipe__basket").width()>($(window).width() - 20)){
  197. $(".recipe__outer-ingredients").scrollLeft(200);
  198. ingradientsOver = true;
  199. }
  200. $(".recipe__container").scrollLeft(200);
  201. $(window).scroll(function(){
  202. if (ingradientsOver &&!isIngradiebtsScroll) {
  203. if ($(window).scrollTop() > ingradientsH) {
  204. $(".recipe__outer-ingredients").animate({
  205. scrollLeft: 0
  206. })
  207. isIngradiebtsScroll = true
  208. // console.log("isScroll")
  209. }
  210. }
  211. if (!isMoreScroll) {
  212. if ($(window).scrollTop() > moreH) {
  213. $(".recipe__container").animate({
  214. scrollLeft: 0
  215. })
  216. isMoreScroll = true
  217. // console.log("isScroll")
  218. }
  219. }
  220. // console.log($(window).scrollTop() > intoH)
  221. })
  222. }
  223. function getOrderURL(_kolData) {
  224. var _ingredientData = _kolData.ingredient;
  225. var resuleData = [];
  226. _ingredientData.forEach(element => {
  227. // console.log(element);
  228. var _result = { "salepage_id": "", "sku_id": "", "qty": 1 };
  229. _result.salepage_id = element.salepage_id;
  230. _result.sku_id = element.sku_id;
  231. if(kolName == 'Chefchouchou' && element.salepage_id =="1587984") {
  232. _result.qty = 2
  233. }
  234. if(_result.sku_id !== null){
  235. resuleData.push(_result);
  236. }
  237. });
  238. console.log(resuleData);
  239. var _utm = _kolData.kol.UTM;
  240. var _url = 'http://shop.pxmart.com.tw/v2/ShoppingCart/BatchInsert?openExternalBrowser=1&data=' + encodeURIComponent(JSON.stringify(resuleData)) + _utm;
  241. console.log(_url);
  242. return _url;
  243. }
  244. function intoPage(){
  245. gsap.set(".recipeWrapper main",{y: 200})
  246. gsap.to(".recipeWrapper main",{y: 0,onComplete: function(){
  247. $(".recipeWrapper main").css("transform","none")
  248. buyCtaAni();
  249. scrollAni();
  250. detectPlay();
  251. }})
  252. }
  253. {
  254. $(document).ready(function () {
  255. init();
  256. });
  257. }
  258. //public
  259. return {
  260. intoPage: function(){
  261. intoPage();
  262. }
  263. };
  264. };
  265. var recipe = new recipe();
  266. const swiper = new Swiper('.recipe__swiper .swiper', {
  267. // Optional parameters
  268. draggable: true,
  269. // loop: true,
  270. // If we need pagination
  271. pagination: {
  272. el: '.swiper-pagination',
  273. clickable: true
  274. }
  275. });