index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. $("*").each(function (index, element) {
  2. // 此元素被點選後執行
  3. $(this).click(function (e) {
  4. // 取得被點選元素的屬性:data-gt-target
  5. var target = $(this).attr("data-gt-target");
  6. var duration = $(this).attr("data-gt-duration");
  7. var offset = $(this).attr("data-gt-offset");
  8. // JS 語法:判斷式
  9. // if (條件) {程式區塊}
  10. // 當條件成立,會執行程式區塊
  11. // 如果 目標有資料 才會執行 { } 內的程式
  12. // 避免出現 undefine (未定義 - 不存在的資料)
  13. if (target) {
  14. //console.log("目標:" + target);
  15. //console.log("時間:" + duration);
  16. //console.log("位移:" + offset);
  17. // 上方位置 = 目標區塊.位移().上方位置
  18. var top = $(target).offset().top;
  19. //console.log("要前往元素的上方位置:" + top);
  20. // 網頁元素.停止().動畫({ 上方捲動:指定元素 - 位移},持續時間)
  21. // parseInt() 將文字轉為數字
  22. $("html").stop().animate({
  23. scrollTop: top - offset
  24. }, parseInt(duration));
  25. }
  26. });
  27. });
  28. // 避免動畫與使用者滾輪衝突
  29. // html 在滾動滾輪時 停止 html 所有效果
  30. $("html").on("mousewheel", function () {
  31. $("html").stop();
  32. });
  33. var scene = document.getElementById('scene');
  34. var parallaxInstance = new Parallax(scene);
  35. var scene1 = document.getElementById('scene1');
  36. var parallaxInstance = new Parallax(scene1);
  37. var scene2 = document.getElementById('scene2');
  38. var parallaxInstance = new Parallax(scene2);
  39. $(window).bind('scroll',function(e){
  40. parallaxScroll();
  41. });
  42. function parallaxScroll(){
  43. var scrolled = $(window).scrollTop();
  44. $('.object178').css('top',(200-(scrolled*.2))+'px');
  45. $('.object188').css('top',(150-(scrolled*.1))+'px');
  46. $('.object177').css('top',(600-(scrolled*.25))+'px');
  47. $('.object182').css('top',(400-(scrolled*.2))+'px');
  48. $('.object241').css('top',(600-(scrolled*.1))+'px');
  49. $('.object242').css('top',(700-(scrolled*.1))+'px');
  50. }
  51. // 文字動畫
  52. const sec = document.querySelectorAll('.sec');
  53. console.log(sec.length);
  54. const title = document.querySelectorAll('.title');
  55. const cover = document.querySelectorAll('.cover');
  56. const test_content = document.querySelectorAll('.test_content');
  57. function checkSlide() {
  58. console.log('pass');
  59. sec.forEach((block, i) => {
  60. // half way through the image
  61. //const test = document.querySelector('.test');
  62. const slideInAt = window.scrollY + window.innerHeight;
  63. console.log(slideInAt);
  64. //console.log(test.offsetTop);
  65. // bottom of the image
  66. const isHalfShown = slideInAt > block.offsetTop;
  67. if (isHalfShown) {
  68. console.log('active');
  69. title[i].style.bottom = '0em';
  70. //document.querySelector('.title').style.bottom = '0em';
  71. test_content[i].classList.add('fadein');
  72. cover[i].classList.add('slidein');
  73. //test.firstElementChild.classList.add('fadein');
  74. //test_content[i].fadeIn(1500);
  75. // $('.test_content').fadeIn(1500);
  76. }
  77. })
  78. };
  79. window.addEventListener('scroll', checkSlide);
  80. window.addEventListener('scroll', sec03PlantImg);
  81. window.addEventListener('scroll', sec03BottomImg);
  82. function sec03PlantImg() {
  83. const slideInAt = window.scrollY + window.innerHeight;
  84. const toTop = document.querySelector('.sec03-big').offsetTop + document.querySelector('.sec03_plant').offsetTop;
  85. const isHalfShown = slideInAt > toTop ;
  86. console.log(toTop);
  87. if (isHalfShown) {
  88. document.querySelector('.sec03_plant .p_cover').classList.add('slidein');
  89. }
  90. };
  91. function sec03BottomImg() {
  92. const slideInAt = window.scrollY + window.innerHeight;
  93. const toTop = document.querySelector('.sec03-big').offsetTop + document.querySelector('.sec_bottom').offsetTop + 300;
  94. const isHalfShown = slideInAt > toTop ;
  95. console.log(toTop);
  96. if (isHalfShown) {
  97. document.querySelector('.sec_bottom .b_cover').classList.add('slidein');
  98. }
  99. };