Day 14 | Demo SliderShow,Carousel

Sử dụng HTML , CSS, JS  thiết kế chức năng SliderShow, Carousel

 
 
 
				
					<body>
		<div class="container">
			<div class="main">
				<span class="control prev">
					<i class="bx bx-chevron-left"></i>
				</span>
				<span class="control next">
					<i class="bx bx-chevron-right"></i>
				</span>
				<div class="img-wrap">
					<img decoding="async" src="img1.jpeg" alt="" />
				</div>
			</div>
			<div class="list-img">
				<div>
					<img decoding="async" src="img1.jpeg" alt="" />
				</div>
				<div>
					<img decoding="async" src="img2.jpeg" alt="" />
				</div>
				<div>
					<img decoding="async" src="img3.jpeg" alt="" />
				</div>
				<div>
					<img decoding="async" src="img4.jpeg" alt="" />
				</div>
				<div>
					<img decoding="async" src="img5.jpeg" alt="" />
				</div>
				<div>
					<img decoding="async" src="img6.jpeg" alt="" />
				</div>
				<div>
					<img decoding="async" src="img7.jpeg" alt="" />
				</div>
				<div>
					<img decoding="async" src="img8.jpeg" alt="" />
				</div>
				<div>
					<img decoding="async" src="img9.jpeg" alt="" />
				</div>
			</div>
		</div>

		<script src="app.js"></script>
	</body>
				
			
				
					* {
	padding: 0;
	margin: 0;
	box-sizing: border-box;
	font-family: sans-serif;
}

body {
	width: 100%;
	height: 100vh;
	background-color: #dadada;
}

img {
	width: 100%;
	height: 100%;
	object-fit: cover;
	object-position: center;
}

.container {
	margin: 50px auto;
	height: 80%;
	width: 70%;
	box-shadow: 0 0 10px #bbb;
}

.main {
	height: 85%;
	position: relative;
}

.control {
	position: absolute;
	top: 50%;
	transform: translateY(-50%);
	font-size: 80px;
	color: white;
	cursor: pointer;
}

.prev {
	left: -10px;
}

.next {
	right: -10px;
}

.img-wrap {
	width: 100%;
	height: 100%;
}

.list-img {
	display: flex;
}

.list-img div {
	cursor: pointer;
	padding: 5px;
	background-color: #bbb;
	flex: 1;
}

.list-img div.active {
	background-color: rgb(220, 86, 86);
}
				
			
				
					let listDivImg = document.querySelectorAll('.list-img div')
let next = document.querySelector('.next')
let prev = document.querySelector('.prev')
let imgWrap = document.querySelector('.img-wrap img')

let currentIndex = 0

setCurrent(currentIndex)

function setCurrent(index) {
	currentIndex = index
	imgWrap.src = listDivImg[currentIndex].querySelector('img').src

	// remove all active img
	listDivImg.forEach((item) => {
		item.classList.remove('active')
	})

	// set active
	listDivImg[currentIndex].classList.add('active')
}

listDivImg.forEach((img, index) => {
	img.addEventListener('click', (e) => {
		setCurrent(index)
	})
})

next.addEventListener('click', () => {
	if (currentIndex == listDivImg.length - 1) {
		currentIndex = 0
	} else currentIndex++

	setCurrent(currentIndex)
})

prev.addEventListener('click', () => {
	if (currentIndex == 0) currentIndex = listDivImg.length - 1
	else currentIndex--

	setCurrent(currentIndex)
})

				
			
LỘ TRÌNH LÊN FULLSTACK, trọn bộ serial course Pro HTML,CSS,JAVASCRIPT MIỄN PHÍ :

HTML : HTML căn bản cho người mới bắt đầu

CSS : CSS căn bản cho người mới bắt đầu

JS: Javascript cho người mới bắt đầu 

 
 
 

Leave a Reply

Your email address will not be published. Required fields are marked *