Language/JavaScript
[JavaScript] 자동 이미지 슬라이더 만들기
rame
2022. 8. 29. 14:24
1. html 파일 작성
2. css 파일 작성
3. javascript 파일 작성
4. 완성 화면
1. html 파일 작성
- 간단하게 이미지 부분과 밑에 이동할 때마다 표시해 줄 점을 만들어줌
<h2>이미지 슬라이드 8/29</h2>
//이미지 삽입
<div class="slideshow-container">
<img class="mySlides fade" src="img/1.png" alt="">
<img class="mySlides fade" src="img/2.png" alt="">
<img class="mySlides fade" src="img/3.png" alt="">
<img class="mySlides fade" src="img/4.png" alt="">
<img class="mySlides fade" src="img/5.png" alt="">
<img class="mySlides fade" src="img/6.png" alt="">
<img class="mySlides fade" src="img/7.png" alt="">
<img class="mySlides fade" src="img/8.png" alt="">
<img class="mySlides fade" src="img/9.png" alt="">
<img class="mySlides fade" src="img/10.png" alt="">
<img class="mySlides fade" src="img/11.png" alt="">
<img class="mySlides fade" src="img/12.png" alt="">
</div>
//이미지의 갯수 만큼 만들어줘야 함
<div style="text-align:center">
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
</div>
2. css 파일 작성
- 부트스트랩을 적용했다면 fade 부분이 정상 작동하지 않을 수 있음
* {box-sizing: border-box}
body {font-family: Verdana, sans-serif; margin:0}
.mySlides {display: none}
.slideshow-container img {vertical-align: middle; width: 100%}
/* Slideshow container */
.slideshow-container {
max-width: 1000px;
position: relative;
margin: auto;
width: 70%;
}
/* Next & previous buttons */
.prev, .next {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
padding: 16px;
margin-top: -22px;
color: white;
font-weight: bold;
font-size: 18px;
transition: 0.6s ease;
border-radius: 0 3px 3px 0;
user-select: none;
}
/* Position the "next button" to the right */
.next {
right: 0;
border-radius: 3px 0 0 3px;
}
/* The dots/bullets/indicators */
.dot {
cursor: pointer;
height: 5px;
width: 5px;
margin: 0 2px;
background-color: #bbb;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
}
.active, .dot:hover {
background-color: #717171;
}
/* Fading animation */
.fade {
-webkit-animation-name: fade;
-webkit-animation-duration: 1.5s;
animation-name: fade;
animation-duration: 1.5s;
}
@-webkit-keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
@keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
/* On smaller screens, decrease dot size */
@media only screen and (max-width: 300px) {
.dot {visibility: hidden}
}
3. javascript 코드 작성
<script>
var slideIndex = 0;
showSlides();
function showSlides() {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("dot");
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
slideIndex++;
if (slideIndex > slides.length) {slideIndex = 1}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slides[slideIndex-1].style.display = "block";
dots[slideIndex-1].className += " active";
setTimeout(showSlides, 2000); // Change image every 2 seconds
}
</script>
4. 완성 화면