bhouse.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. // $(".responsive").slick({
  2. // dots: true,
  3. // slidesToShow: 1,
  4. // slidesToScroll: 1,
  5. // autoplay: true,
  6. // autoplaySpeed: 3000,
  7. // arrows: false,
  8. // });
  9. document.addEventListener('DOMContentLoaded', function() {
  10. $(".responsive").slick({
  11. dots: true,
  12. slidesToShow: 1,
  13. slidesToScroll: 1,
  14. autoplay: true,
  15. autoplaySpeed: 3000,
  16. arrows: false,
  17. });
  18. });
  19. // 直式輪播圖動態更改高度
  20. function changeHeight() {
  21. let firstImg = $('.style_house_sec02_slide div:first-child img');
  22. let firstImgHeight = firstImg.css('height');
  23. $('.style_house_sec02_slide .vertical-img').css("height", `${firstImgHeight}`)
  24. }
  25. changeHeight();
  26. $(window).resize(function () {
  27. setTimeout(() => {
  28. changeHeight();
  29. }, 100)
  30. });
  31. let imgCount = $(".slide_single_img").length;
  32. let heightImg = [];
  33. window.onload = function () {
  34. console.log('onload');
  35. $('.slide_single_img').each(function (i, array) { // jquery.each() 循環讀取所有圖片
  36. let height = $(this).height();
  37. let width = $(this).width();
  38. let x = (height / width);
  39. console.log('height / width = ', x);
  40. if (x >= 1) {
  41. if (i === 0) {
  42. $(this).addClass("img-height-vw");
  43. // $(this).css({
  44. // 'height': 500 + 'px',
  45. // "margin": 'auto',
  46. // });
  47. } else {
  48. // 預設高度為 0
  49. $(this).css({
  50. 'height': 0 + 'px',
  51. 'object-fit': 'contain',
  52. // 'margin': 'auto',
  53. });
  54. }
  55. heightImg.push($(this));
  56. }
  57. // else {
  58. // $(this).css({
  59. // 'height': 580 + 'px',
  60. // 'object-fit': 'cover'
  61. // });
  62. // console.log('this',this);
  63. // }
  64. if (i === imgCount - 1) {
  65. $('#slider-loading').addClass("slider_loading");
  66. $('.style_house_sec02').css("height", "auto");
  67. $('.style_house_sec02 .content').css('opacity', '1');
  68. }
  69. });
  70. let heightImgIndex = [];
  71. function getAllIndex(arr) {
  72. // 篩選符合條件 Index
  73. for (i = 0; i < arr.length; i++) {
  74. if (arr[i].height === 0)
  75. heightImgIndex.push(i);
  76. }
  77. return heightImgIndex;
  78. }
  79. let allSlideImg = document.querySelectorAll('.slide_single_img');
  80. getAllIndex(allSlideImg);
  81. // 取得 slick 當前的 Index
  82. $('.style_house_sec02 .slider').on('beforeChange', function (event, slick, currentSlide, nextSlide) {
  83. for (i = 0; i < heightImgIndex.length; i++) {
  84. console.log(heightImgIndex[i]);
  85. if (nextSlide == heightImgIndex[i]) {
  86. // allSlideImg[nextSlide].style.cssText = `
  87. // height: 500px;
  88. // margin: auto;
  89. // `;
  90. allSlideImg[nextSlide].classList.add("img-height-vw");
  91. } else {
  92. allSlideImg[heightImgIndex[i]].style.height = '0px';
  93. }
  94. }
  95. });
  96. $('.slide_img').each(function () { // jquery.each() 循環讀取所有圖片
  97. var height = $(this).height();
  98. var width = $(this).width();
  99. let x = (height / width);
  100. if (x > 1) {
  101. $(this).css({
  102. 'width': 130 + 'px',
  103. "margin": 'auto',
  104. "padding": "5px",
  105. });
  106. } else {
  107. $(this).css({
  108. "margin-top": '30px',
  109. "padding": "5px",
  110. });
  111. }
  112. });
  113. }
  114. // $('.slide_single_img').each(function (i) { // jquery.each() 循環讀取所有圖片
  115. // let height = $(this).height();
  116. // let width = $(this).width();
  117. // let x = (height / width);
  118. // console.log('index', i);
  119. // console.log('height / width = ', x);
  120. // if (x >= 1) {
  121. // console.log('長圖',i);
  122. // if (i===0) {
  123. // return;
  124. // }
  125. // heightImg.push($(this));
  126. // // 預設高度為 0
  127. // $(this).css({
  128. // 'height': 0 + 'px',
  129. // "margin": 'auto',
  130. // });
  131. // }
  132. // });
  133. // let heightImgIndex = [];
  134. // function getAllIndex(arr) {
  135. // // 篩選符合條件 Index
  136. // for (i = 0; i < arr.length; i++) {
  137. // if (arr[i].height === 0)
  138. // heightImgIndex.push(i);
  139. // }
  140. // return heightImgIndex;
  141. // }
  142. // let allSlideImg = document.querySelectorAll('.slide_single_img');
  143. // getAllIndex(allSlideImg);
  144. // // 取得 slick 當前的 Index
  145. // $('.style_house_sec02 .slider').on('beforeChange', function (event, slick, currentSlide, nextSlide) {
  146. // for (i = 0; i < heightImgIndex.length; i++) {
  147. // console.log(heightImgIndex[i]);
  148. // if (nextSlide == heightImgIndex[i]) {
  149. // allSlideImg[nextSlide].style.cssText = `
  150. // height: 500px;
  151. // margin: auto;
  152. // `;
  153. // } else {
  154. // allSlideImg[heightImgIndex[i]].style.height = '0px';
  155. // }
  156. // }
  157. // });
  158. // $('.slide_img').each(function () { // jquery.each() 循環讀取所有圖片
  159. // var height = $(this).height();
  160. // var width = $(this).width();
  161. // let x = (height / width);
  162. // if (x > 1) {
  163. // $(this).css({
  164. // 'width': 100 + 'px',
  165. // "margin": 'auto',
  166. // "padding": "0px",
  167. // });
  168. // } else {
  169. // $(this).css({
  170. // "margin-top": '30px',
  171. // "padding": "5px",
  172. // });
  173. // }
  174. // });
  175. $('.style_house_sec02 .slider-for').slick({
  176. slidesToShow: 1,
  177. slidesToScroll: 1,
  178. arrows: true,
  179. fade: true,
  180. asNavFor: '.slider-nav',
  181. prevArrow: '<button type="button" class="slick-prev"><img src="/img/arrow_left.png" alt="" width="50"></button>',
  182. nextArrow: '<button type="button" class="slick-next"><img src="/img/arrow_right.png" alt="" width="50"></button>'
  183. });
  184. $('.style_house_sec02 .slider-nav').slick({
  185. slidesToShow: 5,
  186. slidesToScroll: 3,
  187. asNavFor: '.slider-for',
  188. dots: false,
  189. arrows: false,
  190. // centerMode: true,
  191. focusOnSelect: true
  192. });
  193. $(document).on("click", ".article__readMore", function (event) {
  194. $('.bhouseweb_loc_sec02').css('height', 'auto');
  195. $('.article__readMore').hide();
  196. });
  197. $(document).on("click", ".bhouseweb_loc_type>label", function (event) {
  198. $(this).siblings().removeClass('select');
  199. $(this).toggleClass("select");
  200. });
  201. $(".bt_slogan_portfolio").click(function () {
  202. $(".bt_slogan_portfolio").fadeOut();
  203. });
  204. $(".bt_slogan").click(function () {
  205. $(".bt_slogan_portfolio").fadeIn();
  206. });
  207. // Navbar Icon
  208. function changeIcon(e) {
  209. const item = document.querySelector('[data-toggle-class]');
  210. item.className === "close-btn" ? item.className = "navbar-toggler-icon" : item.className = "close-btn";
  211. }
  212. $('.furniture_design_content .slider-for').slick({
  213. slidesToShow: 1,
  214. slidesToScroll: 1,
  215. arrows: false,
  216. fade: true,
  217. asNavFor: '.slider-nav'
  218. });
  219. $('.furniture_design_content .slider-nav').slick({
  220. slidesToShow: 3,
  221. slidesToScroll: 1,
  222. asNavFor: '.slider-for',
  223. focusOnSelect: true
  224. });
  225. // 外部連結
  226. let fb_link = `https://www.facebook.com/sharer.php?u=${location.href}`;
  227. let fb_list = document.querySelectorAll('#collection_icon_fb');
  228. for (let i = 0; i < fb_list.length; i++) {
  229. const item = fb_list[i];
  230. item.setAttribute("href", fb_link);
  231. }
  232. let line_link = `http://line.naver.jp/R/msg/text/?${location.href}`;
  233. let line_list = document.querySelectorAll('#collection_icon_line');
  234. for (let i = 0; i < line_list.length; i++) {
  235. const item = line_list[i];
  236. item.setAttribute("href", line_link);
  237. }
  238. // blog 分類篩選
  239. function getCategories(item) {
  240. // 將分類名稱儲存至 localStorage
  241. localStorage.setItem('category', item);
  242. }
  243. // 錨點
  244. $("*").each(function (index, element) {
  245. $(this).click(function (e) {
  246. var target = $(this).attr("data-gt-target");
  247. var duration = $(this).attr("data-gt-duration");
  248. var offset = $(this).attr("data-gt-offset");
  249. if (target) {
  250. //console.log("目標:" + target);
  251. //console.log("時間:" + duration);
  252. //console.log("位移:" + offset);
  253. // 上方位置 = 目標區塊.位移().上方位置
  254. var top = $(target).offset().top;
  255. $("html").stop().animate({
  256. scrollTop: top - offset
  257. }, parseInt(duration));
  258. }
  259. });
  260. });
  261. let tagList = [];
  262. (function getContentsData() {
  263. // 線上版網址 https://bhouse3.ptt.cx:9002/api/contents?url=/blog
  264. // 本地端網址 http://localhost:9001/api/
  265. fetch('https://bhouse3.ptt.cx:9002/api/contents?url=/blog').then(res => res.json()).then(list => {
  266. list.map(({ blog_tag }) => {
  267. if (blog_tag) {
  268. let tagText = blog_tag.replace("[", "").replace("]", "");
  269. let tagArr = tagText.split(',');
  270. tagArr.map(e => {
  271. tagList.push(e);
  272. })
  273. // 移除重複字串
  274. tagList = tagList.filter((item, index) => tagList.indexOf(item) === index);
  275. // 隨機排序
  276. tagList = tagList.sort(() => 0.5 - Math.random());
  277. const dom = document.querySelectorAll('.blog-tags');
  278. let tagTemplate = [];
  279. // 標籤顯示兩排
  280. let count = 0;
  281. if (document.body.offsetWidth<767) {
  282. count = 8;
  283. } else {
  284. count = 10;
  285. }
  286. for (let i = 0; i < count; i++) {
  287. const element = tagList[i];
  288. let content = `<a href="/blog?tags=${element}">${element}</a>`;
  289. tagTemplate.push(content);
  290. }
  291. // 移除陣列逗號
  292. if (dom) {
  293. for (let i = 0; i < dom.length; i++) {
  294. const element = dom[i];
  295. element.innerHTML = tagTemplate.join("");
  296. }
  297. }
  298. }
  299. })
  300. })
  301. })();