CSS&JS/👀Study and Copy

[JS+CSS]Madia님 영상보다가 디자인 예쁜거 구현해보기 (고객의 소리 부분)

arancia_ 2024. 10. 4. 14:09

https://www.youtube.com/watch?v=dIEViv_i7Xs

데모

See the Pen 고객의 소리 by Oh Ikmyeong (@dpffpself) on CodePen.

 

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
28
29
30
31
32
33
34
35
36
<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>마디아 고객의 소리</title>
    <link rel="stylesheet" href="./style/main.css">
    <script src="./js/main.js" type="module"></script>
</head>
 
<body>
    <a href="https://www.youtube.com/watch?v=dIEViv_i7Xs" target="_blank">마디아고객의 소리</a>
    <div class="wrap">
        <div class="wrap-border"></div>
        <div class="wrap-border"></div>
        <div class="wrap-border"></div>
        <div class="wrap-border"></div>
        <div class="wrap-border"></div>
        <section class="sect">
            <h1 class="sect-title">고객의 소리를<br>매일 확인합니다.</h1>
            <article class="pager">
                <button class="pager-btn pager-btn-prev" title="이전"></button>
                <div class="pager-number">
                    <strong class="pager-number-curr">01</strong>
                    <span>/</span>
                    <span class="pager-number-all">99</span>
                </div>
                <button class="pager-btn pager-btn-next" title="다음"></button>
            </article>
            <ul class="list"></ul>
        </section>
    </div>
</body>
 
</html>
cs

CSS

item.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
.item{
    position:relative;
    overflow:hidden;
    width:100%;
    padding:0 40px;
}
.item-inner-wrap{
    position:relative;
    width:100%;
}
 
/* 별 */
.star-sect{
    display:flex;flex-flow:row nowrap;
    justify-content:flex-start; align-items:center;
    gap:5px;
    position:relative;
    width:100%;
}
 
