JS30 day5 Flex Panels Image Gallery

Posted by Anthony Chao on 2019-11-04

JS30 day05 - Flex Panels Image Gallery

作業內容

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

可以參考這份 CodePen

學到什麼

列點如下:

  • CSS

    1. 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)

    2. flex 的 flex-grow / flex-shrink / flex-basis 參考資料:https://cythilya.github.io/2017/04/06/flexbox-advance/

    3. 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

    1. 使用 toggle 來新增或刪除 class ex. this.classList.toggle('open-active');
    2. 其實單就 Chrome 瀏覽器來說,if(e.propertyName.includes("flex")) 這句可以改成:if(e.propertyName === "flex-grow"),重點在於 Safari 裡面這個事件叫做 flex 而不是 flex-group ,為了符合所有瀏覽器才這樣寫
    3. 在這次的練習中,有一個很容易犯錯的點
      我的 JS 其中有一行是這樣:
    1
    panels.forEach( panel => (panel.addEventListener('transitionend', toggleActive)));

    如果我的 function 這樣寫:

    1
    2
    3
    function toggleActive(e){
    this.classList.toggle('open-active');
    };

    結果是: 什麼都沒發生

    為什麼呢? 因為這裡會監聽到兩個'transitinend'事件:flex-growfont-size,因此他會很快的加上這個 class 又很快的拔掉(觸發兩次),所以要選擇一個來監聽,在這次作業中選擇監聽 flex-grow 事件:

    1
    2
    3
    4
    5
    function toggleActive(e){
    if(e.propertyName.includes("flex")) {
    this.classList.toggle('open-active');
    }
    };

總結

今天這個效果滿炫的,我喜歡!然後又再次體會到自己前端能力的不足,再接再厲!

參考資料:
https://github.com/wesbos/JavaScript30

code 內容:
HTML:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<div class="panels">
<div class="panel panel1">
<p>Hey</p>
<p>Let's</p>
<p>Dance</p>
</div>
<div class="panel panel2">
<p>Give</p>
<p>Take</p>
<p>Receive</p>
</div>
<div class="panel panel3">
<p>Experience</p>
<p>It</p>
<p>Today</p>
</div>
<div class="panel panel4">
<p>Give</p>
<p>All</p>
<p>You can</p>
</div>
<div class="panel panel5">
<p>Life</p>
<p>In</p>
<p>Motion</p>
</div>
</div>

CSS:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
html {
box-sizing: border-box;
background: #ffc600;
font-family: 'helvetica neue';
font-size: 20px;
font-weight: 200;
}

body {
margin: 0;
}

*, *:before, *:after {
box-sizing: inherit;
}

.panels {
min-height: 100vh;
overflow: hidden;
display: flex;
}

.panel {
flex: 1;
background: #6B0F9C;
box-shadow: inset 0 0 0 5px rgba(255,255,255,0.1);
color: white;
text-align: center;
align-items: center;
/* Safari transitionend event.propertyName === flex */
/* Chrome + FF transitionend event.propertyName === flex-grow */
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;
font-size: 20px;
background-size: cover;
background-position: center;
display: flex;
flex-direction: column;
}


.panel1 { background-image:url(https://source.unsplash.com/gYl-UtwNg_I/1500x1500); }
.panel2 { background-image:url(https://source.unsplash.com/rFKUFzjPYiQ/1500x1500); }
.panel3 { background-image:url(https://images.unsplash.com/photo-1465188162913-8fb5709d6d57?ixlib=rb-0.3.5&q=80&fm=jpg&crop=faces&cs=tinysrgb&w=1500&h=1500&fit=crop&s=967e8a713a4e395260793fc8c802901d); }
.panel4 { background-image:url(https://source.unsplash.com/ITjiVXcwVng/1500x1500); }
.panel5 { background-image:url(https://source.unsplash.com/3MNzGlQM7qs/1500x1500); }

/* Flex Children */
.panel > * {
flex: 1;
margin: 0;
width: 100%;
transition: transform 0.5s;
display: flex;
justify-content: center;
align-items: center;
/* border: 1px solid red; */
}
.panel > *:first-child{
transform: translateY(-100%);
}
.panel.open-active > *:first-child{
transform: translateY(0);
}
.panel > *:last-child{
transform: translateY(100%);
}
.panel.open-active > *:last-child{
transform: translateY(0);
}

.panel p {
text-transform: uppercase;
font-family: 'Amatic SC', cursive;
text-shadow: 0 0 4px rgba(0, 0, 0, 0.72), 0 0 14px rgba(0, 0, 0, 0.45);
font-size: 2em;
}

.panel p:nth-child(2) {
font-size: 4em;
}

.panel.open {
font-size: 40px;
flex: 5;
}

JS:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const panels = document.querySelectorAll('.panel');

function toggleOpen(e){
this.classList.toggle('open')
};

function toggleActive(e){
if(e.propertyName.includes("flex")) {
this.classList.toggle('open-active');
}
};

panels.forEach( panel => (panel.addEventListener('click', toggleOpen)));
panels.forEach( panel => (panel.addEventListener('transitionend', toggleActive)));




prevent_hack