Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
# javascript-amazon
레벨3
36 changes: 36 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.12.1/css/all.css" integrity="sha384-v8BU367qNbs/aIZIxuivaU55N5GPF89WBerHoGA4QTcbUjYiLQtKdrfXnqAcXyTv" crossorigin="anonymous">
<link rel="stylesheet" href="style.css">
</head>

<body>

<div class="carousel-wrapper">
<div class='carousel__menu'>
<ul class='menu__list'>
</ul>
</div>
<!-- carousel__container start -->
<div class="carousel">
<div class="carousel__container">
<!-- carousel item start-->
<!-- carousel item end -->
</div>
</div>

<!-- carousel__container end -->
<div class="carousel__button--prev">
<i class="fas fa-arrow-left"></i>
</div>
<div class="carousel__button--next">
<i class="fas fa-arrow-right"></i>
</div>
</div>
<script type="module" src="index.js"></script>
<script src="./prac.js"></script>
</body>
</html>
71 changes: 71 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
class Carousel {
constructor (menuData,contentData) {
this.menuData=menuData;
this.contentData=contentData;
this.makingMenuList();
this.makingSlider();
this.btnHandler();
this.count = 0;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

무슨 count인지 잘 모르겠어요. 구체적으로 의도를 드러내세요.

}
changeLocation (element,index) {
this.count = index;
const width = element.children[0].getBoundingClientRect().width;
element.style.transform = `translateX(${-this.count*width}px)`;
}
scaleMenu (element,index) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

대체로 메서드의 크기, 역할 둘다 좋네요.

this.count = index;
Array.from(element.parentNode.children).forEach((element)=>element.style.transform=`scale(0.99)`)
element.parentNode.children[this.count].style.transform = 'scale(1.1)';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 표현은 너무 길어요.
가로로 너무 길면 변수화를 시켜두는 게 좋아요.
디버깅을 할때도 유리하고요.

const parent = element.parentNode;

}
btnHandler () {
const left =document.querySelector('div.carousel__button--prev');
const right =document.querySelector('div.carousel__button--next');
[left,right].forEach((btn,index)=>{
btn.addEventListener('click',()=>{
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

수업때 말한 것처럼 콜백으로 동작하는 핸들러 같은 함수를 분리해보세요~

index === 0 ? this.count-- : this.count++;
if (this.count >3) this.count =0;
if (this.count<0) this.count =3;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0,3 이런건 매직넘버에요. 변수화 시켜서 의미를 붙여서 사용하는게 더 좋아요.

const el = document.querySelector('.carousel__container');
this.changeLocation(el,this.count);
this.scaleMenu(document.querySelector('.menu__list>li'),this.count);
});
})
}
makingMenuList () {
const menuConatiner=document.querySelector('.menu__list')
this.menuData.forEach((data)=>{
menuConatiner.innerHTML +=`
<li><a href='#'>${data}</a></li>`
})
Array.from(menuConatiner.children).forEach((li,index)=>{
li.addEventListener('click',()=>{
this.scaleMenu(li,index);
this.changeLocation(document.querySelector('.carousel__container'),index);
})
})
}
makingSlider () {
const carouselContainer = document.querySelector('.carousel__container');
carouselContainer.innerHTML=this.contentData.map((data,index)=>`
<div class='carousel__contents'>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

html template 문자열이 너무 길다면 코드가 지저분해질 수 있겠죠?
만약 그렇다면 문자열도 다른곳에 선언해서 모아둘 수 있을거에요.

물론 이렇게 지금한것처럼 관련로직에 두고, 어떤 내용의 HTML 업데이트가 일어나는지 볼 수 있는 것도 응집도 차원에선 좋고요.

<div class='carousel__text'>
<ul>
<h3>${data.title}</h3>
${data.desc.map((desc)=>`<li>${desc}</li>`).join('')}
</ul>
</div>
<div class='carousel__img'>
<img src=${data.imgUrl} />
</div>
</div>`).join('');
}
}

