-
Notifications
You must be signed in to change notification settings - Fork 35
Carousel slider[Fetch이용] #157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +0,0 @@ | ||
| # javascript-amazon | ||
| 레벨3 | ||
| 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> |
| 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; | ||
| } | ||
| changeLocation (element,index) { | ||
| this.count = index; | ||
| const width = element.children[0].getBoundingClientRect().width; | ||
| element.style.transform = `translateX(${-this.count*width}px)`; | ||
| } | ||
| scaleMenu (element,index) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)'; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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',()=>{ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| }) | ||
| }); | ||
| 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"] | ||
| } | ||
| ] | ||
| } | ||
|
|
| 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; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. -webkit 요놈을 붙여야 할지 안붙여도 될지 한번 찾아보세요. |
||
| 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; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
무슨 count인지 잘 모르겠어요. 구체적으로 의도를 드러내세요.