javascript - Image Slider that opens images from different folders on different button click -
i have image slider opens different images same folder(named images 1.jpg,2.jpg in folder img1). possible if can have buttons below image slider gives me sliding images different folders(1.jpg,2.jpg folder img2 ; 1.jpg,2.jpg folder img3)
<div id="slider"> <img src="img/1.jpg" alt="slide 1" /> <img data-src="img/2.jpg" alt="slide 2" /> <img data-src="img/3.jpg" alt="slide 3" /> <img data-src="img/4.jpg" alt="slide 4" /> </div><button>images new folder</button>
i'm guessing using library/code displays image in data-src
. can change 'data-src'
(or images' src
) attribute dynamically , change path using.
it depends on implementation since lib might cache data-src
on initialization , ignore changes it..
anyway, how can (i'm using src
in example can change data-src
well):
var gallery = document.queryselector('#gallery'); var button = document.queryselector('#your-button-id'); button.addeventlistener('click', () => setimagesfolder('foldername')); function setimagesfolder(folder) { gallery.queryselectorall('img').foreach(imgel => { var currpath = imgel.getattribute('data-src'); var basepath = currpath.slice(currpath.lastindexof('/')) console.log(folder + basepath) imgel.setattribute('src', folder + basepath) }) }
<div id="gallery"> <img data-src="folder/1.jpg"> <img data-src="folder/2.jpg"> <img data-src="folder/3.jpg"> <img data-src="folder/4.jpg"> <img data-src="folder/5.jpg"> </div> <button id="your-button-id">change images path</button>
Comments
Post a Comment