JS30 day05 - Flex Panels Image Gallery
作業內容
今天要做的是做出一個網頁,畫面上有五條欄位,我點擊其中一個他就會放大,並同時會有字從上方跟下方冒出來,再點一次他就會縮小

可以參考這份 CodePen
學到什麼
列點如下:
- 
CSS
- 
box-shadow 內陰影的效果還滿漂亮的
box-shadow: inset 0 0 0 5px rgba(255,255,255,0.1);參考資料:https://boohover.pixnet.net/blog/post/35116159-陰影效果及光暈效果的-box-shadow-(css-property) - 
flex 的
flex-grow/flex-shrink/flex-basis參考資料:https://cythilya.github.io/2017/04/06/flexbox-advance/ - 
transition 裡面可以指定各種不同的動畫,表現時間跟方式
 
1
2
3
4
5
6.panel {
transition:
font-size 0.7s cubic-bezier(0.61,-0.19, 0.7,-0.11),
flex 0.7s cubic-bezier(0.61,-0.19, 0.7,-0.11),
background 0.2s;
} - 
 - 
JS
- 使用 toggle 來新增或刪除 class ex. 
this.classList.toggle('open-active'); - 其實單就 Chrome 瀏覽器來說,
if(e.propertyName.includes("flex"))這句可以改成:if(e.propertyName === "flex-grow"),重點在於 Safari 裡面這個事件叫做flex而不是flex-group,為了符合所有瀏覽器才這樣寫 - 在這次的練習中,有一個很容易犯錯的點:
我的 JS 其中有一行是這樣: 
1
panels.forEach( panel => (panel.addEventListener('transitionend', toggleActive)));
如果我的 function 這樣寫:
1
2
3function toggleActive(e){
this.classList.toggle('open-active');
};結果是: 什麼都沒發生
為什麼呢? 因為這裡會監聽到兩個
'transitinend'事件:flex-grow跟font-size,因此他會很快的加上這個 class 又很快的拔掉(觸發兩次),所以要選擇一個來監聽,在這次作業中選擇監聽flex-grow事件:1
2
3
4
5function toggleActive(e){
if(e.propertyName.includes("flex")) {
this.classList.toggle('open-active');
}
}; - 使用 toggle 來新增或刪除 class ex. 
 
總結
今天這個效果滿炫的,我喜歡!然後又再次體會到自己前端能力的不足,再接再厲!
參考資料:
https://github.com/wesbos/JavaScript30
code 內容:
HTML:
1  | <div class="panels">  | 
CSS:
1  | html {  | 
JS:
1  | const panels = document.querySelectorAll('.panel');  |