자바스크립트 슬라이더 스벨트로 바꾸기(Javascript to Svelte slider)
Computer 관심/Svelte2020. 12. 14. 23:31
반응형
스벨트로 슬라이더 컴포넌트를 만들어보면 재미있을 것 같았다. 직접 만들면 디자인이 좋지 않을 것 같아서
자바스크립트로 만들어진 슬라이더를 인터넷에서 찾아 스벨트 컴포넌트로 바꾸어 보았다.
www.w3schools.com/howto/howto_js_slideshow.asp
html
Original
<!-- Slideshow container -->
<div class="slideshow-container">
<!-- Full-width images with number and caption text -->
<div class="mySlides fade">
<div class="numbertext">1 / 3</div>
<img src="img1.jpg" style="width:100%">
<div class="text">Caption Text</div>
</div>
<div class="mySlides fade">
<div class="numbertext">2 / 3</div>
<img src="img2.jpg" style="width:100%">
<div class="text">Caption Two</div>
</div>
<div class="mySlides fade">
<div class="numbertext">3 / 3</div>
<img src="img3.jpg" style="width:100%">
<div class="text">Caption Three</div>
</div>
<!-- Next and previous buttons -->
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>
<br>
<!-- The dots/circles -->
<div style="text-align:center">
<span class="dot" onclick="currentSlide(1)"></span>
<span class="dot" onclick="currentSlide(2)"></span>
<span class="dot" onclick="currentSlide(3)"></span>
</div>
Svelte
<!-- Slideshow container -->
<div class="slideshow-container">
{#each slideData as slide}
<!-- Full-width images with number and caption text -->
<div class="fade {slide.show ? '':'mySlides'}">
<div class="numbertext">{slide.id+1} / {slides.length}</div>
<img src={slide.src} style="width:100%" alt="img_nature_wide">
<div class="text">{slide.caption}</div>
</div>
{/each}
<!-- Next and previous buttons -->
<span class="prev" on:click={() => plusSlides(-1)}>❮</span>
<span class="next" on:click={() => plusSlides(+1)}>❯</span>
</div>
<br>
<!-- The dots/circles -->
<div style="text-align:center">
{#each slideData as slide}
<span class="dot {slide.show==true?'active':''}" on:click={()=>currentSlide(slide)}></span>
{/each}
</div>
Javascript
original
var slideIndex = 1;
showSlides(slideIndex);
// Next/previous controls
function plusSlides(n) {
showSlides(slideIndex += n);
}
// Thumbnail image controls
function currentSlide(n) {
showSlides(slideIndex = n);
}
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("dot");
if (n > slides.length) {slideIndex = 1}
if (n < 1) {slideIndex = slides.length}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
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";
}
svelte
var slideIndex = 0;
let slideData =[{
id:0,
caption:'안녕하세요.',
src:'https://www.w3schools.com/howto/img_nature_wide.jpg',
show:false
},{
id:1,
caption:'방갑습니다.',
src:'https://www.w3schools.com/howto/img_snow_wide.jpg',
show:false
},{
id:2,
caption:'여기에요.',
src:'https://www.w3schools.com/howto/img_lights_wide.jpg',
show:false
}
]
slideData[slideIndex].show = true
// Next/previous controls
function plusSlides(value) {
let num = slideIndex + value
slideData[slideIndex].show = false
if (num == slides.length){
slideIndex = 0
} else if(num < 0){
slideIndex = slides.length+value
} else {
slideIndex = num
}
slideData[slideIndex].show = true
}
// Thumbnail image controls
function currentSlide(obj) {
slideData[slideIndex].show = false
slideIndex =obj.id
slideData[slideIndex].show = true
}
Style
style은 바뀐게 없음
* {box-sizing:border-box}
/* Slideshow container */
.slideshow-container {
max-width: 1000px;
position: relative;
margin: auto;
}
/* Hide the images by default */
.mySlides {
display: none;
}
/* Next & previous buttons */
.prev, .next {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
margin-top: -22px;
padding: 16px;
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;
}
/* On hover, add a black background color with a little bit see-through */
.prev:hover, .next:hover {
background-color: rgba(0,0,0,0.8);
}
/* Caption text */
.text {
color: #f2f2f2;
font-size: 15px;
padding: 8px 12px;
position: absolute;
bottom: 8px;
width: 100%;
text-align: center;
}
/* Number text (1/3 etc) */
.numbertext {
color: #f2f2f2;
font-size: 12px;
padding: 8px 12px;
position: absolute;
top: 0;
}
/* The dots/bullets/indicators */
.dot {
cursor: pointer;
height: 15px;
width: 15px;
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}
}
'Computer 관심 > Svelte' 카테고리의 다른 글
sveltekit에서 프린팅 하는 노하우 (scoped css) (0) | 2021.10.15 |
---|---|
스벨트킷(sveltekit) 넷틀리파이 호스팅하기 (0) | 2021.04.29 |
[svelte] 스벨트 패키지 만들기 (0) | 2020.12.23 |
[Svelte] firebase로 svelte 호스팅하기 (0) | 2020.12.19 |
Svelte 가 좋은 이유. (0) | 2020.12.08 |
댓글()