iT邦幫忙

2025 iThome 鐵人賽

DAY 21
0
自我挑戰組

coding小白盜版學校官網ㄉ30天挑戰日記系列 第 21

coding小白盜版學校官網ㄉ30天挑戰日記 黑化成為AI奴隸主的第二天

  • 分享至 

  • xImage
  •  

嗨嗨大家好,歡迎來到coding小白盜版學校官網的第21天~這個計畫的內容是我這個coding超新手會在這30天裡,把複刻學校官網當作目標,從最基礎的前端html、css、js...開始邊做邊學,紀錄每天的學習進度,看看一個月過去能學到什麼程度。

今天是正式擺爛奴役AIㄉ第二天!不用自己動腦真是神清氣爽哇哈哈哈(反派笑
今天的進度就決定來做之前因為腦袋負荷不來,所以擺爛忽視的...

搜尋欄顯示/收合特效

之前已經做過網頁右上角的彈出式搜尋欄了,
點擊放大鏡就會出現搜尋框,再次點擊就會消失
https://ithelp.ithome.com.tw/upload/images/20251005/20178754mWELQtCTB4.png
學校的原版網站搜尋框出現/收合時都有擦去的特效但我沒有
所以今天我們要把這項讓務交給chatgpt

於是我們提供之前寫的code
html

<div for="search">
   <button class="bt01"><img class="img02" src="img\icon_search.png" alt="搜尋"></button>
   <input class="text-box" type="text" placeholder="關鍵字">
</div>

css

.search{
    position: relative;
    display: inline-block;
}
.bt01{
    display: inline-block;
    border: none;
    background-color: transparent;
    cursor: pointer;
}
.text-box{
    display: none;
    position: absolute;
    top: 5rem;   /* 貼齊上邊 */
    right: 3rem; /* 貼齊右邊 */
    clip-path: inset(0 0 100% 0);
}
.img02{/*搜尋*/
    width: 36px;
    height: 36px;
    vertical-align: middle;
    flex-shrink: 0;
}

接著下指令

我現在已經做好了點擊botton會出現input懸浮輸入框的code,如何為輸入框加上由上至下擦去的顯示/消失動態特效?直接幫我寫一段新的js,讓我可以直接複製替代

這是他給我的代碼

const search = document.querySelector(".bt01");
const text_box = document.querySelector(".text-box");
let isAnimating = false; // 防止動畫中重複點擊

search.addEventListener("click", function () {
  if (isAnimating) return; // 動畫進行中不反應

  if (text_box.style.display === "none" || text_box.style.display === "") {
    // 顯示時:先設為 block 再觸發動畫
    text_box.style.display = "inline-block";
    text_box.style.clipPath = "inset(0 0 100% 0)";
    text_box.style.opacity = "0";
    text_box.style.transition = "clip-path 0.4s ease, opacity 0.3s ease";

    requestAnimationFrame(() => {
      text_box.style.clipPath = "inset(0 0 0 0)";
      text_box.style.opacity = "1";
    });
  } else {
    // 關閉動畫
    isAnimating = true;
    text_box.style.clipPath = "inset(100% 0 0 0)";
    text_box.style.opacity = "0";

    setTimeout(() => {
      text_box.style.display = "none";
      isAnimating = false;
    }, 400); // 與 transition 時間一致
  }
});

有點小不對勁,收合動畫和我想的不一樣,是由上至下消失
他回答:

了解 👌你想要的是最乾淨自然的版本:
顯示時 → 由上往下擦出
收合時 → 由下往上收回(方向相反、速度一致)
不要延遲、不要淡入淡出,只保留「反向的動態」
這樣最接近「顯示的反向動畫」,我幫你整理成最精簡、可直接貼上的版本👇

...但抱怨了好多次都沒得到滿意的答案,只好自己動手ㄌ
最後發現應該是這段「text_box.style.clipPath = "inset(100% 0 0 0)"; // 下方先收起」有貓膩

// 收合時:從下往上收
    text_box.style.clipPath = "inset(0 0 0 0)"; // 確保起點完整
    text_box.style.transition = "clip-path 0.4s ease";
    requestAnimationFrame(() => {
      text_box.style.clipPath = "inset(100% 0 0 0)"; // 下方先收起
    });

    setTimeout(() => {
      text_box.style.display = "none";
    }, 400);

我試著把inset(100% 0 0 0)括號裡的四個數字互換
發現原來它還真的是控制消失方向關鍵耶

inset(100% 0 0 0)消失順序是上至下
inset(0 100% 0 0)消失順序是右至左
inset( 0 0 100%0)消失順序...就是我要的是下至上!!!

任務圓滿達成,微動了一點腦也讓人有成就感,真是完美的一天阿
收工,各位明天見ㄅㄅ!


上一篇
coding小白盜版學校官網ㄉ30天挑戰日記 正式進入倦怠模式
下一篇
coding小白盜版學校官網ㄉ30天挑戰日記 欺騙感情!挑撥離間!
系列文
coding小白盜版學校官網ㄉ30天挑戰日記22
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言