I'm currently learning some javascript and I've been getting up to scratch with ES6. I have learned the basics and continued to build things that I've wanted to build in Javascript for a long time.
I am looking for someone to review the code, and to hopefully aid me in becoming a better developer in learning from mistakes.
So the image slider is working from an array which is a div with 4 hidden img elements. The is an img element which is being created within the div #image-display id. The JS then swaps the src attribute based off the number within the counter and the key from the array.
The working copy is on codepen: https://codepen.io/syncoed/pen/WmpEQJ
HTML
<div id="image-display" class="mx-auto">
<span id="left">❰</span>
<span id="right">❱</span>
</div>
<div id="img-container">
<img id="img-size" src="../image-slider/img/image1.jpg" hidden/>
<img id="img-size" src="../image-slider/img/image2.jpg" hidden/>
<img id="img-size" src="../image-slider/img/image3.jpg" hidden/>
<img id="img-size" src="../image-slider/img/image4.jpg" hidden/>
</div>
</div>
CSS
#image-display{
width:500px;
height: 100%;
border: 3px solid orange;
z-index: -1;
padding:3;
margin:0;
}
#img-size{
height:100%;
width:100%;
z-index: -1;
margin:0;
position: relative;
}
/* left arrow */
#left{
z-index: 0;
position: fixed;
left:21.5%;
top:10%;
cursor: pointer;
color:#ffffff;
padding:0;
}
/* right arrow */
#right{
z-index: 0;
position: fixed;
right:53.5%;
top: 10%;
cursor: pointer;
color:#ffffff;
padding:0;
}
/*Desktop Media Queries*/
@media(min-width: 1573px) {
#right{
right:35%;
top: 12%;
}
#left{
left:35.5%;
top:12%;
}
}
@media(min-width: 1613px) {
#right{
right:35.5%;
top: 11%;
}
#left{
left:35.5%;
top:11%;
}
}
@media(min-width: 1140px) {
#right{
right:37.5%;
top: 11%;
}
#left{
left:37.5%;
top:11%;
}
}
Javascript
const imgDisplay = document.querySelector('#image-display');
const allImages = document.querySelector('#img-container');
//gets all images
let all = allImages.querySelectorAll('img');
var imageContainer = document.querySelector('.selector');
let counter = 0;
var newImg = document.createElement('img');
newImg.setAttribute('id', 'img-size');
newImg.setAttribute('src', all[0].src);
imgDisplay.appendChild(newImg);
let imageSelected = all[counter];
imgDisplay.addEventListener('click', (e) => {
if (counter == all.length - 1 && e.target.id == "right") {
newImg.setAttribute('src', all[0].src);
counter = 0;
} else if (counter == 0 && e.target.id == 'left') {
counter = all.length - 1;
newImg.setAttribute('src', all[counter].src);
} else {
if (e.target.tagName == 'SPAN' && e.target.id == 'right') {
counter++;
newImg.setAttribute('src', all[counter].src);
}
if (e.target.tagName == 'SPAN' && e.target.id == 'left') {
counter--;
newImg.setAttribute('src', all[counter].src);
}
}
});