top_carousel.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <section class="w-100 position-relative top-carousel">
  2. <button class="text-center d-none d-lg-block close-btn">
  3. <i class="fas fa-chevron-up" style="transform: translateY(-2px)"></i>
  4. <p>隱藏</p>
  5. </button>
  6. <div class="container-fluid mx-auto" style="max-width: 1300px">
  7. <div class="slider-content"></div>
  8. <!-- 輪播進度條 -->
  9. <div class="bullet-list"></div>
  10. </div>
  11. </section>
  12. <script>
  13. $.ajax({
  14. method: "get",
  15. url: "../../json/data_index.json",
  16. // url: "https://m3.hhh.com.tw:18689/genjson_new?filename=realtime.json",
  17. dataType: "json",
  18. }).done((data) => {
  19. result = [...data];
  20. renderTopCarousel(result);
  21. console.log('result', result);
  22. }).fail((error) => {
  23. console.log('genjson_new error', error);
  24. });
  25. // 渲染頂部輪播
  26. function renderTopCarousel(data) {
  27. let temp = data[0]["data"];
  28. console.log('temp', temp);
  29. renderBullet(temp);
  30. renderBannerStr("slider-content", temp);
  31. let randomInt = getRandomInt(7);
  32. let adTopup_count = []
  33. // 新刊頭 Banner ga4 (一開始進來先送,剩下輪播送)
  34. setTimeout(() => {
  35. let list_Topup_Banner = document.querySelectorAll('.slider-content .slide-item');
  36. let ad_first;
  37. try {
  38. ad_first = list_Topup_Banner[randomInt].getAttribute('data_id');
  39. } catch (e) {
  40. ad_first = '加載問題,請無視';
  41. console.log(ad_first);
  42. }
  43. dataLayer.push({
  44. 'event': 'Banner_Impression',
  45. 'ad_name': ad_first,
  46. 'ad_location': '新刊頭Banner'
  47. });
  48. adTopup_count.push(ad_first)
  49. }, 1000);
  50. $(".slider-content").slick({
  51. dots: false,
  52. infinite: true,
  53. speed: 500,
  54. autoplay: true,
  55. autoplaySpeed: 3000,
  56. slidesToScroll: 1,
  57. arrows: true,
  58. initialSlide: randomInt, // 起始順序
  59. prevArrow:
  60. '<button type="button" class="slick-prev"><i class="fas fa-chevron-left" style="font-size: 32px;color: white;transform: translateY(-10px);"></i></button>',
  61. nextArrow:
  62. '<button type="button" class="slick-next"><i class="fas fa-chevron-right" style="font-size: 32px;color: white;transform: translateY(-10px);"></i></button>',
  63. });
  64. $(".bullet").removeClass("bullet-active");
  65. $(".bullet")
  66. .eq($(".slick-active").data("slick-index"))
  67. .addClass("bullet-active");
  68. $(".slider-content").on("afterChange", function (event, slick, currentSlide) {
  69. // // 新刊頭廣告 ga4
  70. // let list = document.querySelectorAll('.slider-content .slide-item');
  71. // let ad1 = list[currentSlide + 1].getAttribute('data_id')
  72. // // 送 dataLayer (要看得到廣告才送,電腦手機版不一樣)
  73. // if ($('.slider-content .slide-item').offset().top - $(window).scrollTop() > -175) {
  74. // if (!adTopup_count.includes(ad1)) {
  75. // dataLayer.push({
  76. // 'event': 'Banner_Impression',
  77. // 'ad_name': ad1,
  78. // 'ad_location': '新刊頭Banner'
  79. // });
  80. // adTopup_count.push(ad1);
  81. // }
  82. // }
  83. // 輪播
  84. $(".bullet").removeClass("bullet-active");
  85. $(`.bullet.item-${currentSlide}`).addClass("bullet-active");
  86. });
  87. }
  88. function renderBannerStr(sec, data) {
  89. let adNamesTop = [];
  90. let str = "";
  91. for (let i = 0; i < data.length; i++) {
  92. let adNameTop = `${data[i]["id"]}${data[i]["alt"]}`;
  93. adNamesTop.push(adNameTop);
  94. if (data[i]["Dwebp"]) {
  95. str += `
  96. <a href="${data[i]["link"]}" data-ga4="${data[i]["id"]}${data[i]["alt"]}">
  97. <img class="${sec}-${i + 1} slide-item img-fluid" src="${data[i]["Dwebp"]}" data-bg="${data[i]["Dwebp"]}" alt="${data[i]["alt"]}" data_id="${data[i]["id"]}${data[i]["alt"]}">
  98. </a>`;
  99. } else {
  100. str += `
  101. <a href="${data[i]["link"]}" data-ga4="${data[i]["id"]}${data[i]["alt"]}">
  102. <img class="${sec}-${i + 1} slide-item img-fluid" src="${data[i]["DimgUrl"]}" data-bg="${data[i]["DimgUrl"]}" alt="${data[i]["alt"]}" data_id="${data[i]["id"]}${data[i]["alt"]}">
  103. </a>`;
  104. }
  105. }
  106. $(`.${sec}`).html(str);
  107. // ga4-banners
  108. adNamesTop.forEach((adNameTop) => {
  109. document.querySelectorAll(`a[data-ga4="${adNameTop}"]`).forEach((element) => {
  110. element.addEventListener('click', function () {
  111. dataLayer.push({
  112. 'event': 'Banner_Click',
  113. 'ad_name': adNameTop,
  114. 'ad_location': '新刊頭'
  115. });
  116. });
  117. });
  118. });
  119. }
  120. function renderBullet(data) {
  121. let str = "";
  122. for (let i = 0; i < data.length; i++) {
  123. str += `<div class="bullet item-${i}"></div>`;
  124. }
  125. $(".bullet-list").html(str);
  126. }
  127. // 範圍內取隨機整數 (0-8)
  128. function getRandomInt(max) {
  129. return Math.floor(Math.random() * max);
  130. }
  131. $(".top-carousel .close-btn").click(function () {
  132. $(this).css("display", "none");
  133. $(".top-carousel").addClass("banner-close");
  134. sticky = 0;
  135. });
  136. </script>