window.addEventListener('DOMContentLoaded',()=>{
fetch('./localData.json')
.then((data)=>data.json())
.then((data)=>{
const {menuData,contentData}=data;
const carousel=new Carousel(menuData,contentData);
})
});
35 changes: 35 additions & 0 deletions localData.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"menuData" : [
"Fast, Free Delivery",
"Exclusive deals and offers",
"Prime Originals, movies and TV shows",
"Over 2 million song ad free"
],
"contentData" : [{
"id" : "1",
"imgUrl" : "https://placedog.net/200/250?id=8",
"title" :"Enjoy exclusive savings and rewards",
"desc" : ["Fast, Free Delivery",
"Exclusive deals and offers","Prime Originals, movies and TV shows","Over 2 million song ad free"]
},{
"id" : "2",
"imgUrl" : "https://placedog.net/200/250?id=12",
"title" :"Code Squad is awesome place",
"desc" : ["Code, Feel JS",
"Do you want to be Front Engeneer?","Come to us Master,Good People","Waiting For you!"]
},{
"id" : "3",
"imgUrl" :"https://placedog.net/200/250?id=14",
"title" :"Learning With practice and enthusiastic people",
"desc" : ["React to ur Mind",
"Es6 class is good for you","We will find answer","As we've done so far"]
},{
"id" : "4",
"imgUrl" : "https://placedog.net/200/250?id=15",
"title" :"Watching Dog makes inner-peace",
"desc" : ["It was hard Day.",
"Do you feel exhausted?","Come to Our good puppy","puppy would be with you"]
}
]
}

148 changes: 148 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
* {
box-sizing: border-box;
margin:0;
padding:0;
}
body,html {
margin: 0;
padding:0;
}

.carousel__menu {
padding-top: 50px;
display: flex;
}
.carousel {
margin: 0 auto;
height: 270px;
width: 600px;
overflow: hidden;
position: relative;
}
.carousel__container {
position: relative;
display: flex;
margin-top: 40px;
width: 2450px;
height: 220px;
transition:transform .5s ease-in-out;
-webkit-transition:transform .5s ease-in-out;
}
.carousel__contents {
display: flex;
width: 600px;
height: 220px;
position: relative;
justify-content: center;
align-items: center;
}
.carousel__container .carousel__text {
width: 400px;
display: inline-block;
}
.carousel__container .carousel__text >ul>li {
margin: 3px 0;
font-size: 18px;
}
.carousel__img {
display: inline-block;
width: 220px;
}
.carousel__img > img {
width: 70%;
height: 70%;
border-radius: 50%;
}
.carousel__container .carousel__text {
line-height: 1.4;
}
.carousel__container .carousel__text > ul {
list-style: none;
font-weight: 500;
}
.carousel__button--prev {
width: 30px;
height: 30px;
font-size: 20px;
position: absolute;
display: flex;
justify-content: center;
align-items: center;
left: 10px;
top:50%;
transform: translateY(-50%);
}
.carousel__button--next {
width: 40px;
height: 40px;
font-size: 20px;
position: absolute;
display: flex;
justify-content: center;
align-items: center;
right: 20%;
top: 40%;
border: 1px solid black;
transform: translateY(-50%);
border-radius: 50%;
color: white;
background: gray;
}
.carousel__button--prev {
width: 40px;
height: 40px;
font-size: 20px;
position: absolute;
display: flex;
justify-content: center;
align-items: center;
left: 20%;
top: 40%;
border: 1px solid black;
transform: translateY(-50%);
border-radius: 50%;
color: white;
background: gray;
}
.menu__list {
margin:0 auto;
display: flex;
justify-content: space-between;
-webkit-transition: transform .5s;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-webkit 요놈을 붙여야 할지 안붙여도 될지 한번 찾아보세요.
transition의 지원범위를 살펴보시면 될 듯.

transition:transform .5s;
}
.menu__list > li {
text-align: center;
width:24%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 80px;
padding:0 3px;
-webkit-transition: transform .5s;
transition:transform .5s;
}
.menu__list >li:last-child {
margin-right:0;
}
.menu__list>li>a {
text-decoration: none;
color:black
}

.menu__list>li:first-child {
background-color: #2591C0;
}

.menu__list>li:nth-child(2) {
background-color: #A90067;
}

.menu__list>li:nth-child(3) {
background-color: #008577;
}

.menu__list>li:nth-child(4 ) {
background-color:#E65D16;
}