.star{
    position:relative;
    font-size:15px;
}
.star-bg{
    color:#dedede;
}
.star-sw{
    --_per : calc((var(--sw, 0) * 2) * 10%);
    position:absolute;
    width:100%;
    top:0;left:0;
    clip-path: polygon(0 0, var(--_per) 0, var(--_per) 100%, 0% 100%);
}
.star-score-num{font-size:14px; font-weight:700; color:#000;}
.star-score-whole{font-size:13px;font-weight:500;color:#888;}
 
/* 텍스트 */
.text{
    position:relative;
    width:100%;
    margin-top:20px;
    margin-bottom:60px;
}
.text-title{
    margin-bottom:16px;
    font-size:18px; font-weight:bold;
}
.text-content{
    line-height:1.6;
    font-size:14px; color:#777;
    display:-webkit-box;
    white-space:normal; 
    -webkit-line-clamp:4;
    -webkit-box-orient:vertical;
    overflow:hidden;
    min-height:calc(1em * 6);
}
 
/* 하단영역 */
.info{
    display:flex;flex-flow:row wrap;
    justify-content:space-between; align-items:center;
    gap:10px;
    position:relative;
    width:100%;
}
 
.info-btn{
    padding-bottom:1px;
    background:transparent;
    border:none;
    border-bottom:2px solid #444;
    font-size:13px; font-weight:600;
}
 
.info-date{
    font-size:13px; color:#888;
}
cs

main.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import { List } from "./List.js";
import { Pager } from "./Pager.js";
 
(async function(){
    /* json fetch */
    const data = await fetch("./dummy.json").then(res=>res.json()).then(json=>json.list);
    
    /* reset - pager */
    new Pager()
    .set_data_length(data?.length)
    .init();
 
    /* reset - list */
    new List()
    .set_data(data)
    .init();
 
}())
cs

Pager.js

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
89
90
91
92
93
94
95
96
97
import { List } from "./List.js";
import { PagerModel } from "./PagerModel.js";
 
export class Pager {
    #dataLength;
 
    constructor() {
        this.$curr = document.getElementsByClassName("pager-number-curr")?.[0];
        this.$all = document.getElementsByClassName("pager-number-all")?.[0];
        this.$btnPrev = document.getElementsByClassName("pager-btn-prev")?.[0];
        this.$btnNext = document.getElementsByClassName("pager-btn-next")?.[0];
        this.currSave = null;
    }
 
    /**
     * 
     * @param {*} data 
     * @returns 
     */
    set_data_length(dataLength) {
        this.#dataLength = dataLength;
        return this;
    }//set_data
 
    /**
     * 
     * @returns 
     */
    init() {
        /* cacul */
        PagerModel.cacul_initial(this.#dataLength);
        this.currSave = PagerModel.curr;
 
        /* display */
        this.display_curr_all();
        this.change_pager_btn();
 
        /* (EVENT) */
        this.$btnPrev.addEventListener("click", () => {
            PagerModel.currDecrease();
            if(!this.on_click_btn_common()) return;
        });
        this.$btnNext.addEventListener("click", () => {
            PagerModel.currIncrease();
            if(!this.on_click_btn_common()) return;
        });
 
        /* 최종 */
        return this;
    }//init
 
    /**
     * 
     * @returns 
     */
    on_click_btn_common = () => {
        if (this.currSave === PagerModel.curr){return false;}
        console.log("변화 시작 accept");
        this.currSave = PagerModel.curr;
        this.display_curr_all();
        this.change_pager_btn();
        List.fill();
        return true;
    }//on_click_btn_common
 
    /**
     * 
     * @param {*} curr 
     * @param {*} all 
     */
    display_curr_all() {
        this.$curr.textContent = PagerModel.curr ?? "${1}";
        this.$all.textContent = PagerModel.all ?? "${999}";
    }//display_curr_all
 
    /**
     * 이전/다음 버튼 스타일 off로 바꾸기 및 disabled 설정
     */
    change_pager_btn(){
        if(this.currSave === 1){
            this.$btnPrev.classList.add("off");
            this.$btnNext.classList.remove("off");
            this.$btnPrev.disabled = true;
            this.$btnNext.disabled = false;
        }else if(this.currSave === PagerModel.all){
            this.$btnPrev.classList.remove("off");
            this.$btnNext.classList.add("off");
            this.$btnPrev.disabled = false;
            this.$btnNext.disabled = true;
        }else{
            this.$btnPrev.classList.remove("off");
            this.$btnNext.classList.remove("off");
            this.$btnPrev.disabled = false;
            this.$btnNext.disabled = false;
        }
    }//change_pager_btn
}//class-Pager
cs

PagerModel.js

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
export class PagerModel{
    constructor(){
        /* singleton */
        if (PagerModel.instance) {
            return PagerModel.instance;
        }
        PagerModel.instance = this;
        
        /*  */
        this.curr = null;
        this.all = null;
    }//constructor
 
    /**
     * 
     * @param {*} dataLength 
     */
    static cacul_initial(dataLength = 0){
        if(!dataLength){console.warn("no dataLength", dataLength);}
        this.curr = 1;
        this.all = Math.ceil(dataLength / 4);
    }//cacul_initial
 
    static currIncrease(){
        if(this.curr >= this.all){
            this.curr = this.all;
            return;
        }
        this.curr++;
        console.log("다음페이지"this.curr)
    }//currIncrease
 
    static currDecrease(){
        if(this.curr <= 1){
            this.curr = 1;
            return;
        }
        this.curr--;
        console.log("이전페이지"this.curr)
    }//currDecrease
}//class-PagerModel
cs

List.js

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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import { PagerModel } from "./PagerModel.js";
 
export class List {
    constructor() {
        /* singleton */
        if (List.instance) {
            return List.instance;
        }
        List.instance = this;
    }//constructor
 
    /**
     * 
     * @param {*} data 
     * @returns 
     */
    set_data(data) {
        List.data = data;
        return this;
    }//set_data
 
    /**
     * 
     */
    init() {
        // this.reset();
        List.fill();
    }//init
 
    /**
     * 
     */
    reset() {
        const $list = document.getElementsByClassName("list")?.[0];
        $list.innerHTML = "";
    }//reset
 
    static async fill() {
        const $list = document.getElementsByClassName("list")?.[0];
        const $$itemRemain = $list.querySelectorAll(".item");
 
        /* remove */
        if ($$itemRemain.length) {
            await List.remove_remain($$itemRemain);
        }
 
        /* slice data */
        const items = List.get_items();
 
        /* fill items */
        items.forEach((item) => {
            const $item = List.make_item(item);
            $list.appendChild($item);
            const $wrap = $item.querySelector(".item-inner-wrap"|| $item;
            $wrap.animate([
                {
                    transform:"translateX(100%)",
                    opacity:0,
                },
                {
                    transform:"translateX(0%)",
                    opacity:1,
                }
            ], {
                duration: 400,
                easing: "linear",
                fill: "both"
            });
        });
    }//fill
 
    static remove_remain($$itemRemain) {
        const length = $$itemRemain.length;
        return new Promise((res, rej) => {
            for (let i = 0; i < length; i++) {
                const $itemRemain = $$itemRemain[i];
                const $wrap = $itemRemain.querySelector(".item-inner-wrap"|| $itemRemain;
                const ani = $wrap.animate([
                    { opacity: 1, },
                    { opacity: 0, },
                    {
                        transform: "translateX(-100%)",
                        opacity: 0,
                    }
                ], {
                    duration: 600,
                    easing: "linear",
                    fill: "both"
                });
                ani.addEventListener("finish", () => {
                    $itemRemain.remove();
                    if (i === length - 1) { res("끝") }
                });
            }//for
 
        });
    }//remove_remain 
 
    /**
     * 
     * @returns 
     */
    static get_items() {
        const start = (PagerModel.curr - 1* 4;
        const end = PagerModel.curr * 4
        const items = List.data.slice(start, end);
        console.log(items);
        return items;
    }//get_items
 
    /**
     * 
     * @param {*} item 
     */
    static make_item(item) {
        const { star, peopleCount, title, content, date } = item;
        /* item */
        const $item = document.createElement("LI");
        $item.classList.add("item");
 
        /* wrap */
        const $wrap = document.createElement("DIV");
        $wrap.classList.add("item-inner-wrap");
        $item.appendChild($wrap);
 
        /* 별 */
        const $star = List.make_star({ star, peopleCount });
        $wrap.appendChild($star);
 
        /* 텍스트 */
        const $text = List.make_text({ title, content });
        $wrap.appendChild($text);
 
        /* 버튼 + 날짜 */
        const $info = List.make_info(date);
        $wrap.appendChild($info);
 
        /* 최종 */
        return $item;
    }//make_item
 
    /**
     * 
     * @param {*} param0 
     * @returns 
     */
    static make_star({ star, peopleCount }) {
        const $sect = document.createElement("SECTION");
        $sect.classList.add("star-sect");
 
        /* star */
        const $star = document.createElement("ARTICLE");
        $star.classList.add("star");
        $sect.appendChild($star);
 
        /* star-bg */
        $star.appendChild(List.make_star_text({ isShow: false }));
        /* star-sw */
        $star.appendChild(List.make_star_text({ isShow: true, score: star }));
 
        /* score */
        const $score = document.createElement("ARTICLE");
        $score.classList.add("star-score");
        $sect.appendChild($score);
 
        /* score-num */
        const $num = document.createElement("SPAN");
        $num.classList.add("star-score-num");
        $num.textContent = star;
        $score.appendChild($num);
 
        /* score-whole */
        const $whole = document.createElement("SPAN");
        $whole.classList.add("star-score-whole");
        $whole.textContent = ` (${peopleCount})`;
        $score.appendChild($whole);
 
        return $sect;
    }//make_star
 
    static make_star_text({ isShow = false, score }) {
        const $stars = document.createElement("DIV");
        $stars.classList.add(isShow ? "star-sw" : "star-bg");
        if (isShow) {
            $stars.style.setProperty("--sw", score);
        }
        $stars.textContent = "★★★★★";
        return $stars;
    }
 
    /**
     * 
     * @param {*} param0 
     * @returns 
     */
    static make_text({ title, content }) {
        const $text = document.createElement("SECTION");
        $text.classList.add('text');
 
        const $title = document.createElement("P");
        $title.classList.add("text-title");
        $title.textContent = title;
        $text.appendChild($title);
 
        const $content = document.createElement("DIV");
        $content.classList.add("text-content");
        $content.textContent = content;
        $text.appendChild($content);
 
        return $text;
    }//make_text
 
    /**
     * 
     * @param {*} date 
     * @returns 
     */
    static make_info(date) {
        const $info = document.createElement("SECTION");
        $info.classList.add("info");
 
        const $btn = document.createElement("BUTTON");
        $btn.classList.add("info-btn");
        $btn.textContent = "자세히 보기";
        $info.appendChild($btn);
 
        const $date = document.createElement("P");
        $date.classList.add("info-date");
        $date.textContent = date;
        $info.appendChild($date);
 
        return $info;
    }//make_info
}//class-List
cs