網頁

CSS transition

CSS 的 transition 可以有雙向和單向的設定。

網路上沒有什麼資料,目前只有看到這篇:[CSS筆記] transition、transform、animation 動畫屬性

下面二個都是雙向的例子。

啟動方向回復方向都套用一樣的效果,transition 屬性寫在初始狀態中。

div {
  width: 100px;
  height: 100px;
  
  background: red;
  
  transition: width 2s, height 4s; 
}

div:hover {
  width: 300px;
  height: 500px;
}

Demo
https://www.yuantw.com/demo/css-transition/1.html

———

transition 屬性寫在觸發狀態,會套用在啟動方向。transition 屬性寫在初始狀態,會套用在回復方向

/* 初始狀態 */
div {
  width: 100px;
  height: 100px;

  background: red;

  /* 回復方向 套用 */
  transition: width 5s, height 5s;
}

/* 觸發狀態 */
div:hover {
  width: 300px;
  height: 500px;

  /* 啟動方向 套用 */
  transition: width 2s, height 2s;
}

Demo
https://www.yuantw.com/demo/css-transition/2.html

網頁

JS debounce

以文字輸入框為例,連續輸入時,不會觸發動作(比如:查詢資料庫),要停止輸入並等待一段時間後才觸發動作。

下面的程式會在每次輸入時,清除前一個 setTimeout 並設置一個新的 setTimeout,直到停止輸入,並等待設定的秒數(這個例子是一秒)後觸發動作。

簡單的版本:

let timer;

inputText.addEventListener('input', (e) => {
  clearTimeout(timer);

  timer = setTimeout(() => {
    showArea.textContent = e.target.value; // 要觸發的動作
  }, 1000);
});

Demo
https://www.yuantw.com/demo/js-debounce-1/

———

網路上的資料都是把 debounce 寫成函式:

inputText.addEventListener('input', debounce(showData));

function debounce(fn, delay = 1000) {
  let timer;

  // return 的 function 是 inputText.addEventListener 的 event handler
  return (e) => {
    clearTimeout(timer);

    timer = setTimeout(() => {
      fn(e);
    }, delay);
  };
}

function showData(e) {
  showInfo.textContent = e.target.value;
}

Demo
https://www.yuantw.com/demo/js-debounce-2/

———

上面二個例子的行為是,等一串事件發生後,只觸發一次。

debounce 也可以實作成,立即觸發,忽略後面連續的一串事件

簡單的版本:

let timeoutID;

btn.addEventListener('click', () => {
  if (!timeoutID) {
    console.log(new Date());
  }

  clearTimeout(timeoutID);

  timeoutID = setTimeout(() => {
    timeoutID = null; // reset
  }, 1000);
});

———

下面的程式碼是從 JavaScript30 13 – Slide in on Scroll 修改

function debounce(func, wait = 1000, immediate = true) {
  let timeoutID;

  return (e) => {
    if (immediate && !timeoutID) {
      func(e);
    }

    clearTimeout(timeoutID);

    timeoutID = setTimeout(() => {
      timeoutID = null; // reset

      if (!immediate) {
        func(e);
      }
    }, wait);
  };
}


參考連結:

網頁

[PRACTICE] 取得全網頁高度

網頁包含三個 div。

div 的 CSS 設定:

box-sizing: border-box;

width: 300px;
height: 500px;

margin: 20px auto;
padding: 10px;

border: 5px solid #eee;

每個 div 的高度為:border + padding + 內容 = 500px

第一個 div 跟第二個 div 之間的 margin + 第二個 div 跟第三個 div 之間的 margin(div 之間的 margin 會重疊):20px + 20px

再加上第一個 div 的 margin(top) 跟最後一個 div 的 margin(bottom):20px + 20px

全網頁高度為:20px + 500px + 20px + 500px + 20px + 500px + 20px = 1580px

JS 的部份,document.documentElement 指的是 html 元素。

Demo
https://www.yuantw.com/demo/js-offset-height/

網頁

[PRACTICE] Infinity Scroll

跟著 Udemy 的課程 JavaScript Web Projects: 20 Projects to Build Your Portfolio 做練習。

這個練習是從 Unsplash 的 API 取得圖片,當捲軸快滑到底部時,再次從 API 取得圖片。

在 JS 裡有監聽 scroll 事件。

scroll 事件會連續觸發,導致連續跟 Unsplash API 要圖檔,為了避免這個狀況,可以新增一個變數(imgLoadedStatus)當作開關,值為布林值。

Demo
https://www.yuantw.com/demo/infinity-scroll/

網頁

[PRACTICE] Video Background

(桌機版畫面)

在桌機使用瀏覽器(縮小視窗寬度後)觀看頁面不會出現水平捲軸,但手機的瀏覽器會。

即使已經在 body 設定 overflow-x: hidden; 也沒有作用。

解決方式是用一個 div(.wrapper) 把內容包起來,並在該 div 使用 overflow-x: hidden;

<div class="wrapper">
  <header class="header">
    <nav>
      ...
    </nav>

    <video autoplay loop muted>
      ...
    </video>

    <div class="video-overlay">
      ...
    </div>
  </header>

  <main>
    <div class="container">
      ...
    </div>
  </main>
</div>

GitHub
https://github.com/yuanhuang-tw/video-background

Demo
https://yuanhuang-tw.github.io/video-background/


參考連結:

返回頂端