CitarTodo el código fuente está en Github: https://github.com/Gusgarsaky/lightsliderQUÉ NECESITAMOS
<section class="slider" data-img-folder="img/">
<button class="prevBtn"><i class="fa fa-2x fa-chevron-left"></i></button>
<button class="nextBtn"><i class="fa fa-2x fa-chevron-right"></i></button>
<img src="img/img1.jpg" alt="Image one" class="img" style="right: 0%"/>
</section>
html {
box-sizing: border-box;
}
*, *::before, *::after {
box-sizing: inherit;
}
.slider {
border-radius: 4px;
box-shadow: 0 15px 25px rgba(0,0,0,.15),
0 25px 60px rgba(0,0,0,.25);
display: block;
height: 360px;
margin: 30px auto;
overflow: hidden;
position: relative;
width: 650px;
}
.slider .img {
display: block;
height: 100%;
position: absolute;
transition: all .6s ease-in-out;
width: 100%;
}
.slider .img.left {
left: -100%;
}
.slider .img.right {
right: -100%;
}
.slider > .prevBtn, .slider > .nextBtn {
background-color: transparent;
border: none;
color: rgba(250,250,250,.8);
outline: none;
position: absolute;
top: calc(50% - 25px);
z-index: 1;
}
.slider > .prevBtn {
left: 10px;
}
.slider > .nextBtn {
right: 10px;
}
'use strict'
class Slider {
constructor(el) {
this.slider = el
this.prevBtn = el.querySelector('.prevBtn')
this.nextBtn = el.querySelector('.nextBtn')
this.images = []
this.current = 0
this.url = this.slider.getAttribute('data-img-folder')
this.init()
}
init() {
this.prevBtn.addEventListener('click', function() {
this.loadPrevImg()
}.bind(this))
this.nextBtn.addEventListener('click', function() {
this.loadNextImg()
}.bind(this))
this.loadImages()
}
loadImages() {
let self = this;
let imgUrls = [];
let http = new XMLHttpRequest()
http.open('GET', this.url, false)
http.onload = function() {
if(http.status === 200) {
let html = document.createElement('html')
html.innerHTML = this.response
let links = html.querySelectorAll('a')
for(let i=0; i<links.length; i++) {
let content = (links[i].innerText).trim()
if(self.isValidImg(content)) {
imgUrls.push(self.url+content)
}
}
} else {
console.log('error: '+http.response)
}
}
http.send()
this.createImagesBlob(imgUrls)
}
isValidImg(file) {
let suffixes = ['.jpg', '.jpeg', '.png', '.gif']
for(var i=0; i<suffixes.length; i++) {
if(endsWith(file, suffixes[i])) {
return true
}
}
function endsWith(str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
}
return false
}
createImagesBlob(imgUrls) {
let self = this;
for(let i=0; i<imgUrls.length; i++) {
let http = new XMLHttpRequest()
http.responseType = 'blob'
http.open('GET', imgUrls[i], true)
http.onload = function() {
if(this.status === 200) {
self.images.push(window.URL.createObjectURL(this.response))
}
}
http.send();
} // end for
}
loadPrevImg() {
let self = this
this.current = (this.current > 0) ? this.current - 1 : this.images.length - 1
let img = this.createImage('left')
this.slider.appendChild(img)
setTimeout(function() {
img.style.left = '0%'
// 600 = .6s of slide animation
setTimeout(function() {
self.removeBefore(img)
}, 600)
},30)
}
loadNextImg() {
let self = this
this.current = (this.current < this.images.length - 1) ? this.current + 1 : 0
let img = this.createImage('right')
this.slider.appendChild(img)
setTimeout(function() {
img.style.right = '0%'
// 600 = .6s of slide animation
setTimeout(function() {
self.removeBefore(img)
}, 600)
}, 30)
}
createImage(side) {
let img = document.createElement('img')
img.src = this.images[this.current]
img.classList.add('img')
if (side === 'right') {
img.classList.add('right')
} else {
img.classList.add('left')
}
return img
}
removeBefore() {
let child = this.slider.querySelector('img');
this.slider.removeChild(child);
}
setModeAuto() {
setInterval(function() {
this.loadNextImg()
}.bind(this), 5000)
}