index.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. let userAgent;
  2. let isSafari = false;
  3. let browserName;
  4. window.onload = function(){
  5. /* if(screen.width >= 901){
  6. window.location.href = "https://www2.hhh.com.tw/";
  7. } */
  8. userAgent = navigator.userAgent;
  9. detectBrowser(userAgent);
  10. detectDirection ();
  11. let result;
  12. $.ajax({
  13. method: "GET",
  14. url: "./json/realtime.json",
  15. dataType: "json",
  16. }).done(function (msg) {
  17. result = [...msg];
  18. renderSec00(result);
  19. renderSec02(result);
  20. renderSec03(result);
  21. renderSec06(result);
  22. renderSec07(result);
  23. renderSec08(result);
  24. renderSec09(result);
  25. renderSec10(result);
  26. renderSec11(result);
  27. renderVideo(result);
  28. });
  29. }
  30. function detectBrowser(agent){
  31. if(userAgent.match(/chrome|chromium|crios/i)){
  32. browserName = "chrome";
  33. }else if(userAgent.match(/firefox|fxios/i)){
  34. browserName = "firefox";
  35. } else if(userAgent.match(/safari/i)){
  36. browserName = "safari";
  37. }else if(userAgent.match(/opr\//i)){
  38. browserName = "opera";
  39. } else if(userAgent.match(/edg/i)){
  40. browserName = "edge";
  41. }else{
  42. browserName="No browser detection";
  43. }
  44. if(browserName === 'safari'){
  45. isSafari = true;
  46. }
  47. console.log(isSafari);
  48. }
  49. function detectDirection () {
  50. let height = (window.screen.width * 2) / 3;
  51. $('.sec-02 .slide-item').css('height', `${height}px`);
  52. let topHeight = (window.screen.width * 30) / 131;
  53. $('.sec-00 .slide-item').css('height', `${topHeight}px`);
  54. $('.sec-00 .container-fluid').css('height', `${topHeight}px`);
  55. }
  56. $(".sec-01__slider").slick({
  57. dots: true,
  58. autoplay: false,
  59. arrows: false,
  60. });
  61. function renderSec00(data) {
  62. let temp = data[0]['data'];
  63. renderBannerStr('sec-00__slider', temp);
  64. $(".sec-00__slider").slick({
  65. dots: false,
  66. speed: 800,
  67. autoplay: true,
  68. });
  69. }
  70. function renderSec02(data) {
  71. let temp = data[1]['data'];
  72. renderBannerStr('sec-02__slider', temp);
  73. let str = '';
  74. $(".sec-02__slider").slick({
  75. dots: false,
  76. autoplay: true,
  77. arrows: true,
  78. fade: true,
  79. });
  80. }
  81. function renderBannerStr(sec, data) {
  82. let str = '';
  83. console.log(isSafari);
  84. for(let i = 0; i < data.length; i++){
  85. if(data[i]['webp'] && !isSafari) {
  86. str+= `<div class="${sec}-${i+1} slide-item" onclick="window.open('${data[i]['link']}');" style="background-image: url('${data[i]['webp']}');" data-bg="${data[i]['webp']}"></div>`
  87. } else {
  88. str+= `<div class="${sec}-${i+1} slide-item" onclick="window.open('${data[i]['link']}');" style="background-image: url('${data[i]['imgUrl']}');" data-bg="${data[i]['imgUrl']}"></div>`
  89. }
  90. }
  91. $(`.${sec}`).html(str);
  92. }
  93. function renderSec03(data) {
  94. let randomIdx = Math.floor(Math.random()*3);
  95. let moreLinks = ['https://hhh.com.tw/cases/lists/', 'https://hhh.com.tw/videos/lists/', 'https://hhh.com.tw/columns/lists/'];
  96. const ran = $('.sec-03__tabdiv .nav-item-link').eq(randomIdx);
  97. const ranDiv = $('#pills-tabContent .tab-pane').eq(randomIdx);
  98. ran.addClass('active');
  99. ranDiv.addClass('active');
  100. ranDiv.addClass('show');
  101. $('.sec-03 .morelink').attr('href', $('.sec-03__tabdiv .nav-item-link.active').data('link'));
  102. let temp = data[2]['data'];
  103. for(let i = 0; i < temp.length; i++){
  104. if(temp[i]["tab"] == '最夯設計'){
  105. let str = '';
  106. let sub = temp[i]["data"];
  107. for(let j = 0; j < sub.length; j++){
  108. str+= `<div class="tabpar__card" onclick="window.open('${sub[j]['link']}')";>
  109. <div class="tabpar__card__imgfr" style="background-image: url('${sub[j]['imgUrl']}');" data-bg="${sub[j]['imgUrl']}"></div>
  110. <p class="mt-2 pe-2">${sub[j]['title']}</p>
  111. </div>`
  112. }
  113. $('#pills-hot .tabpar').html(str);
  114. }
  115. if(temp[i]["tab"] == '影音實錄'){
  116. let str = '';
  117. let sub = temp[i]["data"];
  118. for(let j = 0; j < sub.length; j++){
  119. str+= `<div class="tabpar__card" onclick="window.open('${sub[j]['link']}')";>
  120. <div class="tabpar__card__imgfr" style="background-image: url('${sub[j]['imgUrl']}');" data-bg="${sub[j]['imgUrl']}"><img class="tabpar__card__play lazyload" data-src="images/Play-Button.webp"></div>
  121. <p class="mt-2 pe-2">${sub[j]['description']}</p>
  122. </div>`
  123. }
  124. $('#pills-video .tabpar').html(str);
  125. }
  126. if(temp[i]["tab"] == '專欄文章'){
  127. let str = '';
  128. let sub = temp[i]["data"];
  129. for(let j = 0; j < sub.length; j++){
  130. str+= `<div class="tabpar__card" onclick="window.open('${sub[j]['link']}')";>
  131. <div class="tabpar__card__imgfr" style="background-image: url('${sub[j]['imgUrl']}');" data-bg="${sub[j]['imgUrl']}"></div>
  132. <p class="mt-2 pe-2">${sub[j]['title']}</p>
  133. </div>`
  134. }
  135. $('#pills-blog .tabpar').html(str);
  136. }
  137. }
  138. const cardText = document.querySelectorAll('.tabpar__card p');
  139. cardText.forEach((item, i) => {
  140. let len = item.textContent.length;
  141. if(len > 20) {
  142. let str = item.textContent.substring(0, 20) + '...';
  143. item.textContent = str;
  144. }
  145. });
  146. $('.sec-03__tabdiv .nav-item-link').click(function() {
  147. $('.sec-03 .morelink').attr('href',$('.sec-03__tabdiv .nav-item-link.active').data('link'));
  148. });
  149. }
  150. function renderSec06(data) {
  151. let temp = data[3]['data'];
  152. let str = '';
  153. for(let i = 0; i < temp.length; i++){
  154. if(i === 0) {
  155. str+= `<div class="carousel-item active" onclick="window.open('${temp[i]['link']}');"
  156. style="background-image: url('${temp[i]['imgUrl']}');" data-bg="${temp[i]['imgUrl']}">
  157. </div>`
  158. } else {
  159. str+= `<div class="carousel-item" onclick="window.open('${temp[i]['link']}');"
  160. style="background-image: url('${temp[i]['imgUrl']}');" data-bg="${temp[i]['imgUrl']}">
  161. </div>`
  162. }
  163. }
  164. $('.sec-06 .carousel-inner').html(str);
  165. }
  166. function renderSec07(data) {
  167. let temp = data[4]['data'];
  168. let str = '';
  169. for(let i = 0; i < temp.length; i++){
  170. str+= `
  171. <div class="sec-07__slider-${i+1} sec-07__card col-12 mx-1" onclick="window.open('${temp[i]['link']}');">
  172. <div class="sec-07__imgfr mb-2" style="background-image: url('${temp[i]['imgUrl']}');"></div>
  173. <p class="sec-07__cardtxt">${temp[i]['description']}</p>
  174. </div>`
  175. }
  176. $('.sec-07__slider').html(str);
  177. $(".sec-07__slider").slick({
  178. arrows: false,
  179. slidesToShow: 3,
  180. centerMode: true,
  181. infinite: false,
  182. dots: false,
  183. centerPadding: '12px',
  184. responsive: [
  185. {
  186. breakpoint: 480,
  187. settings: {
  188. arrows: false,
  189. slidesToShow: 1,
  190. centerMode: true,
  191. infinite: false,
  192. dots: true,
  193. centerPadding: '12px',
  194. }
  195. }
  196. ],
  197. });
  198. const cardText = document.querySelectorAll('.sec-07__cardtxt');
  199. cardText.forEach((item, i) => {
  200. let len = item.textContent.length;
  201. if(len > 20) {
  202. let str = item.textContent.substring(0, 25) + '...';
  203. item.textContent = str;
  204. }
  205. })
  206. }
  207. function renderSec08(data) {
  208. let temp = data[5]['data'];
  209. let str = '';
  210. for(let i = 0; i < temp.length; i++){
  211. str+= `<div class="sec-08__slider-${i+1} mx-1 slide-item" style="width: 70vw; background-image: url('${temp[i]['imgUrl']}');" onclick="window.open('${temp[i]['link']}');" data-bg="${temp[i]['imgUrl']}"></div>`
  212. }
  213. $('.sec-08__slider').html(str);
  214. $(".sec-08__slider").slick({
  215. autoplay: true,
  216. autoplaySpeed: 8000,
  217. arrows: false,
  218. slidesToShow: 3,
  219. centerMode: true,
  220. infinite: true,
  221. dots: false,
  222. centerPadding: '6px',
  223. responsive: [
  224. {
  225. breakpoint: 480,
  226. settings: {
  227. autoplay: true,
  228. autoplaySpeed: 8000,
  229. arrows: false,
  230. slidesToShow: 1,
  231. centerMode: true,
  232. infinite: true,
  233. dots: false,
  234. centerPadding: '12px',
  235. }
  236. }
  237. ],
  238. });
  239. }
  240. function renderSec09(data) {
  241. let temp = data[6]['data'];
  242. let str = '';
  243. for(let i = 0; i < temp.length; i++){
  244. if(temp[i].video !== 'false') {
  245. str+= `<div class="sec-09__card" onclick="window.open('${temp[i]['link']}');">
  246. <div class="sec-09__card__imgfr" style="background-image: url('${temp[i]['imgUrl']}');" data-bg="${temp[i]['imgUrl']}"><img class="sec-09__card__play lazyload" data-src="images/Play-Button.webp"></div>
  247. <p>${temp[i]['title']}</p>
  248. </div>`
  249. } else {
  250. str+= `<div class="sec-09__card" onclick="window.open('${temp[i]['link']}');">
  251. <div class="sec-09__card__imgfr" style="background-image: url('${temp[i]['imgUrl']}');" data-bg="${temp[i]['imgUrl']}"></div>
  252. <p>${temp[i]['title']}</p>
  253. </div>`
  254. }
  255. }
  256. $('.sec-09__cardgrp').html(str);
  257. $(".sec-09__cardgrp").slick({
  258. arrows: false,
  259. slidesToShow: 3,
  260. slidesToScroll: 1,
  261. infinite: true,
  262. dots: false,
  263. centerPadding: '10px',
  264. responsive: [
  265. {
  266. breakpoint: 480,
  267. settings: {
  268. arrows: false,
  269. slidesToShow: 2,
  270. slidesToScroll: 1,
  271. infinite: true,
  272. dots: false,
  273. centerPadding: '10px',
  274. }
  275. }
  276. ],
  277. });
  278. }
  279. function renderSec10(data) {
  280. let temp = data[7]['data'];
  281. renderRecStr('sec-10', temp);
  282. }
  283. function renderSec11(data) {
  284. let temp = data[8]['data'];
  285. renderRecStr('sec-11', temp);
  286. }
  287. function renderRecStr(sec, data) {
  288. let str = '';
  289. for(let i = 0; i < data.length; i++){
  290. if(data[i].video !== 'false') {
  291. str+= `<div class="${sec}__card col-12 mx-1" onclick="window.open('${data[i]['link']}');">
  292. <div class="${sec}__slider-${i+1} mb-2 slide-item" style="background-image: url('${data[i]['imgUrl']}');" data-bg="${data[i]['imgUrl']}"><img class="${sec}__card__play lazyload" data-src="images/Play-Button.webp"></div>
  293. <p class="${sec}__cardtxt">${data[i]['title']}</p>
  294. </div>`;
  295. } else {
  296. str+= `<div class="${sec}__card col-12 mx-1" onclick="window.open('${data[i]['link']}');">
  297. <div class="${sec}__slider-${i+1} mb-2 slide-item" style="background-image: url('${data[i]['imgUrl']}');" data-bg="${data[i]['imgUrl']}"></div>
  298. <p class="${sec}__cardtxt">${data[i]['title']}</p>
  299. </div>`;
  300. }
  301. }
  302. $(`.${sec}__slider`).html(str);
  303. $(`.${sec}__slider`).slick({
  304. arrows: false,
  305. slidesToShow: 3,
  306. centerMode: false,
  307. infinite: false,
  308. dots: false,
  309. centerPadding: '12px',
  310. responsive: [
  311. {
  312. breakpoint: 480,
  313. settings: {
  314. arrows: false,
  315. slidesToShow: 1,
  316. centerMode: true,
  317. infinite: false,
  318. dots: false,
  319. centerPadding: '12px',
  320. }
  321. }
  322. ],
  323. });
  324. }
  325. function renderVideo(data) {
  326. let temp = data[9];
  327. $('.sec-05__video__txt').text(temp['title']);
  328. $('.sec-05__video__imgfr').css('background-image', `url(https://img.youtube.com/vi/${temp['yt']}/hqdefault.jpg)`);
  329. $('#videoModal').on('shown.bs.modal', function () {
  330. $('#videoModal iframe').attr('src', `https://www.youtube.com/embed/${temp['yt']}?controls=0&autoplay=1&amp;enablejsapi=1&amp;origin=https%3A%2F%2Fm.hhh.com.tw&amp;widgetid=1`);
  331. });
  332. $('#videoModal').on('hidden.bs.modal', function () {
  333. $('#videoModal iframe').removeAttr('src');
  334. });
  335. }
  336. window.addEventListener('scroll', fixedOnScroll);
  337. const navbar = document.querySelector('.navbar-main');
  338. function fixedOnScroll() {
  339. if(window.innerHeight < window.innerWidth){
  340. $('.sec-02 .slide-item').css('height', `130vh`);
  341. $('.sec-00 .slide-item').css('height', `200px`);
  342. $('.sec-00 .container-fluid').css('height', `200px`);
  343. } else {
  344. detectDirection ();
  345. }
  346. const sticky = document.querySelector('.sec-00 .container-fluid').offsetHeight;
  347. if(window.pageYOffset >= sticky){
  348. navbar.classList.add('sticky');
  349. } else {
  350. navbar.classList.remove('sticky');
  351. }
  352. }
  353. $('.navbar-toggler').click(function(){
  354. $(".sec-menu").css('display', 'block');
  355. $(".wholeBody").css('overflow-y', 'hidden');
  356. $('.sec-menu-block').addClass('slidein');
  357. $('.sec-menu-block').removeClass('slideout');
  358. });
  359. $('.navbar-back').click(function(){
  360. $(".sec-menu").css('display', 'none');
  361. $(".wholeBody").css('overflow-y', 'scroll');
  362. $('.sec-menu-block').removeClass('slidein');
  363. $('.sec-menu-block').addClass('slideout');
  364. });
  365. $('.subexpand').click(function(){
  366. $(this).parent().next().toggleClass('show');
  367. $(this).parent().toggleClass('show');
  368. });
  369. $('.navbar-search').click(function(){
  370. $(".sec-search").css('display', 'block');
  371. $(".wholeBody").css('overflow-y', 'hidden');
  372. })
  373. $('.navbar-backs').click(function(){
  374. $(".sec-search").css('display', 'none');
  375. $(".wholeBody").css('overflow-y', 'scroll');
  376. });
  377. $('.navbar-back-fav').click(function(){
  378. $(".sec-favor").css('display', 'none');
  379. })
  380. $('.btn-gotop').click(function () {
  381. $('html, body').animate({
  382. scrollTop: 0
  383. }, 500)
  384. });
  385. $(window).scroll(function() {
  386. if ( $(this).scrollTop() > 500 ){
  387. $('.fixed-btn').fadeIn(222);
  388. } else {
  389. $('.fixed-btn').stop().fadeOut(222);
  390. }
  391. }).scroll();
  392. const searchBtn = document.querySelector('.searchBtn');
  393. const searchBar = document.querySelector('.searchBar');
  394. const searchHotLink = document.querySelectorAll('.sec-search-hots__link');
  395. searchBtn.addEventListener('click', search);
  396. searchBar.addEventListener('keyup', pressSearch);
  397. function search() {
  398. if(searchBar.value == '') {
  399. window.location.href = 'https://m.hhh.com.tw/search/lists/case/';
  400. } else {
  401. window.location.href = `https://m.hhh.com.tw/search/lists/case/${searchBar.value}-keyword/`;
  402. }
  403. }
  404. function pressSearch(e) {
  405. if (e.keyCode === 13) {
  406. e.preventDefault();
  407. search();
  408. }
  409. }
  410. searchHotLink.forEach((item, i) => {
  411. item.addEventListener('click', function() {
  412. window.location.href = `https://m.hhh.com.tw/search/lists/case/${this.textContent}-keyword/`;
  413. })
  414. })
  415. document.addEventListener('lazybeforeunveil', function(e){
  416. var bg = e.target.getAttribute('data-bg');
  417. if(bg){
  418. e.target.style.backgroundImage = 'url(' + bg + ')';
  419. }
  420. });
  421. //jQuery time
  422. var current_fs, next_fs, previous_fs; //fieldsets
  423. var left, opacity, scale; //fieldset properties which we will animate
  424. var animating; //flag to prevent quick multi-click glitches
  425. $(".next").click(function(){
  426. if(animating) return false;
  427. animating = true;
  428. current_fs = $(this).parent().parent();
  429. next_fs = $(this).parent().parent().next();
  430. //activate next step on progressbar using the index of next_fs
  431. $(".form-progressbar li").eq($("fieldset").index(next_fs)).addClass("active");
  432. //show the next fieldset
  433. next_fs.animate({opacity: 1}, {
  434. step: function(now, mx) {
  435. //as the opacity of current_fs reduces to 0 - stored in "now"
  436. //1. scale current_fs down to 80%
  437. scale = 0 + (1 + now) * .5 ;
  438. //2. bring next_fs from the right(50%)
  439. left = (now * 50)+"%";
  440. //3. increase opacity of next_fs to 1 as it moves in
  441. opacity = 0 + now;
  442. next_fs.css({
  443. 'transform': 'scale('+scale+')',
  444. });
  445. next_fs.css({'opacity': opacity});
  446. },
  447. duration: 400,
  448. complete: function(){
  449. next_fs.show();
  450. animating = false;
  451. },
  452. //this comes from the custom easing plugin
  453. easing: 'easeInOutBack'
  454. });
  455. //hide the current fieldset with style
  456. current_fs.animate({opacity: 0}, {
  457. step: function(now, mx) {
  458. //as the opacity of current_fs reduces to 0 - stored in "now"
  459. //1. scale current_fs down to 80%
  460. scale = 1 - (1 - now) * 0.2;
  461. //2. bring next_fs from the right(50%)
  462. left = (now * 50)+"%";
  463. //3. increase opacity of next_fs to 1 as it moves in
  464. opacity = 1 - now;
  465. current_fs.css({
  466. 'transform': 'scale('+scale+')',
  467. });
  468. next_fs.css({'opacity': opacity});
  469. },
  470. duration: 400,
  471. complete: function(){
  472. current_fs.hide();
  473. animating = false;
  474. },
  475. //this comes from the custom easing plugin
  476. easing: 'easeInOutBack'
  477. });
  478. });