I built an image slider with jQuery and thought it would be a good exercise to do the same in vanilla javascript (which is an awesome btw structure;)). Itβs good to know that although I can build things with jQuery, I have very little understanding of what is really happening under the hood.
Anyway, here is my problem. I am trying to set the width of the image that I added to the list item. For some reason, the code I wrote does not affect the width property of the image. I updated css as a health check and it worked fine, so this should be a js issue.
Here is the code:
console.log('Yeh, bitch! Programming!'); function slider() { var settings = { width: "700px" }; // end settings object var sliderImages = document.getElementById('slider-images').querySelectorAll('img'), prevImg = document.getElementById('prev-img'), currentImg = document.getElementById('current-img'), nextImg = document.getElementById('next-img'), wrapperInner = document.getElementById('img-wrapper-inner'), imgList = [], imgAttr; for (var i = 0; i < sliderImages.length; i++) { sliderImages[i].style.display = "none"; }; function grabImages() { var DOM_img; for (var i = 0; i < sliderImages.length; i++) { imgSrc = sliderImages[i].getAttribute('src'); DOM_img = document.createElement('img'); DOM_img.src = imgSrc; imgList.push(DOM_img); console.log(imgList[i]); }; // end for prevImg.appendChild(imgList[imgList.length -1]); currentImg.appendChild(imgList[0]); nextImg.appendChild(imgList[1]); }; // end grabImages grabImages(); var visibleImages = wrapperInner.querySelectorAll('ul li img'); visibleImages.style = 'width: ' + settings.width; };// end slider slider();
Let me know if css and html are needed. I just try not to make the message too long.
source share