김 양의 멋따라 개발따기
[HTML, CSS, JavaScript]carousel 구현하기 본문
1. HTML 코드
<section id="image" class="carouselContainer" style="height: 1440px; width: 1080px;">
<div class="carousel">
</div>
</section>
2. CSS 코드
.carouselContainer {
width: 1080px;
height: 1440px;
overflow-x: hidden;
overflow-y: hidden;
margin: auto;
}
.carouselContainer > .carousel {
display: flex;
transform: translate3d(0, 0, 0);
transition: transform 0.2s;
}
.carousel_item {
width: 1080px;
height: 1440px;
}
.carousel_item > img {
width: 1080px;
height: 1440px;
object-fit: contain;
}
3. JavaScript 코드
/**이미지 다음으로 넘기기 */
function goNextImg(img){
if(now < imgList.length - 1){
now = now + 1;
}else{
now = imgList.length - 1
}
carousel.style.transform = `translate3d(-${
1080 * now
}px, 0, 0)`;
}
/**이미지 이전으로 넘기기 */
function goPreImg(img){
if(now > 0){
now = now - 1;
carousel.style.transform = `translate3d(-${
1080 * now
}px, 0, 0)`;
}}
'Javascript' 카테고리의 다른 글
JavaScript - 간단하게 새 창과 소통하는 Broadcast Channel API (1) | 2024.11.08 |
---|---|
JavaScript - 한글로 입력했을 때 영문으로 변환하기 (0) | 2024.09.27 |
jQuery로 모바일 키보드 이벤트 감지하기 (0) | 2024.01.19 |
Javascript - animation이 끝나는 이벤트를 감지하기 (0) | 2023.11.06 |
터치 프로그래밍시 의도치 않은 에러를 방지하기 위한 삼총사 (1) | 2023.09.20 |